Table of Contents
移动设备优先策略
Bootstrap 网格系统(Grid System)的工作原理
媒体查询
网格选项
基本的网格结构
Hello, world!
偏移列
嵌套列
列排序
Home Web Front-end HTML Tutorial bootstrap学习笔记bootstrap布局方式_html/css_WEB-ITnose

bootstrap学习笔记bootstrap布局方式_html/css_WEB-ITnose

Jun 21, 2016 am 09:00 AM

Bootstrap 3 是移动设备优先的,在这个意义上,Bootstrap 代码从小屏幕设备(比如移动设备、平板电脑)开始,然后扩展到大屏幕设备(比如笔记本电脑、台式电脑)上的组件和网格。

移动设备优先策略

  • 内容
    • 决定什么是最重要的。
  • 布局
    • 优先设计更小的宽度。
    • 基础的 CSS 是移动设备优先,媒体查询是针对于平板电脑、台式电脑。
  • 渐进增强
    • 随着屏幕大小的增加而添加元素。

响应式网格系统随着屏幕或视口(viewport)尺寸的增加,系统会自动分为最多12列。如下图:

Bootstrap 网格系统(Grid System)的工作原理

网格系统通过一系列包含内容的行和列来创建页面布局。下面列出了 Bootstrap 网格系统是如何工作的:

  • 行必须放置在  .container  class 内,以便获得适当的对齐(alignment)和内边距(padding)。
  • 使用行来创建列的水平组。
  • 内容应该放置在列内,且唯有列可以是行的直接子元素。
  • 预定义的网格类,比如  .row  和  .col-xs-4 ,可用于快速创建网格布局。LESS 混合类可用于更多语义布局。
  • 列通过内边距(padding)来创建列内容之间的间隙。该内边距是通过  .rows  上的外边距(margin)取负,表示第一列和最后一列的行偏移。
  • 网格系统是通过指定您想要横跨的十二个可用的列来创建的。例如,要创建三个相等的列,则使用三个  .col-xs-4

媒体查询

媒体查询是非常别致的"有条件的 CSS 规则"。它只适用于一些基于某些规定条件的 CSS。如果满足那些条件,则应用相应的样式。

Bootstrap 中的媒体查询允许您基于视口大小移动、显示并隐藏内容。下面的媒体查询在 LESS 文件中使用,用来创建 Bootstrap 网格系统中的关键的分界点阈值。

/* 超小设备(手机,小于 768px) *//* Bootstrap 中默认情况下没有媒体查询 *//* 小型设备(平板电脑,768px 起) */@media (min-width: @screen-sm-min) { ... }/* 中型设备(台式电脑,992px 起) */@media (min-width: @screen-md-min) { ... }/* 大型设备(大台式电脑,1200px 起) */@media (min-width: @screen-lg-min) { ... }
Copy after login

我们有时候也会在媒体查询代码中包含 max-width ,从而将 CSS 的影响限制在更小范围的屏幕大小之内。

@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

媒体查询有两个部分,先是一个设备规范,然后是一个大小规则。在上面的案例中,设置了下列的规则:

让我们来看下面这行代码:

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

对于所有带有 min-width: @screen-sm-min 的设备,如果屏幕的宽度小于 @screen-sm-max ,则会进行一些处理。

网格选项

下表总结了 Bootstrap 网格系统如何跨多个设备工作:

超小设备手机(<768px) 小型设备平板电脑(≥768px) 中型设备台式电脑(≥992px) 大型设备台式电脑(≥1200px)
网格行为 一直是水平的 以折叠开始,断点以上是水平的 以折叠开始,断点以上是水平的 以折叠开始,断点以上是水平的
最大容器宽度 None (auto) 750px 970px 1170px
Class 前缀 .col-xs- .col-sm- .col-md- .col-lg-
列数量和 12 12 12 12
最大列宽 Auto 60px 78px 95px
间隙宽度 30px(一个列的每边分别 15px) 30px(一个列的每边分别 15px) 30px(一个列的每边分别 15px) 30px(一个列的每边分别 15px)
可嵌套 Yes Yes Yes Yes
偏移量 Yes Yes Yes Yes
列排序 Yes Yes Yes Yes

基本的网格结构

下面是 Bootstrap 网格的基本结构:

<div class="container">   <div class="row">      <div class="col-*-*"></div>      <div class="col-*-*"></div>         </div>   <div class="row">...</div></div><div class="container">....
Copy after login

下面是个具体代码实例:

