Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

Bootstrap 5 网格布局:堆叠到水平


网格示例:堆叠到水平

让我们创建一个基本的网格系统,它在超小型设备上最初是堆叠的,然后在较大的设备上变成水平排列。

以下示例显示了一个简单的“堆叠到水平”的两列布局,这意味着它将在所有屏幕上产生 50%/50% 的分割,除了超小型屏幕,它会自动堆叠(100%)。

col-sm-6
col-sm-6

示例:堆叠到水平

<div class="container-fluid">
  <div class="row">
    <div class="col-sm-6 bg-primary">
      <p>Lorem ipsum...</p>
    </div>
    <div class="col-sm-6 bg-dark">
      <p>Sed ut perspiciatis...</p>
    </div>
  </div>
</div>
亲自尝试 »

提示:.col-sm-* 类中的数字表示 div 应该跨越多少列(总共 12 列)。因此,.col-sm-1 跨越 1 列,.col-sm-4 跨越 4 列,.col-sm-6 跨越 6 列,依此类推。

注意:确保总和加起来不超过 12(不需要使用所有 12 个可用列)。

提示:可以通过将 .container-fluid 类更改为 .container,将任何全宽布局转换为固定宽度响应式布局。

示例:响应式容器

<div class="container">
  <div class="row">
    <div class="col-sm-6">
      <p>Lorem ipsum...</p>
    </div>
    <div class="col-sm-6">
      <p>Sed ut perspiciatis...</p>
    </div>
  </div>
</div>
亲自尝试 »


自动布局列

在 Bootstrap 5 中,有一种简单的方法可以为所有设备创建等宽列:只需从 .col-size-* 中删除数字,并在指定数量的col 元素上仅使用 .col-size 类即可。Bootstrap 会识别有多少列,并且每列都将获得相同的宽度。size 类(sm、md 等)决定了何时列应该具有响应性。

<!-- 两列:在所有屏幕上宽度为 50%,除了超小型屏幕(宽度为 100%) -->
<div class="row">
  <div class="col-sm">1 of 2</div>
  <div class="col-sm">2 of 2</div>
</div>

<!-- 四列:在所有屏幕上宽度为 25%,除了超小型屏幕(宽度为 100%)-->
<div class="row">
  <div class="col-sm">1 of 4</div>
  <div class="col-sm">2 of 4</div>
  <div class="col-sm">3 of 4</div>
  <div class="col-sm">4 of 4</div>
</div>
1 of 2
2 of 2
1 of 4
2 of 4
3 of 4
4 of 4
亲自尝试 »

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.