Modify the layout of columns in Bootstrap dynamically without compromising on responsiveness

On my webpage, I have a section where I display cards of specific sizes (250*250). I want to show a maximum of 4 cards per row, stacking them on smaller viewports. Currently, the layout works fine with 4 or more cards, with the excess automatically moving to the next line. However, the issue arises when there are fewer than 4 cards (which can happen since the cards are generated at runtime from a database in a Rails application).

I am looking for a solution where the cards align themselves within the row while maintaining their size if there are less than 4 cards by adding more margin to the right and left.

For 4 or more cards, here is the design:

https://i.sstatic.net/eq4aX.png

And for less than 4 cards, like 2 cards, the design should look like this:

https://i.sstatic.net/v06P3.png

In terms of alignment issues when only 2 cards are present, it currently looks like this: https://i.sstatic.net/oFoid.png

And for 5 cards, the layout appears as follows: https://i.sstatic.net/OLlei.png

The adjustment in margins is something that can be handled.


Question: How can I ensure that there are always 4 fixed-size columns on large screens, which stack up on smaller devices, dynamically adjusting alignment if there are less than 4 cards in a row?

Here is what has been implemented so far:

HTML

<div class="feature-wrapper pt-5 pb-5 mt-5 mt-lg-0 h-100 ">

<div class="d-flex align-items-center h-100 justify-content-center ">

    <div class="row">

        <div class="my-card col-sm-12 col-lg-3 text-center mb-3 mb-md-0 rounded border">
            <i class="far fa-plus-square fa-10x"></i>
        </div>

        <div class="my-card col-sm-12 col-lg-3 text-center mb-3 mb-md-0 rounded border">
            <i class="fas fa-arrow-alt-circle-down fa-10x"></i>
        </div>
         <div class="my-card col-sm-12 col-lg-3 text-center mb-3 mb-md-0 rounded border">
            <i class="fas fa-arrow-alt-circle-down fa-10x"></i>
        </div>
         <div class="my-card col-sm-12 col-lg-3 text-center mb-3 mb-md-0 rounded border">
            <i class="fas fa-arrow-alt-circle-down fa-10x"></i>
        </div>
    </div>
</div>

CSS

body,html{
  height:100%;
}
.my-card{
      height:250px;
      width:250px;
      background-color:red;
      text-align:center;
}

Answer №1

If you're aiming for a specific layout with fixed size cards, achieving it using the Bootstrap grid might be challenging. However, by incorporating some custom CSS, you can leverage flexbox rows to adjust according to the card width.

Check out this code example:

.flex-shrink {
  flex:0 0 1000px;
}

@media (max-width:768px) {
  .flex-shrink {
    flex:0 0 500px;
  }
}

If you're open to some flexibility in the card sizes (not necessarily sticking to 250px), then the Bootstrap grid can still be utilized effectively. See demo here:

Another option to explore is the auto-layout grid columns, which dynamically adjust to the card sizes but may not result in exact 4 cards per row and 2 cards on mobile. Here's an example:

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

Automatically open the first panel of the Jquery Accordion

Is there a way to automatically have the first panel in my JQuery accordion open when all panels are initially closed? Here is the HTML code for my accordion: <div class="accordionx"> <div class="acitemx"> <h3>First Panel</h3> ...

Enhance Your Text Areas with Vue.js Filtering

Currently, I am working with a textarea that has v-model: <textarea v-model="text"></textarea> I am wondering how to filter this textarea in Vue. My goal is to prevent the inclusion of these HTML quotes: &amp;amp;#039;id&amp;amp;#039 ...

Step-by-step guide on downloading an image in HTML with the click of a button

I have a photo gallery on my website created using PHP and HTML. I am looking to add a functionality where users can click on a button to download the image that is currently set as a background of a specific div element in the gallery. The download button ...

Scroll on sidebar that overflows, rather than the page behind

My goal is to have a fixed background page while allowing the sidebar to scroll independently. Both elements are too large for the screen, so I want the page to scroll normally until the navbar overflows in height and becomes scrollable. Check out the exam ...

Managing events with classes

