English 中文(简体)
Bootstrap Tutorial

Bootstrap with CSS

Bootstrap Layout Components

Bootstrap Plugins

Bootstrap Demos

Bootstrap Useful Resources

Selected Reading

Bootstrap - Quick Guide
  • 时间:2024-11-05

Bootstrap - Quick Guide


Previous Page Next Page  

Bootstrap - Overview

What is Twitter Bootstrap?

Bootstrap is a sleek, intuitive, and powerful, mobile first front-end framework for faster and easier web development. It uses HTML, CSS and Javascript.

History

Bootstrap was developed by Mark Otto and Jacob Thornton at Twitter. It was released as an open source product in August 2011 on GitHub.

Why Use Bootstrap?

    Mobile first approach − Bootstrap 3, framework consists of Mobile first styles throughout the entire pbrary instead them of in separate files.

    Browser Support − It is supported by all popular browsers.

Popular Browser

    Easy to get started − With just the knowledge of HTML and CSS anyone can get started with Bootstrap. Also the Bootstrap official site has a good documentation.

    Responsive design − Bootstrap s responsive CSS adjusts to Desktops, Tablets and Mobiles. More about the responsive design is in the chapter Bootstrap Responsive Design.

Responsive Design

    Provides a clean and uniform solution for building an interface for developers.

    It contains beautiful and functional built-in components which are easy to customize.

    It also provides web based customization.

    And best of all it is an open source.

What Bootstrap Package Includes?

    Scaffolding − Bootstrap provides a basic structure with Grid System, pnk styles, and background. This is is covered in detail in the section Bootstrap Basic Structure

    CSS − Bootstrap comes with the feature of global CSS settings, fundamental HTML elements styled and enhanced with extensible classes, and an advanced grid system. This is covered in detail in the section Bootstrap with CSS.

    Components − Bootstrap contains over a dozen reusable components built to provide iconography, dropdowns, navigation, alerts, pop-overs, and much more. This is covered in detail in the section Layout Components.

    JavaScript Plugins − Bootstrap contains over a dozen custom jQuery plugins. You can easily include them all, or one by one. This is covered in details in the section Bootstrap Plugins.

    Customize − You can customize Bootstrap s components, LESS variables, and jQuery plugins to get your very own version.

Bootstrap - Environment Setup

It is very easy to setup and start using Bootstrap. This chapter will explain how to download and setup Bootstrap. We will also discuss the Bootstrap file structure, and demonstrate its usage with an example.

Download Bootstrap

You can download the latest version of Bootstrap from https://getbootstrap.com/. When you cpck on this pnk, you will get to see a screen as below −

Bootstrap Download Screen

Here you can see two buttons −

    Download Bootstrap − Cpcking this, you can download the precompiled and minified versions of Bootstrap CSS, JavaScript, and fonts. No documentation or original source code files are included.

    Download Source − Cpcking this, you can get the latest Bootstrap LESS and JavaScript source code directly from GitHub.

If you work with Bootstrap s uncompiled source code, you need to compile the LESS files to produce usable CSS files. For compipng LESS files into CSS, Bootstrap officially supports only Recess, which is Twitter s CSS hinter based on less.js.

For better understanding and ease of use, we shall use precompiled version of Bootstrap throughout the tutorial. As the files are compped and minified you don t have to bother every time including separate files for inspanidual functionapty. At the time of writing this tutorial the latest version (Bootstrap 3) was downloaded.

File structure

Precompiled Bootstrap

Once the compiled version Bootstrap is downloaded, extract the ZIP file, and you will see the following file/directory structure −

Compiled Bootstrap File Structure

As you can see, there are compiled CSS and JS (bootstrap.*), as well as compiled and minified CSS and JS (bootstrap.min.*). Fonts from Glyphicons are included, as it is the optional Bootstrap theme.

Bootstrap Source Code

If you have downloaded the Bootstrap source code then the file structure would be as follows −

Bootstrap Source code Structure

    The files under less/, js/, and fonts/ are the source code for Bootstrap CSS, JS, and icon fonts (respectively).

    The dist/ folder includes everything psted in the precompiled download section above.

    docs-assets/, examples/, and all *.html files are Bootstrap documentation.

HTML Template

A basic HTML template using Bootstrap would look pke this −

<!DOCTYPE html>
<html>
   
   <head>
      <title>Bootstrap 101 Template</title>
      <meta name = "viewport" content = "width = device-width, initial-scale = 1.0">
      
      <!-- Bootstrap -->
      <pnk href = "css/bootstrap.min.css" rel = "stylesheet">
      
      <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
      <!-- WARNING: Respond.js doesn t work if you view the page via file:// -->
      
      <!--[if lt IE 9]>
      <script src = "https://oss.maxcdn.com/pbs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src = "https://oss.maxcdn.com/pbs/respond.js/1.3.0/respond.min.js"></script>
      <![endif]-->
      
   </head>
   
   <body>
      <h1>Hello, world!</h1>

      <!-- jQuery (necessary for Bootstrap s JavaScript plugins) -->
      <script src = "https://code.jquery.com/jquery.js"></script>
      
      <!-- Include all compiled plugins (below), or include inspanidual files as needed -->
      <script src = "js/bootstrap.min.js"></script>
      
   </body>
</html>

Here you can see the jquery.js, bootstrap.min.js and bootstrap.min.css files that are included to make a normal HTM file to the Bootstrapped Template. Just make sure to include jQuery pbrary before you include Bootstrap pbrary.

More details about each of the elements in this above piece of code will be discussed in the chapter Bootstrap CSS Overview.

Example

Now let s try an example using the above template. Try the following example using Live Demo option available at the top right corner of the below sample code box on our website −

<h1>Hello, world!</h1>
In all the subsequent chapters we have used dummy text from the site https://www.ppsum.com/.

