What occurs when grid classes are applied to devices with a reduced breakpoint size?

Grid classes are responsive and apply to devices with screen widths equal to or greater than specified breakpoint sizes. They override grid classes targeted at smaller devices. This means that applying a .col-md- class to an element will affect its styling on medium and large devices if a .col-lg- class is not present.

For example:

<div class="col-lg-4">div 1</div>
 <div class="col-lg-4">div 1</div>
 <div class="col-lg-4">div 1</div>

This code will create three columns in a row, which will then stack into vertical blocks when the screen size is reduced.

My question is: Does Bootstrap do something behind the scenes to make this happen, or does it just naturally occur?

Any suggestions for using grid classes effectively? Keep in mind that col-xs-* can also be applied to larger screens. In real projects, what approach do you take?

Thanks.

Answer №1

To properly address this inquiry, one must first grasp how Twitter Bootstrap's grid system operates.

How Does Twitter Bootstrap Identify Devices?

Media queries

We utilize specific media queries in our Less files to establish the key breakpoints within our grid system.

/* Extra small devices (phones, less than 768px) */
/* No media query is needed as this is Bootstrap's default setting */

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

By using min-width, the subsequent code block will be implemented on all screen widths exceeding the breakpoint value.

For instance, CSS rules for medium devices will apply to devices with screen widths equal to or greater than 992px. Within this range, they will override any rules designated for small devices (in compliance with the order of col-sm-* col-md-*).

Regarding extra small devices, CSS rules are not specified by media queries but are typically included in the stylesheet and applied to elements regardless of the device's screen width, unless overridden by a @media query.

Using the following structure (Example here):

<div class="row">
    <div class="col-md-8">.col-md-8</div>
    <div class="col-xs-6 col-md-4">.col-xs-6 .col-md-4</div>
</div>

The resulting CSS would be:

  • On extra small devices
    • First div: Nothing; it expands to fill its parent's width as a block-level element.
    • Second div: .col-xs-6 { width: 50%; }

  • On small devices
    • First div: Nothing; it fills the entire width of its parent.
    • Second div: .col-xs-6 { width: 50%; }

  • On medium devices
    • First div:
      @media (min-width: 992px) .col-md-8 { width: 66.66666667%; }
    • Second div:
      @media (min-width: 992px) .col-md-4 { width: 33.33333333%; }


  • On large devices
    • First div:
      @media (min-width: 992px) .col-md-8 { width: 66.66666667%; }
    • Second div:
      @media (min-width: 992px) .col-md-4 { width: 33.33333333%; }

It can be observed that the first <div> behaves as a normal block-level element on Extra Small and Small devices, while on Medium devices, it responds to the media query.

Hence, reducing the window size below 992px will cause the first <div> to occupy the full horizontal space within its parent element.

Does Bootstrap intervene to facilitate this process(1), or does it remain unaltered(2)?

- (2) is accurate: Bootstrap leaves it unchanged.

When/Where Should Grid Classes Be Utilized?

The choice is ultimately yours. However, just because

col-xs-* can also be applied to large screens
, doesn't mean it should or must be used.

If you aim to arrange columns into vertical blocks on extra small devices, begin with col-sm-* grid classes. To achieve a similar effect on small devices, opt for col-md-* or col-lg-* classes.

Additionally, employing col-lg-* will supersede col-md-* on large devices. Ensure the class order follows this sequence:

