What is the best way to adjust the sizing of Bootstrap columns according to different screen sizes?

I am looking to display 6 items on a webpage...

When viewing on a cell phone (xs), I want two rows of three items to display. On larger screen sizes, I would like to see all six items on the same row.

I attempted to achieve this using the following code:

<div class='row'>
  <div class='col-xs-4 col-sm-2'>1</div>
  <div class='col-xs-4 col-sm-2'>2</div>
  <div class='col-xs-4 col-sm-2'>3</div>
  <div class='col-xs-4 col-sm-2'>4</div>
  <div class='col-xs-4 col-sm-2'>5</div>
  <div class='col-xs-4 col-sm-2'>6</div>
</div>

While this setup works well on large screens displaying all six items in a single row, it shows as six separate rows when viewed on xs screens (phones).

My desired layout is:

1 2 3 4 5 6

on larger screens and

1 2 3

4 5 6

on xs screens.

Do you have any suggestions or solutions? Thank you!

Answer №1

col-xs- was originally used in Bootstrap-3. However, for Bootstrap-4 and smaller screens, it is recommended to use col-4, so your code will appear as follows:

<div class='row'>
  <div class='col-4 col-sm-2'>1</div>
  <div class='col-4 col-sm-2'>2</div>
  <div class='col-4 col-sm-2'>3</div>
  <div class='col-4 col-sm-2'>4</div>
  <div class='col-4 col-sm-2'>5</div>
  <div class='col-4 col-sm-2'>6</div>
</div>

This setup will create three columns on a smartphone and six columns on other devices.

Enhanced spacing between elements:

If you desire additional space between the elements, consider placing another column within your initial column and then adding your content in that column. By default, the gutter has a width of 15px, resulting in a total spacing of 30px between columns. You can customize this spacing by referring to this answer:

To provide an illustration, I have adjusted the initial solution to include a 5px gutter (totaling 10px) and added spacing between elements on smartphones. Additionally, there is a block below the elements to demonstrate close alignment if desired. Remove the mb-n3 margin on the row if no bottom space is required.

<link href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f7d70706b6c6b6b76b65f5d247b47571054">[email protected]</a>/dist/css/bootstrap.min.css" rel="stylesheet"/>

<style>
    .row.gutter {
        margin-left: -5px;
        margin-right: -5px
    }

    [class*="col-"].gutter {
        padding-left: 5px;
        padding-right: 5px;
    }
</style>

<div class="container mt-5">
    <div class='row mb-n3 gutter'>
        <div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
            <div class="col bg-primary">1</div>
        </div>
        <div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
            <div class="col bg-secondary">2</div>
        </div>
        <div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
            <div class="col bg-success">3</div>
        </div>
        <div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
            <div class="col bg-danger">4</div>
        </div>
        <div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
            <div class="col bg-warning">5</div>
        </div>
        <div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
            <div class="col bg-info">6</div>
        </div>
    </div>
</div>  
<div class="container-fluid bg-dark text-light" style="height: 200px;">
    <p>A filler div to show that there can be a space between the rows of elements on a small screen, but no space below the row, if you don&rsquo;t want one.</p>
</div>

Answer №2

Replace col-xs-x with col-x. The former is no longer supported in Bootstrap 4.

<div class="row">
    <div class="col-4 col-sm-2">1</div>
    <div class="col-4 col-sm-2">2</div>
    <div class="col-4 col-sm-2">3</div>
    <div class="col-4 col-sm-2">4</div>
    <div class="col-4 col-sm-2">5</div>
    <div class="col-4 col-sm-2">6</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

Wrapping text around a container element

Is there a way to make the text wrap around a div when you want the div positioned at the bottom of a container? I've been able to align the text properly with the div when it's on top, but when I attempt to move it to the bottom of the page, th ...

Creating unsightly class names using Node.js

Is it possible to automatically create random and unattractive class names? For example, is there a tool or plugin that can substitute the .top-header class with something like .a9ev within my CSS code? It would also be ideal if the class name in the HTML ...

Unable to use the ordered tag correctly in typing

Please review the HTML and CSS Code provided below: HTML : <ol> <li>Test</li> <li> <ol> <li>Sub-Chapter</li> <li>Sub-Chapter</li> </ol> < ...