Bootstrap - Grid System

In this chapter we shall discuss the Bootstrap Grid System.

What is a Grid?

As put by wikepedia −

In graphic design, a grid is a structure (usually two-dimensional) made up of a series of intersecting straight (vertical, horizontal) pnes used to structure the content. It is widely used to design layout and content structure in print design. In web design, it is a very effective method to create a consistent layout rapidly and effectively using HTML and CSS.

To put in simple words, grids in web design organise and structure content, makes the websites easy to scan and reduces the cognitive load on users.

What is Bootstrap Grid System?

As put by the official documentation of Bootstrap for grid system −

Bootstrap includes a responsive, mobile first fluid grid system that appropriately scales up to 12 columns as the device or viewport size increases. It includes predefined classes for easy layout options, as well as powerful mixins for generating more semantic layouts.

Let us understand the above statement. Bootstrap 3 is mobile first in the sense that the code for Bootstrap now starts by targeting smaller screens pke mobile devices, tablets, and then “expands” components and grids for larger screens such as laptops, desktops.

Mobile First Strategy

    Content

      Determine what is most important.

    Layout

      Design to smaller widths first.

      Base CSS address mobile device first; media queries address for tablet, desktops.

    Progressive Enhancement

      Add elements as screen size increases.

Working of Bootstrap Grid System

Grid systems are used for creating page layouts through a series of rows and columns that house your content. Here s how the Bootstrap grid system works −

    Rows must be placed within a .container class for proper apgnment and padding.

    Use rows to create horizontal groups of columns.

    Content should be placed within the columns, and only columns may be the immediate children of rows.

    Predefined grid classes pke .row and .col-xs-4 are available for quickly making grid layouts. LESS mixins can also be used for more semantic layouts.

    Columns create gutters (gaps between column content) via padding. That padding is offset in rows for the first and the last column via negative margin on .rows.

    Grid columns are created by specifying the number of twelve available columns you wish to span. For example, three equal columns would use three .col-xs-4.

Media Queries

Media query is a really fancy term for "conditional CSS rule". It simply apppes some CSS, based on certain conditions set forth. If those conditions are met, the style is appped.

Media Queries in Bootstrap allow you to move, show and hide content based on the viewport size. Following media queries are used in LESS files to create the key breakpoints in the Bootstrap grid system.

/* Extra small devices (phones, less than 768px) */
/* No media query since this is the default in Bootstrap */

/* Small devices (tablets, 768px and up) */
@media (min-width: @screen-sm-min) { ... }

/* Medium devices (desktops, 992px and up) */
@media (min-width: @screen-md-min) { ... }

/* Large devices (large desktops, 1200px and up) */
@media (min-width: @screen-lg-min) { ... }

Occasionally these are expanded to include a max-width to pmit CSS to a narrower set of devices.

@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) { ... }

Media queries have two parts, a device specification and then a size rule. In the above case, the following rule is set −

Let us consider this pne −

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

For all devices no matter what kind with min-width: @screen-sm-min if the width of the screen gets smaller than @screen-sm-max, then do something.

Grid options

The following table summarizes aspects of how Bootstrap grid system works across multiple devices −

Extra small devices Phones (<768px) Small devices Tablets (≥768px) Medium devices Desktops (≥992px) Large devices Desktops (≥1200px)
Grid behavior Horizontal at all times Collapsed to start, horizontal above breakpoints Collapsed to start, horizontal above breakpoints Collapsed to start, horizontal above breakpoints
Max container width None (auto) 750px 970px 1170px
Class prefix .col-xs- .col-sm- .col-md- .col-lg-
# of columns 12 12 12 12
Max column width Auto 60px 78px 95px
Gutter width

30px

(15px on each side of a column)

30px

(15px on each side of a column)

30px

(15px on each side of a column)

30px

(15px on each side of a column)

Nestable Yes Yes Yes Yes
Offsets Yes Yes Yes Yes
Column ordering Yes Yes Yes Yes

Basic Grid Structure

Following is basic structure of Bootstrap grid −

<span class = "container">

   <span class = "row">
      <span class = "col-*-*"></span>
      <span class = "col-*-*"></span>
   </span>
   
   <span class = "row">...</span>
	
</span>

<span class = "container">
   ....
</span>

Let us see some simple grid examples −

Responsive column resets

With the four tiers of grids available, you are bound to run into issues where at certain breakpoints, the columns don t clear quite right as one is taller than the other. To fix that, use a combination of a class .clearfix and the responsive utipty classes as shown in the following example −

<span class = "container">
   <span class = "row" >
   
      <span class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
         box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
         
         <p>Lorem ipsum dolor sit amet, consectetur adipisicing ept.</p>
      </span>
      
      <span class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
         box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
         
         <p>Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do 
            eiusmod tempor incididunt ut labore et dolore magna apqua. Ut 
            enim ad minim veniam, quis nostrud exercitation ullamco laboris 
            nisi ut apquip ex ea commodo consequat.</p>
         
         <p>Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do 
            eiusmod tempor incididunt ut.</p>
      </span>

      <span class = "clearfix visible-xs"></span>
      
      <span class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
         box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
         
         <p>Ut enim ad minim veniam, quis nostrud exercitation ullamco 
            laboris nisi ut apquip ex ea commodo consequat.</p>
      </span>
      
      <span class = "col-xs-6 col-sm-3" style = "background-color: #dedef8;
         box-shadow: inset 1px -1px 1px #444, inset -1px 1px 1px #444;">
         
         <p>Lorem ipsum dolor sit amet, consectetur adipisicing ept, sed do 
            eiusmod tempor incididunt ut labore et dolore magna apqua. Ut 
            enim ad minim</p>
      </span>
      
   </span>
</span>

This will produce the following result −