What is the best way to create a uniform appearance for all five columns in a bootstrap grid?

My goal is to create a layout with 5 columns on extra large screens, 4 columns on large screens, and 2 columns on small/extra small screens. Currently, my code looks like this:

<div class="row mb-5">
        {% for item in foobar %}
        <div class="col-6 col-md-3 col-xl-2 py-2">
           /* Content Here */
        </div>
        {% endfor %}
    </div>

Although this code works for most screen sizes, I am encountering an issue on extra large screens where I want 5 columns but am getting 6 instead.

Since 12 (the total number of columns) is not divisible by 5, I'm struggling to figure out the best approach to achieve my desired layout.

To address this problem, I have attempted the following:

I experimented with adding row-cols-xl-5 to the row class based on suggestions from Stack Exchange answers, but this did not produce the desired result. Additionally, I tried using

<div class="col offset1-xl"></div>
before each content-containing div, which did give me 5 columns on extra large screens. However, this caused alignment issues with adjacent rows and resulted in odd spacing.

What is the easiest and most effective method to achieve the desired responsive layout?

While there is a similar issue discussed on Stack Exchange regarding 5 columns without specifying screen size, my requirement is to have 5 columns only on certain sized screens to ensure responsiveness.

Answer №1

If you want to have a CSS class for 5 columns in a row specifically for extra-large screens, you can create a media query to apply this style.

Here's a demo snippet:

.col-20{
  width: calc(100% / 5);
}
<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
</head>
<body>

<div class="container-fluid">
  <h1>Three equal width columns</h1>
  <p>Note: Try to add a new div with class="col" inside the row class - this will create three equal-width columns.</p>
  <div class="row">
    <div class="col-20 col" style="background-color:lavender;">.col</div>
    <div class="col-20 col" style="background-color:orange;">.col</div>
    <div class="col-20 col" style="background-color:lavender;">.col</div>
    <div class="col-20 col" style="background-color:orange;">.col</div>
    <div class="col-20 col" style="background-color:lavender;">.col</div>
  </div>
</div>

</body>
</html>

Answer №2

Have you experimented with using just the class col-lx by itself? It should automatically adjust as needed, like this:

<div class="row mb-5">
   <div class="col-xl col-lg-3 col-md-3 col-sm-6">
      //Content
   </div>
</div>

Please inform me if this solution is helpful to you.

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

<div class="row mb-5">
  <div class="col-xl col-lg-3 col-md-3 col-sm-6 bg-danger">
  123
  </div>
  <div class="col-xl col-lg-3 col-md-3 col-sm-6 bg-warning">
  123
  </div>
  <div class="col-xl col-lg-3 col-md-3 col-sm-6 bg-danger">
  123
  </div>
  <div class="col-xl col-lg-3 col-md-3 col-sm-6 bg-warning">
  123
  </div>
  <div class="col-xl col-lg-3 col-md-3 col-sm-6 bg-danger">
  123
  </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

Issue with margin-top not meeting set expectations

To assist in providing assistance, I have put together a quick jsfiddle project. I am curious why the use of margin-top for #container causes the entire body to be pushed down from html, rather than just pushing #container away from #containerWrapper. Th ...

Steps to create a div with a z-index of 1 that fills the entire height:

Looking at the image, I have a sidebar that opens over other elements (blue color) using z-index = 1. When the sidebar is open, I want to hide all other elements except for the sidebar. However, towards the end of the sidebar, I can still see other element ...

Experience the Firefox nightmare with a fixed background-attachment!

I've been struggling with a puzzling issue all day, and I'm hoping that someone with more expertise can help me solve it. As I work on updating the design of my website, I've encountered what seems to be a bug specific to Firefox. I've ...

What are some ways to change the appearance of a component using its parent?

In order to make changes to the CSS properties of a Vue component from its parent, effectively overriding any existing styles within the component, I am seeking a solution. Initially, I assumed that styling a component like <my-component> directly f ...

What's the best way to add two distinct colors as background for a header with css?

