A series of 8 columns lined up neatly in Bootstrap 4

In need of a row with 8 columns on a large device and fewer columns on a smaller device.

Bootstrap's 12-grid system only offers predefined classes like col-1, col-2, col-3, col-4, col-6, and col-12, making it impossible to have a row with exactly 8 columns.

Various workarounds have been explored, including solutions found on popular platforms like StackOverflow. However, the recommended solution may not align well with bootstrap's default classes such as col-md-3, col-sm-4, etc.

.col-xs-8r,
.col-sm-8r,
.col-md-8r,
.col-lg-8r {
    position: relative;
    min-height: 1px;
    padding-right: 15px;
    padding-left: 15px;
}

// Responsive styling for custom column

@media (min-width: 1200px) {
    .col-lg-8r {
        width: 12.5%;
        float: left;
    }
}

The above code snippet successfully creates the desired layout.

<div class="row">
    <div class="col-lg-8r">
        ...
    </div>
</div>

However, adding another bootstrap class alongside the custom column disrupts the layout functionality.

<div class="row">
    <div class="col-sm-4 col-md-3 col-lg-8r">
        ...
    </div>
</div>

Not a fan of using nested columns and rows as a workaround.

Bootstrap version being utilized: 4.4.1

Answer №1

If we were to enhance the current Bootstrap 4.4 row columns, it might be simpler. For instance...

@media (min-width: 992px) {
    .row-cols-lg-8 > * {
      flex: 0 0 12.5%;
      max-width: 12.5%;
    }
}

Having 8 columns on lg, then switching to 6 columns under lg...

<div class="row row-cols-lg-8 row-cols-6">
    <div class="col">Column</div>
    <div class="col">Column</div>
    <div class="col">Column</div>
    ...
</div>

Check out the demo:

Answer №2

It seems that the issue causing it to not work is related to the order in which CSS files are loaded. If your CSS is loaded before bootstrap, it may be overwritten by Bootstrap classes. To demonstrate this, I have included a style tag with your CSS after the BS include in the HTML panel:

<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" />
<style>
  .col-xs-8r,
  col-sm-8r,
  .col-md-8r,
  .col-lg-8r {
    position: relative;
    min-height: 1px;
    padding-right: 15px;
    padding-left: 15px;
  }
  
  .col-xs-8r {
    flex: 0 0 12.5%;
  }
  
  @media (min-width: 768px) {
    .col-sm-8r {
      flex: 0 0 12.5%;
    }
  }
  
  @media (min-width: 992px) {
    .col-md-8r {
      flex: 0 0 12.5%;
    }
  }
  
  @media (min-width: 1200px) {
    .col-lg-8r {
      flex: 0 0 12.5%;
    }
  }
</style>
<div class="row">
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
</div>

If you maintain the current order of CSS loading, you can make the selectors more specific (I added .row before the selectors):

.row .col-xs-8r,
.row .col-sm-8r,
.row .col-md-8r,
.row .col-lg-8r {
   position: relative;
   min-height: 1px;
   padding-right: 15px;
   padding-left: 15px;
}

.row .col-xs-8r {
  flex: 0 0 12.5%;
}

@media (min-width: 768px) {
.row .col-sm-8r {
  flex: 0 0 12.5%;
}
}

@media (min-width: 992px) {
.row .col-md-8r {
  flex: 0 0 12.5%;
}
}

