Bootstrap CSS Overview
In this chapter, we will explain the key parts of Bootstrap's underlying structure, including the best practices that make web development better, faster, and more robust.
HTML 5 Doctype
Bootstrap uses some HTML5 elements and CSS properties. For these to work properly, you need to use the HTML5 doctype. Therefore, include the following code snippet at the beginning of your project when using Bootstrap.
....
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
Mobile first is the most significant change in Bootstrap 3.
In previous Bootstrap versions (up to 2.x), you needed to manually include another CSS to make the whole 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 Bootstrap-developed sites mobile-friendly and ensure proper rendering and touch zooming, add the viewport meta tag in the head of the 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 ensures 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 browsers, by adding viewport meta to the user-scalable=no tag, you can disable its zooming functionality.
Typically,maximum-scale=1.0 is used together with user-scalable=no. With zooming disabled this way, users can only scroll the screen, making your site look and 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 class, images in Bootstrap 3 can be made more responsive-friendly.
Next, let's take a look at which CSS properties this class contains.
In the following code, you can see that theimg-responsive class gives images the properties max-width: 100%; and height: auto;, allowing images to scale proportionally without exceeding their parent element's size.
.img-responsive {
display: block;
height: auto;
max-width: 100%;
}
This indicates that the image is displayed as block. When you set an element's display property to block, it is displayed as a block-level element.
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.
If you need to horizontally center an image using the .img-responsive class, use the .center-block class, not .text-center.
Global display, typography, and links
Basic global display
Bootstrap 3 uses body {margin: 0;} to remove the body margin.
See the following settings for 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.
Typography
Use the @font-family-base, @font-size-base, and @line-height-base properties as the typographic styles.
Link styles
Set the global link color via the @link-color property.
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 uses Normalize to establish cross-browser consistency.
Normalize.css is a small CSS file that provides better cross-browser consistency in the default styling of HTML elements.
Container
...
Bootstrap 3's container class is used to wrap the content on the page. Let's take a look at this in the bootstrap.css file .container class。
.container {
padding-right: 15px;
padding-left: 15px;
margin-right: auto;
margin-left: auto;
}
Through the code above, the left and right margins of container (margin-right, margin-left) are left to the browser to decide.
Note that because the padding is a fixed width, containers are not nestable by default.
.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.
@media (min-width: 768px) {
.container {
width: 750px;
}
Bootstrap Browser/Device Support
Bootstrap works well in the latest desktop and mobile browsers.
Older browsers may not be well supported.
The following table shows the latest versions of browsers and platforms supported by Bootstrap:
| Chrome | Firefox | IE | Opera | Safari | |
|---|---|---|---|---|---|
| Android | YES | YES | Not applicable | Not applicable | Not applicable |
| iOS | YES | Not applicable | Not applicable | Not applicable | YES |
| Mac OS X | YES | YES | Not applicable | YES | YES |
| Windows | YES | YES | YES* | YES | Not applicable |
- Bootstrap supports Internet Explorer 8 and later versions of IE.