Table of Contents
Media query
Basic usage
Column offset
列排序
列嵌套
复杂应用
显示隐藏
Home Web Front-end Bootstrap Tutorial Let's take a look at the grid system in Bootstrap

Let's take a look at the grid system in Bootstrap

Mar 17, 2021 am 10:07 AM
bootstrap grid system

Let's take a look at the grid system in Bootstrap

Bootstrap provides a responsive, mobile-first fluid grid system. As the screen or viewport size increases, the system It will be automatically divided into up to 12 columns. There is no official definition of the grid system, but according to various descriptions on the Internet, it can be defined as a regular grid array to guide and standardize the layout and information distribution in web pages. This article will introduce in detail the implementation principle of the Bootstrap grid system

The implementation principle of the grid system is very simple, just by defining the container size and dividing it into 12 equal parts (there are also Divide it into 24 parts or 32 parts (but 12 parts is the most common), then adjust the inner and outer margins, and finally combine it with media queries to create a powerful responsive grid system. The grid system in the Bootstrap framework divides the container into 12 equal parts

The grid system is used to create a page layout through a series of combinations of rows and columns, and content can be placed in these Created layout. Here's an introduction to how the Bootstrap grid system works:

1. "Row" must be contained in .container (fixed width) or .container-fluid (100% width) in order to Give appropriate alignment and padding

<div>
    <div></div>
</div>
Copy after login

2. Create a group of "columns" in the horizontal direction through "row"

3. Content should be placed within "column", and only "column" can be a direct child element of row"

 4. Similar to .row and .col-xs-4 This predefined class can be used to quickly create a grid layout. The mixin defined in the Bootstrap source code can also be used to create a semantic layout

<div>
  <div>
    <div>.col-md-4</div>
    <div>.col-md-8</div>
  </div>
</div>
Copy after login

 5. By setting padding for "column" Attribute, thereby creating a gap (gutter) between columns. By setting a negative value margin for the .row element to offset the padding set for the .container element, which is indirectly the "column" contained in the "row" (column)" offsets the padding

6. The columns in the grid system represent the range they span by specifying a value from 1 to 12. For example, three equal-width columns can use three. col-xs-4 to create

<div>
  <div>
    <div>.col-md-4</div>
    <div>.col-md-4</div>
    <div>.col-md-4</div>
  </div>
</div>
Copy after login

7. If the "column" contained in a "row" is greater than 12, the elements where the extra "column" is located will be Arrange as a whole in a new line

8. The grid class is suitable for devices with a screen width greater than or equal to the dividing point size, and the grid class is overridden for small screen devices. Therefore, apply any . The col-md-* raster classes work on devices with screen widths greater than or equal to the breakpoint size, and the raster classes are overridden for small screen devices. Therefore, applying any .col-lg-* on an element does not also affect Large screen devices

Related recommendations: "bootstrap tutorial"

Media query

In the grid system, we are Use the following media query in a Less or Sass file to create key breaking point thresholds.

/* 小屏幕(平板,大于等于 768px) */
@media (min-width: @screen-sm-min) { ... }

/* 中等屏幕(桌面显示器,大于等于 992px) */
@media (min-width: @screen-md-min) { ... }

/* 大屏幕(大桌面显示器,大于等于 1200px) */
@media (min-width: @screen-lg-min) { ... }
Copy after login

We occasionally include max-width in the media query code to limit the impact of CSS to a smaller Within the range of screen sizes

@media (max-width: @screen-xs-max) { ... }
@media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) { ... }
@media (min-width: @screen-md-min) and (max-width: @screen-md-max) { ... }
@media (min-width: @screen-lg-min) { ... }
Copy after login

Use the following table to see in detail how Bootstrap’s grid system works on a variety of screen devices

lg in English is large, which means large; md The English word is middle, which means medium; the English word sm is small, which means small; the English word xs is extra small, which means extra small. It also corresponds to the clothing size

Basic usage

The grid system is used for layout, which is actually a combination of columns

The simple understanding of column combination is to change the number to merge the columns (principle: the total number of columns cannot exceed 12), which is somewhat similar to the colspan attribute of the table

The column combination method is very simple and only involves two CSS Features: Floating and width percentage

.col-md-1, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-md-10, .col-md-11, .col-md-12 {
    float: left;
 }
Copy after login
.col-md-12 {
    width: 100%;
  }
  .col-md-11 {
    width: 91.66666667%;
  }
  .col-md-10 {
    width: 83.33333333%;
  }
  .col-md-9 {
    width: 75%;
  }
  .col-md-8 {
    width: 66.66666667%;
  }
  .col-md-7 {
    width: 58.33333333%;
  }
  .col-md-6 {
    width: 50%;
  }
  .col-md-5 {
    width: 41.66666667%;
  }
  .col-md-4 {
    width: 33.33333333%;
  }
  .col-md-3 {
    width: 25%;
  }
  .col-md-2 {
    width: 16.66666667%;
  }
  .col-md-1 {
    width: 8.33333333%;
  }
