Uneven quantities of elements in a Bootstrap grid

My website has a grid that is populated from a CMS, but I am unsure if the number of items will be odd or even. The design specifies 3 columns on desktop and 2 columns on mobile devices, with the last item centered if there is only one.

I currently have to programmatically check the number of items and use col-xs-6 for the last item in an even set, or col-xs-12 for the last item in an odd set.

<div class="row partners">
    <div class="container">
        <h1>Our Partners.</h1>
        <div class="col-md-4 col-xs-6">
            <div class="headshot-wrapper">
                <p><img src="images/headshot.jpg" /></p>
            </div>
            <h2>Partnername</h2>
        </div>
        <div class="col-md-4 col-xs-6">
            <div class="headshot-wrapper">
                <p><img src="images/headshot.jpg" /></p>
            </div>
            <h2>Partnername</h2>
        </div>
        <div class="col-md-4 col-xs-12">
            <div class="headshot-wrapper">
                <p><img src="images/headshot.jpg" /></p>
            </div>
            <h2>Partnername</h2>
        </div>
    </div>
</div>

I'm wondering if there's a way to achieve this layout without using programming logic. It would be great if Bootstrap had "greedy" columns that automatically filled out remaining spaces in the grid.

Answer №1

When trying to center floats, it's often a struggle. A workaround is to use inline-block instead.

To achieve this, add a "centered" class to the row:

<div class="row partners centered"> 

Then adjust the column behavior as follows:

.centered {
    text-align: center;
    font-size: 0;
}
.centered > div {
    float: none;
    display: inline-block;
    text-align: center;
}

Here's an example: https://jsfiddle.net/d2gcf39w/


Additionally, remember that in Bootstrap hierarchy, it should follow Container > Row > Col. Therefore, make sure to rearrange the first two lines like this:

<div class="container">
    <div class="row partners centered">
        <h1>Our Partners.</h1>
       ...
    </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

The second entry is not being styled by CSS, is there an issue with the code in this section?

I am facing a challenge while trying to set the same background image for two div elements: .abc { background: url('images/autogen.png') no-repeat 0px -133px; width: 256px; height: 256px; }; .def { background: url('images/autogen.png' ...

The functionality of AngularJS ng-click is disrupted by the presence of {{$index}}

Having an issue with AngularJS where using $index breaks the ng-click function. This issue arises within a div utilizing ng-repeat, the repeat code is functioning correctly... <a class="accordion-toggle" data-toggle="collapse" data-parent="#acc{{$inde ...

What is the best way to set up a clickable image without making the entire div clickable?

Is it possible to make just a specific image clickable within a div without making the entire div clickable? I have an image inside an anchor tag and a surrounding div that is clickable. The issue is that the entire space around the first image, .home, is ...

Highlighting the active nav-item in Bootstrap-5 is proving to be a challenge

I'm having trouble with the Bootstrap navbar. I want to change the color of the active nav-link, but it's not working as expected. I am currently using Bootstrap-5. Below is the CSS code I have: .nav-link { color: #666777; font-weight: 4 ...

Creating a dynamic MPTT structure with expand/collapse functionality in a Django template

I am looking for a way to display my MPTT model as a tree with dropdown capability (open/close nodes with children) and buttons that can expand/collapse all nodes in the tree with just one click. I have searched for examples, but the best I could find is ...

Issue with Bootstrap and Django navbar not expanding when toggled

I'm having some issues with the collapsible navbar component in Bootstrap. I suspect that I might not be importing the right libraries. Below is my base.html: <!doctype HTML> <html lang="en"> <head> <!-- Required met ...

Styling Divs Beside Each Other Using CSS

I'm attempting to display the "left" and "right" divs side-by-side in the following example. However, they are not appearing next to each other as expected. Can someone point out my mistake? Thank you. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML ...

How can I use the active class in an ASP.NET application with a Twitter Bootstrap navigation bar

Currently, I am utilizing Twitter bootstrap within an asp.net project and the snippet below showcases my navbar code: <div class="navbar"> <div class="navbar-inner"> <div class="container"> ...

Choose the first and last div element in the selection

Is there a way to target the first and fourth div elements using the :nth-child selector for styling purposes? .main{ width:460px; height:240px; } .box{ width:100px; height:100px; background:red; float:left; margin:10px; } /*I tried */ . ...

Problem with the hover functionality of the <li> tag

I'm having an issue with applying the hover property to the li element in the sidebar. The hover effect works, but the icon tag is not showing the hover effect. I want the icon to also show the hover effect along with the li's hover effect. Here ...

Using Module.Controller() syntax within AngularJS UI Bootstrap 0.6.0 Modal for enhanced functionality

When it comes to defining the controller for my modal instance, I am looking to make a change. Instead of using the traditional method like this: var ModalInstanceCtrl = function ($scope, $modalInstance, items) { /*ctrl-code-here*/ }; I would prefer to u ...

Create table cells made of material that have widths determined by predefined lengths

First, let me showcase my code. <ng-container matColumnDef="position"> <th mat-header-cell *matHeaderCellDef> No. </th> <td mat-cell *matCellDef="let element"> {{element.position}} </td> </ng-conta ...

Ways to enable smooth scrolling feature in Safari

I attempted to implement smoothscroll using a polyfill by adding the following code to the bottom of my body tag: <script src="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e99a8486869d819a8a9b868585c ...

Unraveling CSS inheritance: A guide to overriding inherited styles

I need to make some adjustments to a WordPress template. One of the changes is to set the background color of an element to transparent. The wrapper div, which has the background color applied to it, contains many nested child divs that all inherit that co ...

Designing a structured layout with div elements resembling tables within a Bootstrap 4 card

I'm struggling with formatting this card. My goal is to create two columns, one with the class col-xs-3 and the other with the class col-xs-9. The titles in the col-xs-3 column should align with the corresponding titles in the col-xs-9 column. Unfort ...

Steps for implementing a Toggle Navigation Bar in CSS

I'm looking to implement a show/hide navigation menu similar to the one showcased in this inspiration source: Code Snippet (HTML) <div id="menus"> <nav id="nav"> <ul> <li><a href="#">HOME</a></li> <li& ...

Question regarding CSS positioning: How do you place an asterisk inside a display block element?

The platform I'm working with is generating code like the one below for forms: <div class="sf-fieldWrp"> <label for="Textbox-3">Contact Name</label> <input type="text" value="" requi ...

Steps to conceal an accordion upon loading the page and reveal it only when clicking on a specific element

Below is the code I am using to display an accordion in a Fancybox popup. However, I do not want the accordion to be visible on page load. If I hide it, the content inside also gets hidden when showing the accordion in the popup. When user clicks on Click ...

Tips for applying a CSS wrapper to an element, not just text

I am working with a group of span elements within a td that has a set width. My goal is to wrap the span elements onto new lines without breaking up the text inside them. For example, if I have: <td> <span>bind</span> <span&g ...

Perform a toggle action on the first row when clicking within the current row using Jquery

I've been grappling with the idea of creating a function that can display and hide a comment field when a button is clicked. The challenge here is that there are multiple line items with their own comment boxes. I want to find a way to achieve this wi ...