@media (min-width: 1200px) {
.row .col-lg-8r {
  flex: 0 0 12.5%;
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet" />

<div class="row">
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
  <div class="col-lg-8r col-sm-4 col-md-3">
    column
  </div>
</div>

IMPORTANT

Additionally, note that Bootstrap 4 is based on flex and should avoid using float, instead utilize flex-basis or the shorthand flex property.

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

What is the best way to update the content of a div on an HTML page by incorporating the content of a div from a different page using JavaScript?

I have a requirement where I need to implement a link in a Table of Contents that, upon clicking, should replace the existing content of one div on a page with the content of another div on a different page. My goal is to accomplish this using only JavaScr ...

SwiperJs: I'm struggling to align groups of 2 slides in the center because I am unable to limit the width of the slides

I am currently working on a project involving SwiperJS to create a slider that showcases two slides per group, neatly centered within the container of the slider. Despite my best efforts, I have encountered an issue where the slides expand to occupy the en ...

Why are my AngularJs elements not appearing in the print dialog window?

Whenever I try to open the print Dialogue on a specific page, all I'm seeing is a blank screen. The majority of the content on this page consists of AngularJS elements. To trigger the print dialogue, I use the following code: <a href=""onclick="wi ...

CSharp MVC using HTML input field styled with Bootstrap features

I have set up a textbox on my webpage which prompts the user to input a string. Upon pressing a button, this string is compared to find matching fields in the database. Additionally, I have another button that triggers the display of a bootstrap modal wher ...

Is it possible to align text to an image within an input text field?

<input type="text" class="usernm" style="color:black;font-weight: bold;outline-width: 0;" id="username" /> I have a question about styling HTML. I am attempting to include an icon inside a text field, but I'm strugglin ...

more concise jQuery script

Currently, I am a beginner in jquery and still in the learning phase. Although my existing code is functional and achieving the desired outcome, it feels lengthy and inefficient. I am seeking a way to streamline it and make it more dynamic. Specifically, t ...

Manipulating active classes on different divisions with JQuery

I've completed this code so far: <div class="yearly-archive"> <p> YEAR - <span class="archive-year year-active"> 2014 </span> / <span class="archive-year"> 2013 </span> / <span class="archi ...

CSS: The hiding should only apply to specific sections of my menu

I'm having trouble hiding a portion of my menu. Whenever I use the display:none property, the entire menu disappears. I have assigned IDs to different elements to distinguish them, so I'm not sure why this is happening. Here's the code snipp ...

How come I am able to reference a component in styled-components while others cannot?

When using styled-components, you can reference another React component using the syntax ${Label}. However, I have noticed that it only works with certain components and not others. Despite reading the documentation at , I encountered an issue when trying ...

Customizing the positioning of a dropdown menu to the right in WordPress

I've encountered an issue with the native dropdown menu in Wordpress. The menu is dropping elements on the left side, but I need them to appear on the right side. Below is the CSS code I'm currently using: div#navigation { float: left; p ...

Chrome DevTools automatically translates all HEX color codes into their corresponding RGB values

Chrome DevTools now automatically converts all HEX colors to RGB values, even if the color is set in CSS or using DevTools. Is there a way to prevent this conversion? I know about the method of holding down Shift + clicking the color icon to convert color ...

Arrange two divs next to each other on the page: the div on the right can be scrolled through, while the div on the

I am attempting to create a layout similar to this https://i.sstatic.net/Y1FCp.png My goal is to: Have two main divs positioned side by side inside a wrapper div Ensure the wrapper expands to fill 100% of the browser size The left div (banner) should b ...

Parsing JSON multiple times to extract data on the top 10 games from Twitch's API

Hey there! I have a little coding challenge for you. The current code below is only fetching the first channel from the JSON. My goal is to fetch all the streams and display them in a grid format with three streams per row. Can you help me achieve this? (B ...

Enhancing User Experience with Interactive React Hover Effects

Hello, I have tried various combinations but still cannot get the desired background color to display when hovering over my <p> element. I am looking for a CSS-only solution and not interested in using JavaScript hover events. I would appreciate it ...

What is the best way to adjust the size of table columns dynamically to accommodate content, align them to the left, and set a maximum

I came across many examples that are somewhat close to what I need, but I require more control over the table design. I want to prevent text wrapping, display ellipsis, and provide a tooltip when hovering over the content to view it completely. This proje ...

Is there a way to modify the font color in Chrome when the input autofill feature is active?

I tried adding the css code below: input:-webkit-autofill{ color:white; } Unfortunately, it didn't have any effect. If anyone can assist me with this issue, I would greatly appreciate it. Thank you. ...

What is the best way to create flexible Bootstrap 3 columns with varying heights?

I'm struggling to remove space and create a flex column .col-md-5 with varying heights. Despite trying various solutions found on Google, I still can't get it to function properly. The current outcome is not as expected. https://i.sstatic.net/fc ...

Is it possible to create a custom dropdown in react with both radio buttons and checkboxes?

I've been searching for a custom third-party library to meet a specific need I have. Here's a snapshot of the dropdown in question. I looked into https://www.npmjs.com/package/multiselect-react-dropdown but it doesn't cover this particular s ...

Incorporate JavaScript to enable the transfer of text from one textarea to another upon clicking a button, while simultaneously clearing the original textarea

After working on the provided code, I have managed to create a functionality where text from one textarea is copied to another textarea when a button is clicked using JavaScript. <head> <script type="text/javascript"> function displayOut(){ ...

How can I align bullets in an unordered list with the left margin of the heading above?

Is there a way to line up the bullets with the left margin of the text above the list? Here's an example: <html> <style> ul.p1 {padding-left: 40px} ul.p2 {padding-left: 0px} </style> <body> <h2&g ...