Why is the div element within the container not automatically occupying 12 columns, but rather only 1?

I'm struggling to comprehend the bootstrap grid system, as explained in Murach's .Net book.

Please take the time to read through the entire post. I am aware of the solution (

<div class="col-lg-12">Col x</div>
works), but I am curious about the rationale behind bootstrap's design.

I copied an example from a book where "Column a" and "Column b" are supposed to utilize the 12 columns provided by bootstrap's grid system. This is what I gathered from the book. The snippet looks like this:

<main class="container">
    <div class="row">
        <div>Column a</div>
        <div>Column b</div>
    </div>
    <div class="row">
        <div class="col-md-4">Column c</div>
        <div class="col-md-8">Column d</div>
    </div>
    <div class="row">
        <div class="col-md-4 col-sm-6">Column e</div>
        <div class="col-md-8 col-sm-6">Column f</div>
    </div>
</main>

In the supplied code, the divs should span the full 12 columns according to the book's illustration (where "Column 1" and "Column 2" correspond to "Column a" and "Column b" in my code).

The expected output based on the book's screenshot can be viewed here.

However, when implementing the code, the actual result differs, as shown in this image. Only applying the aforementioned trick corrects this issue.

The following details were specified in the head section:

<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<script src="Scripts/jquery-3.0.0.min.js"></script>
<script src="Scripts/bootstrap/min.js"></script>
<link href="style.css" rel="stylesheet" />

This is the content of style.css:

.container 
{
    padding-top:1em;
}
.row 
{
    margin-bottom: 2em ;
}
.row div
{
    border: 1px solid black ;
    padding: 0.5em ;
    background-color:lightgrey ;
}

Answer №1

The behavior of the DIV elements is determined by flexbox. When a simple DIV is placed inside a Bootstrap 4 row class, it automatically follows the default flexbox behavior. It does not occupy one column unit; rather, it adjusts its width according to the content because neither grow nor shrink properties are specified.

The Bootstrap 4 col-* classes have specific attributes designed to function within the parent row, including flex-basis, max-width, padding, and more.

For additional information, visit

Explore further details in the Bootstrap documentation...

"Rows serve as containers for columns. Each column features horizontal padding (referred to as a gutter) to control the spacing between them. These paddings are offset on the rows using negative margins... In a grid layout, content should be placed within columns, with only columns directly under rows."

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

Badging with Angular, HTML, and Bootstrap

I've been attempting to integrate Bootstrap 5's badges into my Angular application, but they don't seem to be displaying correctly together. While using Angular 13, the html within the app-root component appears clustered without proper for ...

What is an easy method for including a broader div within a Y-scrolling container containing multiple divs?

Here is a scenario for consideration: http://codepen.io/anon/pen/NPGKPq Is there a CSS approach, without incorporating Javascript, to display the full width of the blue div? Essentially, rather than having a horizontal scrollbar, only show a vertical scr ...

Creating a table with a static first column and vertical text positioned to the left of the fixed column

To create a table with the first column fixed, refer to this fiddle link: http://jsfiddle.net/Yw679/6/. You also need a vertical text to be positioned to the left of the fixed column in a way that it remains fixed like the first column. The disparities be ...

Steps for displaying the dropdown menu

I am having trouble with displaying the dropdown menu when hovering over the class (dropbtn). I tried adding .dropdown:hover .dropdown-content-strategy{..} but it's not working as expected. If I do manage to show the dropdown menu, it stays visible ev ...

Troubleshooting directive not functioning properly with AngularJS ng-click

My HTML img tag is not responding to ng-click when clicked. I'm puzzled by this issue occurring in masonryPictureView.html Main page - home.html <ng-masonry> <ng-picture ng-items="projectdescription.pictures"></ng-picture> </n ...

Reverse the impact of the preceding CSS directive

In this screenshot example, the padding at the bottom in Material-UI has been increased from 16px to 24px using the CSS rule below: .MuiCardContent-root:last-child { padding-bottom: 24px; } https://i.sstatic.net/IWaIu.png I want to revert it back to ...

The jQuery bookmarklet in the djangobyexample book is completely unresponsive

As I work my way through Django By Example, I came across a chapter where a jQuery bookmarklet is created within a Django app. This allows users to easily save jpg images from websites into their user profile area within the Django app. Although the tutor ...

What is the best way to retrieve the parent div's ID with JavaScript and Selenium?

My current project involves utilizing webdriverjs, and I am faced with the challenge of retrieving the parent id of a specific webelement among multiple elements with similar classes. Each element has a different id that gets dynamically generated when a m ...

Retrieve the browser version of a client using C# (When Internet Explorer Compatibility mode is activated)

Is there a way to retrieve the actual version of the client's browser from C# code? Using Request.Browser or HTTP_USER_AGENT provides details, but in Internet Explorer (IE), when compatibility mode is enabled, it always returns IE 7 regardless of the ...

Setting the height of a webpage element to match the height of the window through CSS

How can I make the .Bck div stretch to the full height of the browser window? I attempted using jQuery, but the height doesn't adjust correctly when I resize the window. What is wrong with my JavaScript and can this be achieved with CSS alone? <!- ...

Capture the item selected from the dropdown using ng-model to retrieve the name instead of the

I need help getting the name/text/html of the selected option in a dropdown using angular.js, rather than just its value. Currently, I have this setup which retrieves the value: Here is my HTML code snippet <select class="SelectCar" ng-model="Selected ...

execute operations on a separate webpage

Currently, I am facing a challenge while developing an application that allows regular users to modify data that is typically only accessible through an admin panel. My application effectively filters out any sensitive information; however, once the data i ...

Randomly switch out the wordpress header image at designated intervals

I have a custom header image on my website that displays 6 random images each time the site is visited. I am attempting to change this group of images while the user is browsing my site. Here is the code I am using: function show_banner(){ document.ge ...

Replicate the preceding input data by simply clicking a button

Here is some HTML and jQuery code that I am working with: $(".btn-copy").click(function() { var previousContent = $(this).prev()[0]; previousContent.select(); document.execCommand('copy'); }); <script src="https://cdnjs.cloudflare.com ...

Is there a way to modify my code to eliminate the need for a script for each individual record?

Whenever I need to create a code with the ID by browsing through my records, is there a way to make just one function for all the records? $tbody .= '<script> $(document).ready(function(){ $("#img'.$idImage .'").click(functi ...

Removing HTML tags and then reapplying HTML tags Part 1

Attempting to convert the following string: <p> string <b> bold <em>italic string</em> also(bold) </b> </p> to this modified string: <p> string </p> <!----------- ...

What could be causing the event listener to not be triggered?

I'm having an issue with a simple code that is supposed to display an alert box when a hyperlink is clicked, but it's not working. I believe the script is not being called. Can someone help me figure out what's wrong? Here is the HTML code: ...

Conceal the HTML input element until the JavaScript has finished loading

Currently, I am using a JQuery plugin to style a form input with type 'file'. The plugin adds a new span over the input element to create a mask effect and encloses everything in a div. However, there is an issue where the standard HTML input box ...

Get the input tag's value prior to submitting the form

After the user enters a number into an input tag in my HTML code, I need to immediately retrieve that value for mathematical calculations. I intend to use the "onchange" form event to trigger a PHP function responsible for performing the calculation before ...

Adjusting data points on a radar chart.js through user interaction

I have a radar map generated using chart.js, along with some input fields where users can enter values that I want to reflect in the graph. I am exploring ways to update the chart dynamically as the user types in the values. One option is to have the char ...