Increase the width of the Bootstrap column by one unit specifically for large screens

I'm currently trying to implement Bootstrap's documentation in order to create a layout where one column is wider than the other two. You can find my JSFiddle at the end of this post.

If you'd like more information, you can check out the official Bootstrap documentation here.

My goal is to achieve the following layout with 3 columns, but only for large devices: https://i.sstatic.net/PTWcB.png

After experimenting with the code snippet below:

<div class="row no-lf-margin">
    <div class="col-xl-3 col-lg-3 col-md-12 col-sm-12 bg-warning">
        1
    </div>
    <div class="col-xl-7 col-lg-7 col-md-12 col-sm-12 bg-danger">
        2
    </div>
    <div class="col-xl-2 col-lg-2 col-md-12 col-sm-12 bg-secondary">
        3
    </div>
</div>

I found that the first column (orange) was taking up too much space and the third column (grey) wasn't quite right either. It seems like I need something like col-xl-2.5 for the first column and col-xl-1.5 for the third one.

Note: The layout works fine on other screen sizes because they stack due to col-md-12 and col-sm-12.


In an attempt to follow the documentation more closely, I tried the snippet below:

<div class="row no-lf-margin">
      <div class="col bg-warning">
         1
      </div>
      <div class="col-7 bg-danger">
        2
      </div>
      <div class="col bg-secondary">
        3
      </div>
</div>

The result was better for the first column (orange), but the third column (grey) was still not quite right. Additionally, all columns were now displayed next to each other on all screen sizes as there was no mention of col-md-12 or col-sm-12.

Next, I attempted to combine the two approaches:

<div class="row no-lf-margin">
    <div class="col col-md-12 col-sm-12 bg-warning">
        1
    </div>
    <div class="col-7 col-md-12 col-sm-12 bg-danger">
        2
    </div>
    <div class="col col-md-12 col-sm-12 bg-secondary">
        3
    </div>
</div>

Unfortunately, this resulted in all columns being stretched to col-12 and stacking on top of each other.


JSFiddle containing both versions and desired outcome: https://jsfiddle.net/gcdn3str/

Answer №1

When using the .row and .col classes in Bootstrap, it's important to ensure that the total number of columns always adds up to 12. For example, if you have 2 col-6 elements, they will each take up 50% of the width. Similarly, if you use 3 col-4 elements, they will evenly divide the page into thirds.

If you want certain columns to be larger than others, you can mix and match different column sizes as long as they add up to 12. For instance: col-4, col-5, col-3.

You can also nest rows within columns to further customize the layout. This allows you to adjust the sizing of individual columns. For example:

<div class="row">
    <div class="col-md-4">1</div>
    <div class="col-md-8">
        <div class="row">
            <div class="col-md-7">2</div>
            <div class="col-md-5">3</div>
        </div>
    </div>
</div>

Feel free to experiment with different combinations to achieve the layout that best suits your needs!

Answer №2

Here is the recommended code snippet to achieve the desired effect:

.element
    {
         margin: 0;
     }

    .blue {
        background-color: blue;
    }

    .green {
        background-color: green;
    }

    .purple {
        background-color: purple;
    }
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<div class="container-fluid">
        <div class="row no-gutters">
            <div class="item col-12 col-lg-3 blue"></div>
            <div class="item col-12 col-lg-8 green"></div>
            <div class="item col-12 col-lg-1 purple"></div>
        </div>
    </div>

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

Verify if the jQuery library has an existing style attribute

I've been working on a script to loop through my form inputs and check for the presence of the style attribute. Despite feeling like I'm close to getting it right, there seems to be an issue with my code. There are actually only 2 inputs with the ...

Differences in Spacing: Exploring Bootstrap v5.1.1 Compared to 4.0

