The impact of breakpoints in Bootstrap

As a newcomer to Bootstrap, I have a question about the code snippet below:

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row border">
    <div class="col-lg-4 border">One</div>
    <div class="col-lg-4 border">Two</div>
    <div class="col-lg-4 border">Three</div>
  </div>
</div>

When I resize my browser window to a smaller size that doesn't meet the lg breakpoint criteria, the three elements stack on top of each other. However, if I remove the breakpoint entirely, like so:

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
  <div class="row border">
    <div class="border">One</div>
    <div class="border">Two</div>
    <div class="border">Three</div>
  </div>
</div>

The three elements now appear as inline elements instead of stacking on top of each other. Why is this happening? The code remains the same with col-lg-4, and the lg breakpoint isn't being triggered as long as I stay within the small window size.

Answer №1

The reason behind this behavior is due to the fact that the Bootstrap row utilizes display:flex, with the default flexbox direction being flex-direction:row. Removing the .row class will result in the inner divs stacking as anticipated.

<div class="container">
    <div class="border">
        <div class="border">One</div>
        <div class="border">Two</div>
        <div class="border">Three</div>
    </div>
</div>

Check out the demonstration here:


It's important to note that the .row class should only be utilized for containing grid columns (.col*).

Answer №2

Here is the essential CSS snippet from bootstrap (I've excluded irrelevant portions)

.row {
    display: -ms-flexbox;
    display: flex;
    -ms-flex-wrap: wrap;
    flex-wrap: wrap;
    /*margin-right: -15px;*/
    /*margin-left: -15px;*/
}

/*.col, .col-1, .col-10, .col-11, .col-12, .col-2, .col-3, .col-4, .col-5, .col-6, .col-7, .col-8, .col-9, .col-auto, .col-lg, .col-lg-1, .col-lg-10, .col-lg-11, .col-lg-12, .col-lg-2, .col-lg-3, */
.col-lg-4,
/*.col-lg-5, .col-lg-6, .col-lg-7, .col-lg-8, .col-lg-9, .col-lg-auto, .col-md, .col-md-1, .col-md-10, .col-md-11, .col-md-12, .col-md-2, .col-md-3, .col-md-4, .col-md-5, .col-md-6, .col-md-7, .col-md-8, .col-md-9, .col-md-auto, .col-sm, .col-sm-1, .col-sm-10, .col-sm-11, .col-sm-12, .col-sm-2, .col-sm-3, .col-sm-4, .col-sm-5, .col-sm-6, .col-sm-7, .col-sm-8, .col-sm-9, .col-sm-auto, .col-xl, .col-xl-1, .col-xl-10, .col-xl-11, .col-xl-12, .col-xl-2, .col-xl-3, .col-xl-4, .col-xl-5, .col-xl-6, .col-xl-7, .col-xl-8, .col-xl-9, .col-xl-auto */ {
    /*position: relative;*/
    width: 100%;
    /*min-height: 1px;*/
    /*padding-right: 15px;*/
    /*padding-left: 15px;*/
}

@media (min-width: 992px)
.col-lg-4 {
    -ms-flex: 0 0 33.333333%;
    flex: 0 0 33.333333%;
    max-width: 33.333333%;
}

In summary: .row is a flex container with child elements having auto flex-basis unless specified. .col-lg-4 always takes up 100% width, but on larger screens (>= 992px), it shrinks to one third, with a flex-basis of 33.333333%.

If you remove the col-lg-4 class, the elements revert to auto flex-basis, taking up only necessary space.

Answer №3

The Bootstrap grid system consists of five classes:

.col- (for extra small devices with a screen width less than 576px)
.col-sm- (for small devices with a screen width equal to or greater than 576px)
.col-md- (for medium devices with a screen width equal to or greater than 768px)
.col-lg- (for large devices with a screen width equal to or greater than 992px)
.col-xl- (for extra large devices with a screen width equal to or greater than 1200px)

These classes automatically adjust based on screen sizes changes

Bootstrap Grid System

Answer №4

To maintain a stacked layout for divs with the specified class, consider replacing col-lg-4 with col-xs-12.

I trust this suggestion will be beneficial for your design.

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

Display or conceal elements based on the screen size using Bootstrap 3 classes

While this may seem straightforward for some, the reality is quite different. I have two images that need to be displayed based on screen size. In my HTML code, I used classes like hidden-md, hidden-sm, and hidden-xs assuming they would only show on larg ...

AngularJS - Changing Views with Templates

At the moment, I am changing my directives in the following manner. Within my directive: (function(){ 'use strict'; var controller = ['$scope', function ($scope) { }]; angular .module('moduleName' ...

The CSS styling feature is not functional within the JavaMail API

After receiving an email sent using the JavaMail API, I noticed that the CSS styles are not being applied and only the HTML content is rendered. HTML CODE <!doctype html> <html lang="en"> <head> <meta charset="u ...

Troubleshooting: Unable to load template file content in AngularJS ng-view

Setting up a demo angular app has been quite the challenge for me. Despite my efforts, the content from my partial file (partials/test.html) is not loading into the layout file as expected. Below is a snippet of my index.html: <!DOCTYPE html> <ht ...

The car glides smoothly across the screen from left to right before disappearing from view

I'm looking to create an animation where a car moves from left to right and then switches to another image moving from right to left. Can anyone provide assistance with this task? Here's the code I have so far: .car-movement { position: a ...

What's the best way to ensure that a div remains fixed at the top of the page while also being centered?

I have a div that I've centered using margin left and right auto. However, I want this bar to be visible throughout my page, so I positioned it absolutely. But now the bar is not centered anymore, it's appearing on the left. How can I position th ...

Why does a relatively positioned element always begin at the top corner of the window?

I am really struggling to understand the concept of relative versus absolute positioning, especially when dealing with parent or grandparent elements that have position values. Can someone please help me clarify these two scenarios: Case 1: grandparent ...

How can I dynamically assign a class to an HTML element in Codeigniter?

In my CodeIgniter application, I am utilizing a Bootstrap template that includes a menu as a separate file. This menu is called on all pages and when a menu item has the class="active", it is highlighted to show users their current location. My question is ...

When I apply a percentage to the height of a div element, it shrinks down to a height

I have several lists with a flex layout. Each column contains 3 list items, and the flex-basis is set to one-third. Within each li element, there is a div for a background image and another div to display text, which should be positioned below the image. ...

Scrolling Horizontally with Bootstrap Cards

I'm trying to implement horizontally scrolling cards using Bootstrap V.5, like this: <div class="container-fluid py-2"> <div class="d-flex flex-row flex-nowrap" style="overflow: auto;"> <div cl ...

Reduce the text of the link

Trying to simplify a task, but I'm struggling with the solution. What I want to achieve is shortening a link to 30 characters and adding ... at the end if it's longer. Also, I'd like to make it possible to see the full link on hover similar ...

What is the process to set a Woocommerce checkout field as optional when a checkbox is marked?

I need assistance with customizing the checkout page on Wordpress Woocommerce. Specifically, I want to make the field 'billing_name' not required if the checkbox 'buy_on_company' is checked. Currently, I have successfully hidden ' ...

Utilize various CSS styles for individual sections within a multi-page website

Having a web page with multiple subpages each having their own specific CSS can be quite challenging. Sometimes, when all the CSS files are applied globally, they tend to mix with each other causing a headache for developers. One common approach is to decl ...

Is there a way to enhance the height of Bootstrap combobox items? Can this be achieved?

Is it possible to improve the height of Bootstrap combobox item? How can I achieve that?https://i.sstatic.net/MkGFd.jpg 1 2 3 4 ...

Using a declared const in a separate React file

I've been searching for a solution, but haven't found anything that helps. I'm trying to retrieve data from an API and pass it to another page. The information is currently defined as "drink.strDrink", including the name and image. However ...

Having trouble pinpointing the issue with this particular if statement?

I am currently working on creating a form that compiles all entered data when the user fills out all fields. The form is connected to a PHP file and functions properly, but I encountered issues when implementing validation logic. The "Validation" section ...

Update the function to be contained in a distinct JavaScript file - incorporating AJAX, HTML, and MySQL

Currently, I am working on a project and need to showcase a table from MySQL on an HTML page. The JavaScript function in my code is responsible for this task, but due to the Framework 7 requirement, I must separate the function into a different .js file ra ...

AngularJS - Sending configuration values to a directive

I'm trying to figure out how to pass parameters (values and functions) to an Angular directive. It seems like there should be a way to do this in Angular, but I haven't been able to locate the necessary information. Perhaps I'm not using th ...

Utilizing document.getElementById().value in hidden fields for asp.net web pages does not function correctly

Recently, I developed a basic website using ASP.NET Web Pages with Razor markup. The site includes a form with two hidden fields: latitude and longitude. My goal was to utilize the HTML5 Geolocation API in my JavaScript script to obtain the user's loc ...

css subcategories - divs failing to meet css criteria

I am encountering an issue where my subclass is not working on DIV elements. While the CSS rules are able to match inline tags like span and a, they do not seem to work with div. Please refer to the example provided below: <style> .row { disp ...