如何防止内联块 div 换行?

cssweb developmentfront end technology更新于 2024/10/17 5:32:17

在 CSS 中,"display"属性用于设置子元素的显示方式。当我们将 display 属性设置为"inline-block"时,它会并排显示所有子元素。此外,它会自动创建响应式设计,因为如果空间不足,它会自动换行子元素。

有时,我们需要停止换行子元素来管理网页空间。在这种情况下,我们可以管理 CSS 的"white-space"属性,以避免子元素换行。

语法

用户可以按照以下语法,使用"white-space"CSS 属性来防止内联块 div 换行。

CSS_selector {
    white-space: nowrap;
}

在上述语法中,CSS_selector 是所有设置了"inline-block"显示的子元素的父元素。

让我们通过以下示例了解如何防止 inline-block 元素换行。

示例 1

在下面的示例中,我们创建了包含"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>

示例 2

在下面的示例中,我们添加了多个包含不同数据的表格。第一个表格包含学校数据,第二个表格包含 AC 数据。

我们将两个表格的 display 设置为等于内联块,以便它们在网页上并排显示。此外,我们将"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>

示例 3

在下面的示例中,我们演示了如何有条件地阻止内联块 div 元素换行。如果我们需要在任何特定条件下阻止内联块 div 元素换行,可以使用 JavaScript。

在 JavaScript 中,我们访问所有 div 元素,并使用 forEach() 方法遍历所有 div 元素。我们使用 style 对象的"whitespace"属性,通过 JavaScript 阻止所有内联块 div 换行。

<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 元素换行。我们使用了 CSS 属性"white-space"来防止 div 元素换行。然而,防止内联块 div 元素换行并不是一个好习惯,因为它会降低网页的响应能力。但在某些特定情况下,如果我们不想让 div 元素垂直扩展,我们可以防止这种情况发生。


相关文章


有用资源