Home Web Front-end HTML Tutorial 详解Bootstrap网格系统_html/css_WEB-ITnose

详解Bootstrap网格系统_html/css_WEB-ITnose

Jun 24, 2016 am 11:20 AM

bootstrap框架中的网格系统就是将容器平分成12份,在使用的时候可以根据实际情况重新编译LESS/SASS源码来修改12这个数值。bootstrap框架的网格系统工作原理:

1、数据行(.row)必须包含在容器(.container)中,以便其赋予合适的对齐方式和内距(padding)

<div class="container"><div class="row"></div></div>
Copy after login

2、在行(.row)中可以添加列(.column),但列数之和不能超过平分的总列数(如:12)

<div class="container"><div class="row">  <div class="col-md-4"></div>  <div class="col-md-8"></div></div></div>
Copy after login

3、具体内容应当放在列容器(.column)之内,而且只有列(.column)才可以作为行容器(.row)的直接子元素

4、通过设置内距(padding)从而创建列与列之间的间距,然后通过为第一列和最后一叠设置负值的外距(margin)来抵消内距(padding)的影响

在bootstrap网格系统中带有响应式效果,其带有四种类型的浏览器,(超小屏,小屏,中屏和大屏),其断点是768px,992px,1220px

容器(.container),针对不同的浏览器分辨率,其宽度也不一样:自动,760px,970px,1170px;

.container {  padding-right: 15px;  padding-left: 15px;  margin-right: auto;  margin-left: auto;  @media (min-width: 768px) {  .container {    width: 750px;  }  @media (min-width: 992px) {  .container {    width: 970px;  }  @media (min-width: 1200px) {  .container {    width: 1170px;  }
Copy after login

行容器(.row),将容器的行平分了12等份,也就是列。每个列都有个padding-left:15px和padding-right:15px;这样也导致了第一列的padding-left和最后一列的paading-right占据了中宽度的30px

.col-xs-1, .col-sm-1, .col-md-1, .col-lg-1, .col-xs-2, .col-sm-2, .col-md-2, .col-lg-2, .col-xs-3, .col-sm-3, .col-md-3, .col-lg-3, .col-xs-4, .col-sm-4, .col-md-4, .col-lg-4, .col-xs-5, .col-sm-5, .col-md-5, .col-lg-5, .col-xs-6, .col-sm-6, .col-md-6, .col-lg-6, .col-xs-7, .col-sm-7, .col-md-7, .col-lg-7, .col-xs-8, .col-sm-8, .col-md-8, .col-lg-8, .col-xs-9, .col-sm-9, .col-md-9, .col-lg-9, .col-xs-10, .col-sm-10, .col-md-10, .col-lg-10, .col-xs-11, .col-sm-11, .col-md-11, .col-lg-11, .col-xs-12, .col-sm-12, .col-md-12, .col-lg-12 {  position: relative;  min-height: 1px;  padding-right: 15px;  padding-left: 15px;}
Copy after login

行容器(.row)定义了margin-left和margin-right值为-15px,用来抵消第一列的左内距和最后一列的右内距,这样第一列和最后一列与容器(.container)之间就没有间距了

.row {  margin-right: -15px;  margin-left: -15px;}
Copy after login

基本用法

由于bootstrap框架在不同屏幕尺寸使用了不同的网格样式,下面就以中屏(970px)为例。

1、列组合

列组合就是更改数字来合并列(列总数不能超过12),有点类似于表格的colspan属性;列组合方式只涉及两个特性:浮动于宽度百分比

<div class="container">           <div class="row">               <div class="col-md-4">col-md-4</div>               <div class="col-md-8">col-md-8</div>           </div>           <div class="row">               <div class="col-md-4">col-md-4</div>               <div class="col-md-4">col-md-4</div>               <div class="col-md-4">col-md-4</div>           </div>           <div class="row">               <div class="col-md-3">col-md-3</div>               <div class="col-md-6">col-md-6</div>               <div class="col-md-3">col-md-3</div>           </div>       </div>
Copy after login

效果如下:

确保所有列左浮动

.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

列偏移

有时候,我们不希望相邻的两个列紧靠在一起,但又不想用margin或者其他技术手段,这是可以用列偏移(offset)来实现。使用列偏移只需在列元素上添加类名.col-md-offset-*(星号代表要偏移的列组合数),具有这个类名的列就会偏移,如:在列元素上添加.col-md-offset-4,表示该列向右偏移4个列的宽度

<div class="container">           <div class="row">               <div class="col-md-4">1111</div>               <div class="col-md-4 col-md-offset-2">111</div>               <div class="col-md-2">333</div>           </div>           <div class="row">               <div class="col-md-4 col-md-offset-4">列偏移</div>               <div class="col-md-2">col-md-2</div>               <div class="col-md-2">col-md-2</div>           </div>       </div>
Copy after login

效果如下:

实现原理:

利用十二分之一的margin-left,有多少个offset,就有多少个margin-left

.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,不然会导致列断行显示

列排序

列排序就是改变列的方向,并且设置浮动的距离。在bootstrap网格系统中是通过添加类名。col-md-push-*和col-md-pull-*

<div class="container">       <div class="row">          <div class="col-md-4">col-md-4</div>          <div class="col-md-8">col-md-8</div>       </div>   </div>
Copy after login

效果如下:

col-md-4居左,col-md-8居右,如果要互换位置,就需要将col-md-4向右移动8个列的距离,也就是添加类名.col-md-push-8;同时需要将col-md-8向左移动4个列的距离,也就是添加类名.col-md-pull-4

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

列嵌套

列嵌套可以在一个列中添加一个或做个行(row)容器,然后在这个行容器中插入列,在列容器中的行容器(row),宽度为100%时,就是当前外部列的宽度

<div class="container">       <div class="row">           <div class="col-md-8">               我在里面嵌套了一个网格               <div class="row">                   <div class="col-md-6">col-md-6</div>                   <div class="col-md-6">col-md-6</div>               </div>           </div>           <div class="col-md-4">col-md-4</div>       </div>       <div class="row">           <div class="col-md-4">col-md-4</div>           <div class="col-md-8">               我在里面嵌套了一个网格               <div class="row">                   <div class="col-md-4">col-md-4</div>                   <div class="col-md-4">col-md-4</div>                   <div class="col-md-4">col-md-4</div>               </div>           </div>       </div>   </div>
Copy after login

 

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

See all articles