The function will not be triggered when the form is submitted

I've set up this form to send data to the server-side. It's built in HTML/CSS with AngularJS as well. I made sure that the button is placed inside the form, a common mistake that can cause issues. However, despite my efforts, the function "onAddE ...

Sass - Trouble with Media Queries in Jekyll

My website was created using Jekyll and Sass. I attempted to make the site responsive, but for some reason, the media queries are not affecting the div class. This is my style.sass snippet: .img-link width: calc(32% - 14px) border: 2px solid whit ...

Utilizing Modernizr for detecting support of background-clip:text functionality

Recently, I decided to utilize Modernizr in order to check for support of the css property background-clip: text. After some research, I came across this page:https://github.com/Modernizr/Modernizr/issues/199 I then added the following code to my website ...

Problem with the placement of Google autocomplete dropdown

I'm currently utilizing the NgxAutocomPlace module within my Angular application. This module interacts with the Google Autocomplete API by generating a .pac-container to display autocomplete suggestions. However, I'm facing an issue where on mo ...

Create an HTML element that has a 'floating' effect

Is it possible to have an HTML element like a span or div on the page that occupies no space? I want to be able to toggle its visibility without affecting the layout, such as having a label appear next to a table row when hovered over without taking up e ...

Make SVG Path (Vector Graphic) Simplification easier

Provided Input: <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="800pt" height="600pt" viewBox="0 0 800 600"> <g enable-backgrou ...

The background is set and the vertical scrolling overflow is enabled

My website has an image set as the background, with a fixed position and overflow-y to allow scrolling. However, I have encountered an issue where the height of the image is causing problems on certain displays and resolutions. To temporarily fix this pro ...

Implementing text wrapping within input elements for optimal print.css layout

Can you help me with a print.css issue I'm experiencing? I am currently working on my print stylesheet and I've encountered a challenge where I need to word wrap input fields to ensure all the text is displayed correctly. Despite trying various ...

What is the method for altering the track color on a range slider?

Struggling to change the track color of a range slider using CSS in Chrome? Look no further, as I found a solution using jQuery (link). However, after implementing the solution, the expected output is not achieved. jQuery: $('.text-size-slider .slid ...

What's the best way to position an ion-label at the top of the stack

I'm having trouble creating an input label similar to the new Google style. However, when the label moves up, it gets cut in the middle as shown here. Despite trying to adjust the margin, padding, and Z-index, none of these solutions have resolved my ...

"Perpetual access to color selection through the input type

Can you assist me? I am currently using the input type color and looking to have the color picker always visible without requiring a click. Is there a way to achieve this through html, css, javascript/jquery, or any other method? Thank you in advance. I ...

Personalize a bootstrap theme specifically for the mobile interface

My current template, based on Bootstrap, displays a text along with a button and an image. Using a checkbox, you can choose whether the image appears to the right or left of the text. One issue I'm facing is that in the mobile view, the text and butt ...

Solution for fixing the position of a div scrollbar under the browser scrollbar

When working on my web app, I faced an issue with a fixed div modal that takes up the entire screen. The problem is that the scrollbar for this modal is not visible, only the body scrollbar can be seen. It seems like the fixed div's scrollbar is posit ...

What is the best way to update the color of a header in an HTML document

My HTML and CSS code is causing the h1 title to appear in blue instead of red. I am unsure why this is happening and how to fix it. Any help would be greatly appreciated. <!doctype html> <h1>les planètes du système solaire <h1> < ...

Showing the blank lines from the uploaded information within a <p> tag

Here is some text with empty lines (retrieved from a Database) inside a paragraph element: <p className={styles.biography}>{loaderData.cast.biography} </p> .biography { font-size: 1.4rem; color: #333333; text-align: justify; } U ...

Tips for generating more compact CSS files to simplify your development workflow

At the moment, I have included two CSS files within my <head> tags: <link rel="stylesheet" href="/stylesheets/init.css"> <link rel="stylesheet" href="/stylesheets/layout.css"> The init.css file is used for resetting elements and definin ...

concealing a div with smarty

I am working with a Smarty template that includes company IDs for multiple companies. My goal is to use an if statement to display a Google map in a div if the company ID matches a certain number, and hide the div if it does not match that number. I have ...