I have multiple divs with the same class. When one of these divs is clicked, I want to change the display of the other divs to 'block'. Currently, I am using inline JavaScript for this functionality, but I would like to achieve it without inline ...

Restricting the number of times a user can click on

I am currently displaying a table with data obtained from a database query. The structure of the table is outlined below: <table id="dt-inventory-list" class="table table-responsive"> <thead> <tr> <th>Field ...

Resetting the selected options in AngularJS dropdown lists

Utilizing ng-repeat in my HTML code to iterate over a JavaScript array and displaying it within a selection. I am trying to achieve the functionality of clearing all selected data from these dropdown lists when a button is clicked. Snippet of HTML: <d ...

Adding a background color with a background image layered underneath it in CSS - here's how!

Looking to set a background color for a div, with a background image on the bottom of the same div. However, I need the background color to stop once the background image begins. Check out the example image below: ...

Looking to target an element using a cssSelector. What is the best way to achieve this?

Below are the CSS Selector codes I am using: driver.findElement(By.cssSelector("button[class='btn-link'][data-sugg-technik='append_numbers']")).click(); driver.findElement(By.cssSelector("button[class='btn-link'][data-sugg- ...

Overlap one element entirely with another

Currently, I am working on a way for an element called overlayElement to completely cover another element known as originalElement. The overlayElement is an iFrame, but that detail may not be significant in this scenario. My goal is to ensure that the over ...

What is the best way to connect internal files within Angular?

I am encountering an issue when trying to connect my login page from the navbar. Here is the code snippet for the navbar that I have: <div class="navbar-container"> <ul id="slide-out" class="side-nav center-align"> <li> <d ...

Enhancing Website Design with Image Borders

I'm currently designing a website for a Media Agency and I'm looking to add a Frame-style border that gives the impression of being placed inside a television. The Television Image I have is a PNG file with a solid outer frame and a transparent i ...

Ways to eliminate the unusual dashed space in ReactJS using Bootstrap

While developing an app with NextJS and Bootstrap, I noticed a strange dashed gap at the top of the screen in the elements tab. Despite checking for margin or padding-top properties on any element, there doesn't seem to be a clear cause for this issue ...

Selecting radio buttons across multiple div classes

I've been struggling to programmatically select specific radio buttons on a webpage. My goal is to automatically choose the second option in each group of radio buttons, but I'm getting lost in the syntax. Unlike most examples I've found on ...

The functionality of CSS scroll snap does not seem to be compatible with the CSS Grid

When utilizing CSS scroll snap with Flexbox, the snapping functionality works adequately: * { box-sizing: border-box; margin: 0; padding: 0; } .slider { font-family: sans-serif; scroll-snap-type: mandatory; scroll-snap-points-y: repeat(100vw ...

Steps to eliminate the x icon from the text box that appears automatically in Internet Explorer

I'm currently working with a UI5 text box that includes a built-in option for clearing the text using a 'X' button. However, I've noticed that in Internet Explorer, an additional default cross mark is being added alongside the UI5 cros ...

Unable to transfer card from the top position in the screen

Utilizing Tailwind (flowbite) for the frontend of my project, I encountered an issue where the Navbar was overlapping the Card. I attempted using mt-8 to resolve the problem, but it did not yield significant changes. What adjustments should I make to achi ...

mobile-friendly sticky footer

Currently working on a website project utilizing Bootstrap 3 (http://www.patrickmanser.ch/fnws) and I am trying to implement a sticky footer. After adapting the code snippets from Bootstrap 3 examples according to my specifications, everything seems to fun ...

When trying to include an external class that extends Pane in a main class in JavaFX, simply adding it may not achieve the

My primary class is as follows: public class Digital_Analog_Clock_Beta_1 extends Application { @Override public void start(Stage primaryStage) { double outerBoxWidth = 500, outerBoxHeight = outerBoxWidth / 2.5; Rectangle outerB ...

Distance between cursor and the conclusion of the text (autofocus)

I discovered a method to automatically position the cursor at the end of a string using autofocus: <input name="adtitle" type="text" id="adtitle" value="Some value" autofocus="" onfocus="this.setSelectionRange(this.value.length,this.value.length);"> ...