Configuration of Bootstrap "col" without an xl number

Hello everyone,

I am currently working on a project using Bootstrap, where I have a row with five child elements:

<div class="container">
   <div class="row">
      <div class="col">[content]</div>
      <div class="col">[content]</div>
      <div class="col">[content]</div>
      <div class="col">[content]</div>
      <div class="col">[content]</div>
   </div>
</div>

(The content within these elements consists of Bootstrap cards)

I have applied flexible layout classes to the col divs:

<div class="col col-12 col-md-6 col-lg-4 col-xl-3">[content]</div>

While this works well for smaller screens and leads to wrapping, I want the col divs to behave like regular col elements when there is enough room for all five pieces of content to fit on the same row.

I tried using col-xl-auto, but the cards did not evenly divide up the space on larger screens.

I attempted to use Bootstrap's row-cols-* functionality by adding

class="row row-cols-xl-5"
to the row and col-xl-1 to each content div, but this did not produce the desired result. The row seemed to limit itself to the space previously occupied by the first five columns, causing the cards to squeeze into that space. I expected row-cols-* to divide the same horizontal space into * columns rather than restricting the width of the row. Am I misunderstanding this feature?

If anyone has any ideas on how to achieve the desired behavior of making col-xl-something act like a standard col, I would greatly appreciate it.

Answer №1

A valuable lesson learned

My attempt to use the row class "row row-cols-xl-5" with each content div set to col-xl-1 was unsuccessful.

It's important to remember that column classes take precedence over row classes. By using ".col-xl-1," you are essentially forcing each column to be 8.33% in width, which overrides the 20% column width set by ".row-cols-xl-5."

Bootstrap doesn't strictly adhere to a 12-column grid system; instead, it utilizes percentage-based widths and flex properties to size columns. This contrasts with CSS Grid, where you explicitly define the number of columns a div should span.

Including

<div class="col col-12 ...">[content]</div>

The use of ".col" alongside ".col-12" is redundant. ".col" sets "flex-grow: 1;" on the column, which is then overridden by ".col-12," setting "flex-grow: 0;" while maintaining "width: 100%."

I experimented with col-xl-auto, but it didn't yield the desired outcome.

The "-auto" modifier assigns the columns their natural minimum width, setting "flex-grow: 0;" and "width: auto;" for each column.

Proven Solution

Is there a way to achieve the standard column behavior with col-xl-something?

Remember, column classes take precedence over row classes. Implement the following:

This will result in:

  • Full-width columns by default: ".row-cols-1";
  • At the "md" breakpoint, two equal columns: ".row-cols-md-2";
  • At the "lg" breakpoint, three equal columns: ".row-cols-lg-3";
  • For the "xl" breakpoint, ".col-xl" columns will have "flex-grow: 1;".

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3a5855554e494e485b4a7a0f1409140a175b564a525b0b">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86e4e9e9f2f5f2f4e7f6c6b3a8b5a8b6abe7eaf6eee7b7">[email protected]</a>/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>

<div class="container">
   <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3">
     <div class="col-xl">[content]</div>
     <div class="col-xl">[content]</div>
     <div class="col-xl">[content]</div>
     <div class="col-xl">[content]</div>
     <div class="col-xl">[content]</div>
   </div>
</div>

Furthermore, since your ideal layout and card elements were not specified, consider whether a predefined column structure is necessary. Evaluate the behavior of your layout across different viewport widths before assigning column numbers.

If a fluid card layout is your goal, initiate with the initial layout and adjust as needed starting at the "md" breakpoint:

<div class="container">
   <div class="row">
      <div class="col-md">[content]</div>
      <div class="col-md">[content]</div>
      <div class="col-md">[content]</div>
      <div class="col-md">[content]</div>
      <div class="col-md">[content]</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

Simple HTML/CSS text blocks positioned beside images in a list format

I'm attempting to design a list (ul) where each li has an img on the left and text aligned to the left (including a title and paragraphs) positioned next to the image on the right. I've experimented with float: left and various display propertie ...

Strange alignment issues occurring solely on certain PCs

Currently, I am in the process of developing a website for a client who has requested that it be an exact replica of the mockup provided. However, I have encountered some issues with the headers and certain divs that contain background elements. Surprising ...

Although PhoneGap resolution remains consistent, the outcomes may vary

I seem to be facing a similar issue as discussed in this post on Stack Overflow: Screen Resolution On A PhoneGap App My problem is that I have both an iPad and an Android tablet, both with a resolution of 1024 pixels across. However, while my screen displ ...

