min-content 和 max-content 是如何工作的?
在 CSS 中,我们使用长度、百分比和关键字值来定义元素的大小。对于希望自由有效地呈现网页内容的开发人员来说,CSS 大小调整属性是必要的。您可以使用它来应用网站样式。更重要的是,您可以这样做而不必考虑每个网页使用的 HTML 代码。
有时我们会使用 fit-content、min-content 和 max-content 关键字值类型。让我们进入文章,了解 min-content 和 max-content 的工作原理。
CSS min-content
关键字“min-content 大小调整”表示内容固有的最小宽度。这意味着内容将利用所有软换行机会缩小到最长单词的大小。
语法
以下是 CSS min-content 的语法
<style>
{
width: min-content;
}
</style>
示例
让我们看下面的例子,我们将使用 min-content 并观察输出。
<!DOCTYPE html>
<html>
<head>
<style>
.tutorial {
width: min-content;
border: 2px solid #DE3163;
}
body {
background-color: #D5F5E3;
font-family: verdana;
}
</style>
</head>
<body>
<center>
<div class="tutorial">
<img src="https://tutorialspoint.com/cg/images/logo.png" alt="LOGO" />
<h1>Tutorials Point originated from the idea that there exists a class of readers who respond better to online content and prefer to learn new skills at their own pace from the comforts of their drawing rooms.</h1>
</div>
</center>
</body>
</html>
当我们运行以上代码时,它将生成一个包含图像以及文本的输出,这些文本显示为网页上显示的框中的收缩,因为我们已使用 min-content 提到了它。
CSS max-content
短语 max-content 大小调整隐式地表示内容的最大宽度或高度。这表示文本内容永远不会换行,即使这会导致溢出。
语法
以下是 CSS max-content 的语法
<style>
{
width: max-content;
}
</style>
示例
以下是如何在网页上实现 max-content 的示例。
<!DOCTYPE html>
<html>
<head>
<style>
.tutorial {
width: max-content;
border: 2px solid #196F3D;
}
body {
background-color: #D6EAF8;
font-family: verdana;
}
</style>
</head>
<body>
<center>
<div class="tutorial">
<img src="https://tutorialspoint.com/cg/images/logo.png" alt="LOGO" />
<h1>Tutorials Point originated from the idea that there exists a class of readers who respond better to online.</h1>
</div>
</center>
</body>
</html>
运行以上代码后,输出窗口将弹出,显示图像以及在网页上没有任何换行的文本。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP