
在CSS中,‘display’属性用于设置子元素的显示。当我们为 display 属性设置“inline-block”值时,它会并排显示所有子元素。此外,它会自动创建响应式设计,就好像它没有找到足够的空间一样,它会自动包裹子元素。
有时,我们需要停止包装子元素来管理网页空间。在这种情况下,我们可以管理 CSS 的“white-space”属性,以避免包裹子元素。
用户可以遵循以下语法,使用“white-space”CSS 属性来防止内联块 div 换行。
CSS_selector {
white-space: nowrap;
}
在上面的语法中,CSS_selector是我们设置“inline-block”显示的所有子元素的父元素。
让我们通过下面的示例来了解如何防止内联块元素换行。
在下面的示例中,我们创建了包含“container”类名称的父 div 元素。之后,我们在父容器中添加了三个子元素,每个子元素都包含“inline-block-div”类名。
在 CSS 中,我们对父容器使用“white-space: no-wrap”CSS 属性,对所有子元素使用“display: inline-block”。此外,我们还使用了一些其他 CSS 属性来设置 div 元素的样式。
在输出中,用户可以尝试减小浏览器窗口的大小,并观察内联块 div 元素没有换行,也没有进入下一行。
<html>
<head>
<style>
.container {
white-space: nowrap;
}
.inline-block-div {
display: inline-block;
width: 200px;
height: 100px;
border: 1px solid black;
margin: 10px;
}
</style>
</head>
<body>
<h2> Preventing the <i> inline-block divs </i> from wrapping </h2>
<div class = "container">
<div class = "inline-block-div"> Div 1 </div>
<div class = "inline-block-div"> Div 2 </div>
<div class = "inline-block-div"> Div 3 </div>
</div>
</body>
</html>
在下面的示例中,我们添加了多个包含不同数据的表。第一个表包含学校数据,第二个表包含 AC 数据。
我们将两个表的显示设置为等于内联块,以便在网页上并排显示它们。此外,我们将“white-space”属性与“container”div 元素一起使用。
在输出中,我们可以并排观察两个表格,如果我们减小窗口大小,表格不会进入下一行,因为我们防止两个表格元素换行。
<html>
<head>
<style>
.container {white-space: nowrap;}
.table {white-space: nowrap; display: inline-block; vertical-align: top; margin: 10px; border: 1px solid black;}
th, td {padding: 10px 40px; border: 1px solid black;}
</style>
</head>
<body>
<h2> Preventing the <i> inline-block divs </i> from wrapping </h2>
<div class = "container">
<table class = "container table">
<tr><th> school Name </th> <th> Total Students </th> </tr>
<tr><td> ABC School </td> <td> 100 </td></tr>
</table>
<table class = "container table">
<tr><th> AC brand </th> <th> color </th><th> Price </th></tr>
<tr><td> LG </td> <td> White </td> <td> 10000 </td> </tr>
</table>
</div>
</body>
</html>
在下面的示例中,我们演示了如何防止内联块 div 元素有条件地换行。如果我们需要防止内联块 div 元素在任何特定条件下换行,我们可以使用 JavaScript。
在 JavaScript 中,我们访问所有 div 元素并使用 forEach() 方法迭代所有 div 元素。我们使用样式对象的“whitespace”属性来防止所有内联块 div 使用 JavaScript 换行。
<html>
<head>
<style>
.child {width: 300px; background-color: blue; height: 200px; display: inline-block; margin: 10px; color: white; font-size: 30px;}
</style>
</head>
<body>
<h2> Preventing the <i> inline-block divs </i> from wrapping </h2>
<div class = "parent">
<div class = "child"> First </div>
<div class = "child"> Second </div>
<div class = "child"> Third </div>
<div class = "child"> Fourth </div>
</div>
<script>
let divs = document.querySelectorAll('div');
let divsArray = Array.from(divs);
// add white-space: nowrap to all div
divsArray.forEach(div => {
div.style.whiteSpace = 'nowrap';
});
</script>
</body>
</html>
用户学会了如何防止内联块 div 元素换行。我们使用“white-space”CSS 属性来防止 div 元素换行。然而,阻止内联块 div 元素换行并不是一个好的做法,因为它会消除网页的响应能力,但在某些特定情况下,如果我们不想垂直扩展 div 元素,我们可以阻止这种情况。
以上就是如何防止inline-block div换行?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号