Copy after login
<div>
  <div>
    <div>.col-xs-4</div>
    <div>.col-xs-8</div>
  </div>
  <div>
    <div>.col-xs-4</div>
    <div>.col-xs-4</div>
    <div>.col-xs-4</div>
  </div>
  <div>
    <div>.col-xs-3</div>
    <div>.col-xs-6</div>
    <div>.col-xs-3</div>
 </div>
</div>
Copy after login

Lets take a look at the grid system in Bootstrap

【Fluid layout container】

Change the outermost layout element .container to .container-fluid, You can convert a fixed-width grid layout to a 100% width layout

.container-fluid {
    padding-right: 15px;
    padding-left: 15px;
    margin-right: auto;
    margin-left: auto;
}
Copy after login

Column offset

Using column offset is very simple, just add the column element Add the class name "col-md-offset-*" (where the asterisk represents the number of column combinations to be offset), then the column with this class name will be offset to the right. For example, adding "col-md-offset-4" to the column element means that the column is moved to the right by the width of 4 columns

The implementation principle is very simple, using one-twelfth (1/12) margin-left. There are as many margin-left

as there are offsets.
.col-md-offset-12 {
   margin-left: 100%;
  }
  .col-md-offset-11 {
    margin-left: 91.66666667%;
  }
  .col-md-offset-10 {
    margin-left: 83.33333333%;
  }
  .col-md-offset-9 {
    margin-left: 75%;
  }
  .col-md-offset-8 {
    margin-left: 66.66666667%;
  }
  .col-md-offset-7 {
    margin-left: 58.33333333%;
  }
  .col-md-offset-6 {
    margin-left: 50%;
  }
  .col-md-offset-5 {
    margin-left: 41.66666667%;
  }
  .col-md-offset-4 {
    margin-left: 33.33333333%;
  }
  .col-md-offset-3 {
    margin-left: 25%;
  }
  .col-md-offset-2 {
    margin-left: 16.66666667%;
  }
  .col-md-offset-1 {
    margin-left: 8.33333333%;
  }
  .col-md-offset-0 {
    margin-left: 0;
  }
Copy after login

  不过有一个细节需要注意,使用”col-md-offset-*”对列进行向右偏移时,要保证列与偏移列的总数不超过12,不然会致列断行显示

<div>
  <div>
    <div>.col-xs-4</div>
    <div>.col-xs-4 .col-xs-offset-4</div>
  </div>
  <div>
    <div>.col-xs-2</div>
    <div>.col-xs-4 .col-xs-offset-4</div>
    <div>.col-xs-4</div>
  </div>
</div>
Copy after login

Lets take a look at the grid system in Bootstrap

列排序

  列排序其实就是改变列的方向,就是改变左右浮动,并且设置浮动的距离。在Bootstrap框架的网格系统中是通过添加类名“col-md-push-*”和“col-md-pull-*” (其中星号代表移动的列组合数)。

  Bootstrap仅通过设置left和right来实现定位效果

.col-md-pull-12 {
    right: 100%;
  }
  .col-md-pull-11 {
    right: 91.66666667%;
  }
  .col-md-pull-10 {
    right: 83.33333333%;
  }
  .col-md-pull-9 {
    right: 75%;
  }
  .col-md-pull-8 {
    right: 66.66666667%;
  }
  .col-md-pull-7 {
    right: 58.33333333%;
  }
  .col-md-pull-6 {
    right: 50%;
  }
  .col-md-pull-5 {
    right: 41.66666667%;
  }
  .col-md-pull-4 {
    right: 33.33333333%;
  }
  .col-md-pull-3 {
    right: 25%;
  }
  .col-md-pull-2 {
    right: 16.66666667%;
  }
  .col-md-pull-1 {
    right: 8.33333333%;
  }
  .col-md-pull-0 {
    right: 0;
  }

  .col-md-push-12 {
    left: 100%;
  }
  .col-md-push-11 {
    left: 91.66666667%;
  }
  .col-md-push-10 {
    left: 83.33333333%;
  }
  .col-md-push-9 {
    left: 75%;
  }
  .col-md-push-8 {
    left: 66.66666667%;
  }
  .col-md-push-7 {
    left: 58.33333333%;
  }
  .col-md-push-6 {
    left: 50%;
  }
  .col-md-push-5 {
    left: 41.66666667%;
  }
  .col-md-push-4 {
    left: 33.33333333%;
  }
  .col-md-push-3 {
    left: 25%;
  }
  .col-md-push-2 {
    left: 16.66666667%;
  }
  .col-md-push-1 {
    left: 8.33333333%;
  }
  .col-md-push-0 {
    left: 0;
  }