After comparing two pages, one using Bootstrap v5.1.1 and the other with 4.0.0, I have observed differences in spacing. Bootstrap v5.1.1: .topspacer { padding-top: 70px; } .righticon { float: right; } .clear { clear: both; } a { text- ...

css overflowing content can disrupt the layout of the screen

I am currently working on creating a modal that will be displayed in the center of the screen with the same width as the main page. However, I am facing an issue where the modal is not perfectly centered. I suspect that this is due to the overflow style ...

Customize the appearance of tables in your WordPress theme

I'm currently working on a website using the Wordpress Twenty Ten theme and I've run into an issue with inserting html tables. The theme seems to be overriding my styling, resulting in very unattractive tables. I've attempted to fix this pro ...

Issue with Bootstrap 4 Webpack Sass functionality failure

Encountering a problem with Webpack and Bootstrap 4 mixins and functions. sass|css loader: { test: /\.(sa|sc|c)ss$/, use: [ // fallback to style-loader in development // which creates style nodes from JS strings (IE: imports) ...

Having trouble aligning the form in the center?

Despite #container being centered, I am struggling to center the <form>. It is important for me to have all elements inside the form aligned horizontally and despite trying various techniques, it is not working as expected. <!-- Container Start - ...

CSS guidelines for handling single line breaks with the white-space property set to pre-wrap

Is it considered acceptable to include solitary carriage return characters in an HTML block styled with white-space: pre-wrap? I am working with an application that receives SOAP messages and logs them to a file. The logging process involves removing new- ...

Tips for adjusting the position of the sidebar with a grid system

I am working with a traditional design: #divleft{ float:left; } #sidebar{ float:right; } @media screen and (max-width: 900px) { #divleft, #sidebar{ float:none; } divleft and sidebar are elements within panelb; Now, I am exploring ...

React JS onClick() failing intermittently

I've spent hours trying to solve this problem. The issue first arose on iOS while working on a website I had recently launched. I've tested the code below, and it works correctly 90% of the time, but occasionally fails. <button className=" ...

How can I incorporate a background image into an Angular template without using a CSS file?

Can you please provide guidance on setting a background image in Angular using the ngFor directive within a carousel grid layout? <div class="slide slick-bg s-bg-1" *ngFor="let movie of nowPlaying" style="https://image.tmdb.org/ ...

Align and resize image using CSS styling

Seeking assistance on centering and cropping images using CSS. Tried implementing the technique from this specific article. However, experiencing inconsistencies in device UI output. Can someone shed light on this behavior? Scenario: The objective is t ...

Presenting XML data in HTML using a customized CSS stylesheet

I've explored various methods for showcasing an XML file on a web page using HTML, but I haven't yet encountered one that incorporates a CSS style sheet. The XML file I have already includes a CSS style sheet. When viewed in a web browser, the . ...

Flex children do not adhere to max-height and max-width properties

There seems to be an issue with setting the max-width and height for flex children. The div is getting squished down to fit its children exactly, possibly due to the styling associated with div .content. It's not clear why this is happening. The divi ...

Issue with CSS hover effect not being detected in IE 7 on a div element

Need some help with a top navigation setup for a website where I have a div inside an li element. Encountering issues in IE 7 where there are "holes" in the box causing the drop down to disappear when the user is still interacting with it. Initially trie ...

The CSS selector "not:nth-child" is failing to function properly within the template

Trying to apply a margin-top to all labels except the first one in a row using the :not() and :nth-child selectors. Having trouble with my selector, can anyone help? .form-group:not(:nth-child(1)) + .label { margin-top: 20px; } <div class="form- ...

Is it possible to style a strikethrough item in a list using CSS?

Within this list, I want to use CSS to indent any list item that drops a line. I attempted to achieve this using the following CSS: li:not(::first-line) { text-indent: 15px; } Unfortunately, it doesn't seem to be working as expected. Here is an exa ...

Tips on positioning images to the left and right without causing text to wrap around them

I have successfully floated an image to the left of text with the following CSS. How can I achieve a similar effect with an image to the right of text on the same page? Do I need to use a clearfix between the left and right images? Thank you! .innercont ...

center commotion excitement vessel vertically

I am facing a dilemma once again and could really use a helping hand. I have created an exciting animation that I have placed within a Bootstrap 4 tab, but I'm struggling to center it vertically. Is there someone out there who could help me out? Tha ...

Achieve a Smooth Transition: Utilizing Bootstrap 3 to Create an Alert Box that Fades In upon Click and Fades Out

Incorporating AngularJS and Bootstrap 3 into my web app, there is an "Update" button that saves user changes. Upon clicking the "Update" button, I aim to trigger and display bootstrap's alert box with the message "Information has been saved," followed ...

Adjust the size of the window to prevent the text from shifting

How can I lock text and tables in place when resizing the window? Can someone provide an example? ...