Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Bootstrap 3 CSS Overview

Bootstrap 3 CSS Overview

Goals

In this tutorial, we have discussed the key points of Bootstrap 3 CSS. Next we will explain several key factors of how Bootstrap 3 works.

HTML 5 Doctype

Bootstrap 3 uses some HTML5 elements and CSS properties. For these to work properly, you need to use the HTML5 document type (Doctype).







  ...



If you do not use the HTML5 doctype at the beginning of a web page created with Bootstrap, you may face some browser display inconsistencies, and even inconsistencies in certain situations, to the extent that your code cannot pass W3C standard validation.

Mobile first

This is probably the most significant change in Bootstrap 3. In previous versions of Bootstrap (up to 2.x), you needed to manually include another CSS to work together with the main CSS to make your project mobile-friendly. Now things are different: Bootstrap 3's default CSS is itself mobile-friendly.

Bootstrap 3 is designed with mobile devices first and desktop devices second. This is actually a very timely shift, because more and more users are now using mobile devices.

To make websites developed with Bootstrap mobile-friendly and ensure proper rendering and touch zooming, you need to add the viewport meta tag in the head of the web page, as shown below:





width attribute controls the width of the device. Assuming your site will be viewed on devices with different screen resolutions, setting it to device-width will ensure that it renders correctly on different devices.

initial-scale=1.0 Ensure that the page is rendered at a 1:1 scale when loaded, with no zooming.

On mobile device browsers, by adding to the viewport meta tag user-scalable=no you can disable its zooming function. Usually,maximum-scale=1.0 is used together with user-scalable=no. With zooming disabled this way, users can only scroll the screen, which makes your website feel more like a native app. Note that we do not recommend this approach for all websites; it depends on your own situation!

Responsive Images

By adding the img-responsive The class makes images in Bootstrap 3 more friendly to responsive layouts. Let's see which CSS properties this class contains. In essence, it assigns the max-width: 100%; and height: auto; properties to images, allowing images to scale proportionally without exceeding the size of their parent element.



.img-responsive {

  display: inline-block;

  height: auto;

  max-width: 100%;

}

This indicates that the image is displayed as inline-blockWhen you set an element's display property to inline-block, the element is displayed inline relative to the surrounding content, but unlike inline, in this case we can set the width and height.

Settings height:auto, the height of the element depends on the browser.

Settings max-width set to 100% overrides any width specified via the width attribute. This makes images more responsive-friendly.

Global display, typography, and links

Bootstrap 3 uses body {margin: 0;} to remove the body margin.

See the following settings for the body



body {

  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;

  font-size: 14px;

  line-height: 1.428571429;

  color: #333333;

  background-color: #ffffff;

}

The first rule sets the body's default font family to "Helvetica Neue", Helvetica, Arial, sans-serif。

The second rule sets the default font size of text to 14 pixels.

The third rule sets the default line height to 1.428571429.

The fourth rule sets the default text color to #333333.

The last rule sets the default background color to white.

The default styles for links are set as follows



a:hover,

a:focus {

  color: #2a6496;

  text-decoration: underline;

}



a:focus {

  outline: thin dotted #333;

  outline: 5px auto -webkit-focus-ring-color;

  outline-offset: -2px;

}

Therefore, when the mouse hovers over a link, or for a link that has been clicked, the color will be set to #2a6496. At the same time, an underline will appear.

In addition, clicked links display a thin dashed outline with a color code of #333. Another rule sets the outline to 5 pixels wide, and for webkit-based browsers there is a -webkit-focus-ring-color browser extension. The outline offset is set to -2 pixels.

All of these styles can be found in scaffolding.less.

Avoiding cross-browser inconsistencies

Bootstrap 3, like its previous versions, uses Normalize to establish cross-browser consistency.

Container

Bootstrap 3's container The class is used to wrap the content on the page. Let's take a look at the following class



.container {

  margin-right: auto;

  margin-left: auto;

}

With the code above, the left and right margins of the container are left for the browser to decide.



.container:before,

.container:after {

  display: table;

  content: " ";

}

This creates pseudo-elements. Setting display is table, creates an anonymous table-cell and a new block formatting context.:before The pseudo-element prevents top margin collapse,:after the pseudo-element clears floats.

If both the conteneditable When the property appears in HTML, due to some Opera bugs, a space is created around the above element. This can be fixed by using content: " " to fix it.



.container:after {

  clear: both;

}

It creates a pseudo-element and ensures that all containers contain all floated elements.

Bootstrap 3 CSS has a responsive media query that sets a max-width for container at different media query threshold ranges to match the grid system.