Having trouble getting SCSS to function properly? (Updated Code)

Transitioning from CSS to SCSS is my current project. I decided to make the switch because of SCSS's nesting capabilities, which I find more convenient for coding. However, I'm facing some challenges getting it to work properly. In my regular CSS ...

Fluid design: prevent alteration in body width caused by vertical scrollbar

I have successfully implemented a liquid layout for my website by setting the body tag width to 100%. <html> <head> </head> <body> <div id="container"> some content </div> </body> body { mar ...

Fixed Sidebar and Dynamic main content with top and bottom sections

Hello, I've been encountering an issue with coding the layout of my website. I want the sidebar to remain fixed regardless of screen size, while also making sure that the content area adjusts fluidly. The header is staying at the top as desired, but I ...

What causes the width of a card to vary between Windows and a Macbook Pro?

Here is the code I have: <div id="app"> <v-app id="inspire"> <v-content> <v-container> <v-card max-width="1000" class="mx-auto" > <v-form v-model="valid"> ...

Chrome is where all the action happens, while Firefox prefers to keep things on the left

After a long break, I returned to designing my website and re-learning CSS. My approach involved creating a WordPress-integrated site with minimal HTML/PHP work, focusing more on utilizing the available CSS classes and IDs provided by a template theme (Bla ...

Exclusive workshop on concealing H1 header in Jumbotron

Trying to make a Jumbotron's height 100% of the user's browser window with a special class, but running into issues on mobile devices as a div within the jumbotron is covering up the headline. Any suggestions for a fix? Live link: HTML <di ...

Image of outer space enclosed by a circular boundary

Check out this fiddle I created here. It's a simple concept of an image inside a red circle. Is there a way to add some spacing around the image within the circle? This is the markup I currently have: <div style=" width: 50px; he ...

text box with an immobile header

As the browser window size decreases, the layout changes. However, when scrolling down, the search text box moves up and is no longer visible due to its lack of fixation. How can I make the search text box stay fixed as I scroll down? I tried implementing ...

Comprehending the Syntax of the ExtJS Framework

I have recently inherited a project utilizing Sencha's ExtJS framework without any handover or documentation. As I navigate through the code, I find myself trying to decipher certain syntax and exploring resources tailored for newcomers to this specif ...

Is there a way to make jQuery Validate display errors within the form elements rather than outside of them?

Given the jQuery Validate basic example provided in the link below, what method can be used to display error messages within form elements whenever feasible (excluding checkboxes)? http://docs.jquery.com/Plugins/validation#source ...

Adding padding-top to the h1 element within a vertically aligned div

I have a div with a set height and the content inside adjusts vertically according to the height. To align it vertically, I added the property display-table; to the parent div and display: table-cell; vertical-align: middle; to the child div. The alignment ...

Tips on solving the Navigation bar burger problem with HTML and CSS

## I am having trouble making my navigation bar responsive using HTML and CSS as the navigation burger does not appear in mobile view ## To take a look at my code on jsfiddle, click here: [jsfiddle] (https://jsfiddle.net/abhishekpakhare/nxcdso7k/1/ ...

How can I position text next to an input within a <td> element in a vertical arrangement?

Below is the HTML code: <fieldset><legend>Callout Report</legend> <table> <tr><td>Start Time</td><td> <input type="text" id="startTimeUI" autocomplete="off" /> </td></tr> <tr><td& ...

Custom sized box causing Bootstrap Navbar to be unresponsive

I am having trouble loading the Bootstrap 3 navbar inside a .box class with specific CSS rules: .box { height: 600px; width: 320px; position: relative; border: 2px solid #eee; } Unfortunately, it is not displaying re ...

There appears to be a gap on the right side of the <span> element, resembling an additional space at the end

While testing a button on my website, I noticed that all the spans have an extra space at the end. Adding a background box made this gap very noticeable. Upon further inspection of my other pages, I realized that the issue exists there too. I have not made ...

The Art of Positioning Containers in CSS

I am facing some issues with my CSS. In the JSP webpage, I have a container styled with a div that works perfectly. However, when I insert another div tag to style certain elements, those elements end up outside of the container for some reason. Below is ...

What is the step-by-step process for executing the following functions: 1. magnify -> 2. spin 360 degrees -> 3. restore to original size before magnification -> 4. switch colors on and

Can you show me a demo featuring the following functions in order: 1. enlarge -> 2. rotate 360 degrees -> 3. resize to original size -> 4. change color (toggle) $(document).ready(function() { $('.tlist td div').click(function() { ...