<div class="container">        <h1 id="Hello-world">Hello, world!</h1>        <div class="row">            <!--超小设备手机(<768px) -->            <div class="col-xs-1" style="background: #f00">1</div>            <div class="col-xs-1" style="background: #b2b0b0">2</div>            <div class="col-xs-1" style="background: #ff6a00">3</div>            <div class="col-xs-1" style="background: #ffd800">4</div>            <div class="col-xs-1" style="background: #4cff00">5</div>            <div class="col-xs-1" style="background: #0ff">6</div>            <div class="col-xs-1" style="background: #0094ff">7</div>            <div class="col-xs-1" style="background: #b200ff">8</div>            <div class="col-xs-1" style="background: #ff00dc">9</div>            <div class="col-xs-1" style="background: #ff006e">10</div>            <div class="col-xs-1" style="background: #ac5050">11</div>            <div class="col-xs-1" style="background: #54bd4f">12</div>        </div>        <div class="row">            <!--小型设备平板电脑(≥768px) -->            <div class="col-sm-4" style="background: #b2b0b0">1</div>            <div class="col-sm-4" style="background: #ffd800">1</div>            <div class="col-sm-4" style="background: #ac5050">1</div>        </div>        <div class="row">            <!--中型设备台式电脑(≥992px) -->            <div class="col-md-4" style="background: #ac5050">1</div>            <div class="col-md-8" style="background: #54bd4f">1</div>        </div>        <div class="row">            <!--大型设备台式电脑(≥1200px) -->            <div class="col-lg-8" style="background: #ac5050">1</div>            <div class="col-lg-4" style="background: #54bd4f">1</div>        </div>    </div>
Copy after login

偏移列

偏移是一个用于更专业的布局的有用功能。它们可用来给列腾出更多的空间。例如, .col-xs=* 类不支持偏移,但是它们可以简单地通过使用一个空的单元格来实现该效果。

为了在大屏幕显示器上使用偏移,请使用 .col-md-offset-* 类。这些类会把一个列的左外边距(margin)增加 * 列,其中 * 范围是从 111

在下面的实例中,我们有

..
,我们将使用 .col-md-offset-3 class 来居中这个 div。

<div class="container">        <div class="row">            <div class="col-xs-6 col-md-offset-3"                style="background-color: #dedef8;">                <p>               测试偏移列---此处往右偏移了3列                </p>            </div>        </div>        <div class="row">            <div class="col-xs-1" style="background: #f00">1</div>            <div class="col-xs-1" style="background: #b2b0b0">2</div>            <div class="col-xs-1" style="background: #ff6a00">3</div>            <div class="col-xs-1" style="background: #ffd800">4</div>            <div class="col-xs-1" style="background: #4cff00">5</div>            <div class="col-xs-1" style="background: #0ff">6</div>            <div class="col-xs-1" style="background: #0094ff">7</div>            <div class="col-xs-1" style="background: #b200ff">8</div>            <div class="col-xs-1" style="background: #ff00dc">9</div>            <div class="col-xs-1" style="background: #ff006e">10</div>            <div class="col-xs-1" style="background: #ac5050">11</div>            <div class="col-xs-1" style="background: #54bd4f">12</div>        </div>    </div>
Copy after login

显示效果:

嵌套列

为了在内容中嵌套默认的网格,请添加一个新的 .row ,并在一个已有的 .col-md-* 列内添加一组 .col-md-* 列。被嵌套的行应包含一组列,这组列个数不能超过12(其实,没有要求你必须占满12列)。

在下面的实例中,布局有两个列,第二列被分为两行四个盒子。

<div class="container">        <div class="row">            <div class="col-xs-4" style="background: #b2b0b0">第一列</div>            <div class="col-xs-8" style="background: #dedef8">第二列--里面嵌套了四个DIV                <div class="row">                    <div class="col-xs-6" style="background: #0094ff">我是内容一<br /><br /><br /></div>                    <div class="col-xs-6" style="background: #b200ff">我是内容二</div>                </div>                <div class="row">                    <div class="col-xs-6" style="background: #ff00dc">我是内容三<br /><br /><br /></div>                    <div class="col-xs-6" style="background: #ff006e">我是内容四</div>                </div>            </div>        </div>    </div>
Copy after login

显示效果:

列排序

Bootstrap 网格系统另一个完美的特性,就是您可以很容易地以一种顺序编写列,然后以另一种顺序显示列。

您可以很轻易地改变带有 .col-md-push-*.col-md-pull-* 类的内置网格列的顺序,其中 * 范围是从 111

在下面的实例中,我们有两列布局,左列很窄,作为侧边栏。我们将使用 .col-md-push-*.col-md-pull-* 类来互换这两列的顺序。

<div class="container">        <div class="row">            <div class="col-xs-4 col-md-push-8" style="background: #ff00dc">左边</div>            <div class="col-xs-8 col-md-pull-4" style="background: #ff006e">右边</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
4 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 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.

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

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.

Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Gitee Pages static website deployment failed: How to troubleshoot and resolve single file 404 errors? Apr 04, 2025 pm 11:54 PM

GiteePages static website deployment failed: 404 error troubleshooting and resolution when using Gitee...

See all articles