Copy after login
<div>
    <div>
      <div>.col-md-9 .col-md-push-3</div>
      <div>.col-md-3 .col-md-pull-9</div>
    </div>
</div>
Copy after login

Lets take a look at the grid system in Bootstrap

列嵌套

  为了使用内置的栅格系统将内容再次嵌套,可以通过添加一个新的 .row 元素和一系列 .col-sm-* 元素到已经存在的 .col-sm-* 元素内。被嵌套的行(row)所包含的列(column)的个数不能超过12

<div>
    <div>
        <div>
        我的里面嵌套了一个网格
            <div>
            <div>col-xs-6</div>
            <div>col-xs-6</div>
          </div>
        </div>
    <div>col-xs-4</div>
    </div>
    <div>
        <div>.col-xs-4</div>
        <div>
        我的里面嵌套了一个网格
            <div>
              <div>col-xs-4</div>
              <div>col-xs-4</div>
              <div>col-xs-4</div>
            </div>
        </div>
    </div>
</div>
Copy after login

Lets take a look at the grid system in Bootstrap

复杂应用

  在bootstrap中,.col-xs-应用于(

  而.col-xs-、.col-sm-、.col-md-、.col-lg-是可以用混合使用的

  比如,要实现≥992px的时候分四列一排,(≥768px and

nbsp;html>


<meta>
<title>Document</title>
<link>
<style>
.row div{border:1px solid black;}    
</style>


<div>
    <div>
      <div>aaa</div>
      <div>bbb</div>
      <div>ccc</div>
      <div>ddd</div>
    </div>
</div>

Copy after login

显示隐藏

为了加快对移动设备友好的页面开发工作,利用媒体查询功能并使用下面这些工具类可以方便的针对不同设备展示或隐藏页面内容

  形如.visible-*-*的类针对每种屏幕大小都有三种变体,每个针对 CSS 中不同的display属性,如下所示 

  因此,以超小屏幕(xs)为例,可用的 .visible-*-* 类是:.visible-xs-block、.visible-xs-inline 和 .visible-xs-inline-block

  比如,要实现≥992px时存在三列,(≥768px and

nbsp;html>


<meta>
<title>Document</title>
<link>
<style>
.row div{border:1px solid black;}    
</style>


<div>
    <div>
      <div>aaa</div>
      <div>bbb</div>
      <div>ccc</div>
    </div>
</div>

Copy after login

更多编程相关知识,请访问:编程视频!!

The above is the detailed content of Let's take a look at the grid system in Bootstrap. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to get the bootstrap search bar How to get the bootstrap search bar Apr 07, 2025 pm 03:33 PM

How to use Bootstrap to get the value of the search bar: Determines the ID or name of the search bar. Use JavaScript to get DOM elements. Gets the value of the element. Perform the required actions.

How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

How to do vertical centering of bootstrap How to do vertical centering of bootstrap Apr 07, 2025 pm 03:21 PM

Use Bootstrap to implement vertical centering: flexbox method: Use the d-flex, justify-content-center, and align-items-center classes to place elements in the flexbox container. align-items-center class method: For browsers that do not support flexbox, use the align-items-center class, provided that the parent element has a defined height.

How to write split lines on bootstrap How to write split lines on bootstrap Apr 07, 2025 pm 03:12 PM

There are two ways to create a Bootstrap split line: using the tag, which creates a horizontal split line. Use the CSS border property to create custom style split lines.

How to resize bootstrap How to resize bootstrap Apr 07, 2025 pm 03:18 PM

To adjust the size of elements in Bootstrap, you can use the dimension class, which includes: adjusting width: .col-, .w-, .mw-adjust height: .h-, .min-h-, .max-h-

How to insert pictures on bootstrap How to insert pictures on bootstrap Apr 07, 2025 pm 03:30 PM

There are several ways to insert images in Bootstrap: insert images directly, using the HTML img tag. With the Bootstrap image component, you can provide responsive images and more styles. Set the image size, use the img-fluid class to make the image adaptable. Set the border, using the img-bordered class. Set the rounded corners and use the img-rounded class. Set the shadow, use the shadow class. Resize and position the image, using CSS style. Using the background image, use the background-image CSS property.

How to use bootstrap button How to use bootstrap button Apr 07, 2025 pm 03:09 PM

How to use the Bootstrap button? Introduce Bootstrap CSS to create button elements and add Bootstrap button class to add button text

How to set up the framework for bootstrap How to set up the framework for bootstrap Apr 07, 2025 pm 03:27 PM

To set up the Bootstrap framework, you need to follow these steps: 1. Reference the Bootstrap file via CDN; 2. Download and host the file on your own server; 3. Include the Bootstrap file in HTML; 4. Compile Sass/Less as needed; 5. Import a custom file (optional). Once setup is complete, you can use Bootstrap's grid systems, components, and styles to create responsive websites and applications.

See all articles