<div class="col-xs-* col-sm-* col-md-* col-lg-*">

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The Tumblr HTML code is not valid: There is a stray start tag for `html

Occasionally, I check my tumblog for validation in order to fix any errors. While most issues are easy to explain, there is one that has me stumped. What exactly does this mean? (please note: the outputted markup is out of my control) Error: Error: Stra ...

The html-docx-js package generates incomprehensible text

Has anyone successfully utilized html-docx-js recently? I attempted the following code snippet: var newHTML = "<!DOCTYPE html><html><head lang='en'><meta charset='UTF-8'><title>Report</title& ...

Having trouble with the login form that utilizes PHP and MySQL?

After spending a few hours on it, I am struggling to make this code work. Essentially, I have a bookings page where users enter their details. I set the account password as the phone number for simplicity. After booking, their details are added to a table ...

What is the solution for adjusting the positioning of the `<select>` element in Bootstrap?

Here is a snippet of HTML code: <button class="btn">hello</button> <select class="input-small"><option>1</option></select> <button class="btn">world</button> The position of the select element is not correc ...

Effortlessly create a seamless transition in background color opacity once the base image has finished

I've set up a div with a sleek black background. Upon page load, I trigger an API request for an image, which is then displayed in a secondary div positioned behind the main one. After this, I aim to smoothly transition the overlaying div's opaci ...

Sticky element on the left side vanishes while scrolling towards the right

My goal is to design a webpage with five distinct regions, out of which three are meant to be sticky. However, while implementing this feature, I encountered an issue - when the user scrolls to the right beyond the viewport width, the left sticky elements ...

Incorporating descriptions below the images within this JavaScript carousel

I'm struggling to add text under the images in this slider. The functionality is simple, but I can't figure it out. I might have to rewrite it. Here's the HTML and JS: <div id="look-book-scroll"> <a href="javascript:;" id="lookbo ...

Issue with min-height not being recognized in Page Wrap

The code snippet provided is as follows: html ,body { height: 100%; font-style: Helvetica; } .page_background, .page { margin: 0 auto; } .page_background { width: 100%; min-height: 100%; background: -webkit-linear-gradient(#282828, #88 ...

Can you explain the significance of CSS properties in aligning a form element at the center?

After looking into how to center a form element using CSS, I discovered that setting the margin to auto and specifying a width property are necessary steps. But, what exactly does the width represent in this context? For instance, when I set the width to ...

Ensure that a link housed within a div does not possess dual hover effects

I'm struggling to achieve the desired hover state because there are two states triggered due to the link being inside the div button! Intended Unhovered State - Unexpected Hovered State - Desired Hovered State - HTML - <li> <div id= ...

Validating Angular Forms within a NgFor loop

Consider the scenario where there is a form implemented as shown below: <form #form="ngForm"> <div *ngFor="let action of member_plan.actions"> <div class="container"> <h4 class="text-sm-center black mb-3">{{ ...

What steps should be followed to direct the user to a different webpage after changing the index page name?

I'm in the process of creating a new website. The previous name of my website was home.html but I recently changed it to index.php. However, when I try to access the website, it keeps redirecting me to home.html instead of index.php. Can anyone offer ...

Display a close button for every individual item and use JavaScript to conceal the current item

I have a list that contains input fields along with delete link elements. I want to display the delete link element when the user hovers over the respective input field. Additionally, clicking on the close button (X) should hide the entire list item (li) f ...

Is there a way to reveal multiple inputs by simply pressing a button?

Currently, I am working on developing a website using HTML, CSS, and JavaScript. As part of the process, I am looking to implement a login interface. I have already included some input fields and written a sign-in.js script that verifies whether a user h ...

Exploring the power of CSS grid in React/Gatsby for creating dynamic layouts with varying numbers of columns in each

I need help creating a dynamic grid layout where the number of columns alternates between rows. For example, I want the first row to have one column, the second row to have two columns, and so on. Right now, my grid only has two columns per row. Does anyon ...

Issues with implementing scrollmagic section wipe demonstration

Hey everyone, I've been trying to implement the parallax section wipe effect using ScrollMagic, following the demo from here. Unfortunately, no matter what I do, I can't seem to get it to work. It's been driving me crazy and I've spent ...

The OnSelectedIndexChange event seems to be failing to trigger

I've been struggling to get an event to execute properly. Adding AutoPostBack = "true" didn't fix the issue, and trying the same event on a different DropDownList was unsuccessful. <div class="col-sm-3" style="float: left"> ...

Stopping a jQuery function from executing multiple times while it is already active - is it possible?

Currently, I am utilizing the http://jsfiddle.net/CqAU2/ plugin for image rotation on my website. The issue I am facing is that when the user clicks on the image box multiple times, it continues rotating each time. I want the click action to only be regi ...

Tips for Positioning Ant Design Select Dropdown Anchor to the Right of the Select Component

Currently, I am utilizing Ant Design in my project and encountering an issue with a Select component. The Select is positioned to the right side of the screen and contains labels that are quite long. This causes the dropdown to overflow and a scrollbar t ...

Adding JSON objects to an HTML body with pure JavaScript - no need for jQuery!

I have my website content stored in a 'content.json' file, resulting in an empty body in my HTML index. Currently, I am able to console.log all the elements from 'script.js' and view all the divs declared in 'content.json' wit ...