h2 {background-color:#C61236; color:#FFFFFF; font-size:medium; line-height:1.5; text-indent:20px;} Although the current setup looks good, I am looking to incorporate a new color block at the start of the text. I'm not sure what steps to take nex ...

Shift the "Login link" to the right side of the Bootstrap navbar

Currently working on a Django website and incorporating Bootstrap/CSS/HTML to enhance the design. I've set up a navbar menu and want the login tab on the right side. However, despite my attempts, I'm not achieving the desired outcome: The HTML ...

Discover the ultimate guide to implement a captivating visual overlay using CSS

I've crafted some links using CSS to incorporate a background image (to give it the appearance of a button) employing the subsequent code: <a class="btnImg" id="btnImgConfig" href="#"></a> .btnImg { width:100px; height:100px; ...

Obtaining a single element from a hidden display while dragging using Jquery UI

Is there a way to move an element outside of a container with overflow hidden when using drag and drop functionality in CSS? Currently, when I drag an element with the "item" class, it stays within the confines of the container with overflow set to hidden. ...

Locate the final flexbox in the initial row and the initial flexbox in the concluding row

How can I identify the last element of the first row and the first element of the last row in the given code to make it round from all corners? It should be noted that the column number will vary as well as the last element of the first row and first eleme ...

What are the steps to transforming shorthand CSS into longhand?

Is there a tool available that can automatically convert shorthand CSS to longhand? I am in need of this functionality because I would like to utilize SmartSprites, which is not compatible with shorthand formatting. Additionally, if there is a tool that c ...

Struggling to toggle the visibility of a table with a button - successfully hiding it, but unable to make it reappear?

I need a button that can toggle (show/hide) a table. Currently, my code hides the table successfully, but it fails to show the table again when I click the button. It seems like there is an issue with refreshing or redirecting after clicking the button for ...

automating the styling of elements

Hey everyone, I have a specific element that I want to conditionally apply multiple styles to. Initially, I approached it like this: <Text style={[ discountCodeState === '' || !isActiveHandler(value) ? [ ...

I have a question for you: How can I customize the font size in Material-UI TextField for different screen sizes?

I'm facing a challenge in Material-UI where I need to set different font sizes for TextField. While it may be simple in HTML/CSS, it's proving to be tricky in Material-UI. Can anyone help me figure out how to achieve this? The code snippet below ...

Connect the scroll wheel and then proceed to scroll in the usual way

There are two div elements with a height and width of 100%. One has the CSS property top:0px, while the other has top 100%. I have implemented an animation that triggers when the mousewheel is used to scroll from one div to the other. The animation functi ...

Maintain the style of the main navigation menu when hovering over the sub-navigation items

I am currently in the process of redesigning the navigation bar for a specific website. The site utilizes Bootstrap, with a main navbar and a secondary navbar-subnav. While I have been able to style both navs accordingly, I am encountering difficulties whe ...

Center the navigation links within the navigation block

I am using the startbootstrap-small-business template and customizing it to fit my requirements. I have inserted a new logo which caused me to adjust the height of the navbar to approximately 120px. Now, my goal is to vertically align the links on the righ ...

Utilizing Bootstrap Modals to seamlessly redirect me to a new empty page

Despite the fact that my website functions perfectly fine without a build system, I am encountering an issue with the Bootstrap modals while using the Yeoman: Angular + Gulp build system. Whenever I click on a list item, instead of the modal appearing, it ...

Tips for placing a searchbox on top of a carousel?

I need to create a search box that overlays on top of a carousel image. I currently have the search box positioned absolutely, but it's not responsive as it doesn't stay within the carousel as the width decreases. How can I make it responsive whi ...

Blending ThreeJS graphics with typical HTML/CSS pages

Is it feasible to swap out an animated image in the background of a website with JSON geometry while keeping the HTML elements in the forefront? I've attempted to utilize DOM Elements in ThreeJS without success. I experimented with incorporating my JS ...

The flex box structure crumbles, causing the boxes to align side by side instead of in a masonry layout

I'm currently facing a challenge with my project regarding the utilization of flexbox. Everything seems to be in order, but as you resize your viewport to around 1400px, the column layout collapses. I'm looking for a way to prevent this from hap ...