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

Bootstrap Responsive Design

Bootstrap Responsive Design

Introduction

This tutorial explains how to apply responsive design in web page layouts. In this course, you will learn about responsive Web design. With the popularity of mobile devices, how to let users get a good visual experience when browsing your site on mobile devices has become an unavoidable issue. Responsive Web design is an effective way to achieve this.

What Is Responsive Web Design

Responsive Web design is a method that lets users get a good visual experience when browsing a website on devices of various sizes. For example, you browse a website on a computer monitor and then on a smartphone. The smartphone's screen is much smaller than the computer monitor, yet you feel no difference; the user experience of the two is almost the same, which means the site has done a good job with responsive design.

We have appliedfluid layout examplein ourresponsive features, and ask you to view it at different screen sizes. You can Chrome or FireFox resize the browser with a window-resizing extension.

Click here to view the Bootstrap responsive design example。

How Responsive Web Design Works

To apply responsive Web design, you need to create a CSS containing styles that adapt to various device sizes. Once the page is loaded on a specific device, various fonts and Web development technologies such as Media Queries are used on the page; at this point, the device's viewport size is detected first, and then device-specific styles are loaded.

A Closer Look at the CSS of Responsive Web Design

We will learn how 'responsive design' achieves subtle differences by studying 'bootstrap-responsive.css'. Before that, you must add the following line of code to the head section of your web page:



The viewport meta tag overrides the default viewport and helps load styles related to the specific viewport.

width The attribute sets the screen width. It contains a value such as 320, meaning 320 pixels, or the value 'device-width', which tells the browser to use the original resolution.

initial-scale The attribute is the initial scale of the viewport. When set to 1.0, the device's original width is presented.

Of course, you must add Bootstrap's responsive CSS, as follows:



Now, if you look at the responsive CSS file, you will find that after some common declarations (from lines 10 to 22), there are various sections beginning with '@media' This is how styles for various devices are written.

The first section begins with '@media (max-width: 480px)' to set styles for devices with a maximum width of 480 pixels.

The second section begins with '@media (max-width: 767px)' to set styles for devices with a maximum width of 767 pixels.

The third section begins with '@media (min-width: 768px) and (max-width: 979px)' to set styles for devices with a minimum width of 768 pixels and a maximum width of 979 pixels.

The next section sets styles for devices with a maximum width of 979 pixels. So it begins with@media (max-width: 979px)'.

The last two sections begin with '@media (min-width: 980px)' and '@media (min-width: 1200px)' respectively; the former sets styles for devices with a minimum width of 980 pixels, and the latter for devices with a minimum width of 1200 pixels.

So, the basic role of such a style sheet is, by using the 'min-width' and 'max-width' attributes, to determine which styles to use based on the device's maximum and minimum width.

Explanation

To make layouts more responsive, Bootstrap does three things:

  1. It modifies the width of the columns in the grid.

  2. Whenever needed, it uses stacked elements instead of floated elements. If you are not clear about what stacked elements are, the following form from w3.org may help:

The root element (html) forms the root of the stacking context, and other stacking contexts are generated by any positioned element (including relatively positioned elements with a computed value of 'z-index' other than 'auto'). A stacking context is not necessarily related to a containing block.

3. To properly render headings and text at their sizes.

Faster Development of Mobile-Friendly Layouts

Bootstrap has several useful classes for developing mobile-friendly layouts. These classes can be seen in 'responsive.less'.

.visible-phone, visible on phones with a width of 767px or less, hidden on tablets from 979px to 768px, and hidden on desktops by default.

.visible-tablet, hidden on phones with a width of 767px or less, visible on tablets from 979px to 768px, and hidden on desktops by default.

.visible-desktop, hidden on phones with a width of 767px or less, hidden on tablets from 979px to 768px, and visible on desktops by default.

.hidden-phone, hidden on phones with a width of 767px or less, visible on tablets from 979px to 768px, and visible on desktops by default.

.hidden-tablet, visible on phones with a width of 767px or less, hidden on tablets from 979px to 768px, and visible on desktops by default.

.hidden-desktop, visible on phones with a width of 767px or less, visible on tablets from 979px to 768px, and hidden on desktops by default.

Click here to download all the HTML, CSS, JS, and image files used in this tutorial.