Is there a way to consistently maintain a specific column size in Bootstrap?

I'm currently working on a website and running into an issue that I can't seem to resolve. The problem lies in the layout of multiple cards with information; they are arranged in columns and when there are more than 4 columns, they switch to a new row. However, the challenge arises when the number of cards is not divisible by 4, resulting in less than 4 columns in a row. This inconsistency in layout is due to my use of the col-sm Bootstrap class, which offers responsiveness but doesn't handle cases with less than 4 columns per row effectively. I attempted using col-sm-3 instead, but it compromised the website's overall responsiveness. (Refer to the screenshots) Additionally, I'm utilizing Razor views to combine C# and HTML in my project.

View when using col-sm: View col-sm

Desired layout: View col-sm-3

Issue with col-sm-3: Problem with col-sm-3

Here is the code for displaying the cards:

.vehicule-card {
    border-radius: 10px;
    border: solid 4px #2F3136;
    background-color: #333333;
}

.vehicule-cardbody {
    height: 5em;
}

.vehicule-name {
    background-color: #392A49;
    color: #B8BABD;
    width: 10em;
    margin: auto;
    padding: 3px;
    border-radius: 10px;
    border: solid 4px #2F3136;
}

.vehicule-image {
    border-radius: 10px;
    border: solid 4px #2F3136;
}
<div class="container-fluid">
        <div class="headline">COMPACT CARS</div>
        <div class="row">
            @foreach (var car in Model.cars)
            {
                if (counter != 0 && counter % 4 == 0)
                {
                @:</div>
                @:<div class="row">
                }

                    <div class="col-sm-3">
                     <div class="card p-3 m-2 box-shadow vehicule-card">
                      <img class="card-img-top vehicule-image" src="@car.ImgPath" alt="Card image cap">
                            <div class="card-body vehicule-cardbody">
                                <h2 class="vehicule-name">@car.Name</h2>
                            </div>
                     </div>
                    </div>

                counter++;
            }

        </div>




    </div>

Please note that attempting to run the code will not work as it's a Razor view.

Thank you

Answer №1

To achieve the desired layout, you can utilize Bootstrap’s grid system.

If the image size you need exceeds what Bootstrap 4 offers with container-xl (Bootstrap 5 has container-xxl), you can define your own xxl container with a maximum width of 1440px, which can be adjusted as needed.

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

<style>
body {
    background-color: #212121;
    color: #fff;
}

h1 {
    color: #B8BABD;
}

hr {
    width: 10%;
    border-top: 5px solid #392A49;
} 

.container-xxl {
    width: 100%;
    padding-right: 15px;
    padding-left: 15px;
    margin-right: auto;
    margin-left: auto;
}

@media (min-width:1500px) {
    .container-xxl {
        max-width: 1440px;

    }
}

.vehicule-card {
    border-radius: 10px;
    border: solid 4px #2F3136;
    background-color: #333333;
}

.vehicule-cardbody {
    height: 5em;
}

.vehicule-name {
    background-color: #392A49;
    color: #B8BABD;
    width: 100%;
    text-align: center;
    padding: 3px;
    border-radius: 10px;
    border: solid 4px #2F3136;
}

.vehicule-image {
    border-radius: 10px;
    border: solid 4px #2F3136;
}
</style>

<div class="container-xxl">
    <h1 class="text-center mt-5">COMPACT CARS</h1>
    <hr>
    <div class="row row-cols-1 row-cols-sm-2 row-cols-md-3 row-cols-lg-4">
        <div class="col mb-3">
            <div class="card p-3 m-2 box-shadow vehicule-card">
                <img class="card-img-top vehicule-image" src="https://via.placeholder.com/192x108/FF8F8F/000000.png?text=Blista" alt="Card image cap">
                <div class="card-body px-0 vehicule-cardbody">
                    <h2 class="vehicule-name">Blista</h2>
                </div>
            </div>
        </div>
        <div class="col mb-3">
            <div class="card p-3 m-2 box-shadow vehicule-card">
                <img class="card-img-top vehicule-image" src="https://via.placeholder.com/192x108/FFEE8F/000000.png?text=Dilettante" alt="Card image cap">
                <div class="card-body px-0 vehicule-cardbody">
                    <h2 class="vehicule-name">Dilettante</h2>
                </div>
            </div>
        </div>
        <div class="col mb-3">
            <div class="card p-3 m-2 box-shadow vehicule-card">
                <img class="card-img-top vehicule-image" src="https://via.placeholder.com/192x108/B0FF8F/000000.png?text=Issi" alt="Card image cap">
                <div class="card-body px-0 vehicule-cardbody">
                    <h2 class="vehicule-name">Issi</h2>
                </div>
            </div>
        </div>
        <div class="col mb-3">
            <div class="card p-3 m-2 box-shadow vehicule-card">
                <img class="card-img-top vehicule-image" src="https://via.placeholder.com/192x108/8FD2FF/000000.png?text=Panto" alt="Card image cap">
                <div class="card-body px-0 vehicule-cardbody">
                    <h2 class="vehicule-name">Panto</h2>
                </div>
            </div>
        </div>
        <div class="col mb-3">
            <div class="card p-3 m-2 box-shadow vehicule-card">
                <img class="card-img-top vehicule-image" src="https://via.placeholder.com/192x108/AB8FFF/000000.png?text=Prairie" alt="Card image cap">
                <div class="card-body px-0 vehicule-cardbody">
                    <h2 class="vehicule-name">Prairie</h2>
                </div>
            </div>
        </div>
        <div class="col mb-3">
            <div class="card p-3 m-2 box-shadow vehicule-card">
                <img class="card-img-top vehicule-image" src="https://via.placeholder.com/192x108/FF8FF4/000000.png?text=Rhapsody" alt="Card image cap">
                <div class="card-body px-0 vehicule-cardbody">
                    <h2 class="vehicule-name">Rhapsody</h2>
                </div>
            </div>
        </div>
    </div>
</div>

While my code example uses plain HTML, you can adapt it to work with Razor for your specific needs.

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

Fixing blurry text on canvas caused by Arbor.js mouse event issues

Currently, I am utilizing arborjs in my project. The text within the canvas is created using fillText in html5. While everything functions correctly on a Retina display MacBook, the text appears blurry. To address this, I applied the following solution: v ...

Is there a way to choose columns 2 to 6 of the row that is currently selected

I am looking to create a hover effect for columns 1 through 7 of a row where, when the user hovers over any column within that range, certain styles are applied to all columns in the row except for columns 1 and 7. This means that the background color shou ...

How to centrally align text in list items using Bootstrap 5

Incorporating Bootstrap 5 into my project, I am facing a challenge with vertically aligning text in an li element. Despite trying various techniques mentioned in the Bootstrap documentation for middle/center alignment, none of them seem to be effective. I ...

Show various shapes based on the values retrieved from MYSQL database

I am just starting to learn MYSQL and PHP, and I am currently working on creating a Course registration application using xampp. This application is designed to collect user data and store it in a MYSQL database. Within my MYSQL table, I have user details ...

No input value provided

I can't figure out what's going wrong. In other js files, this code works fine. Here is my HTML code: <table class='table'> <tr> <th>Event</th><td><input class='for ...

What is the best way to store items in localStorage within Angular version 4.4.6?

I have been working on implementing a basic authentication system in Angular 4.4 with MongoDB as the backend database. login.component.ts import { Component, OnInit } from '@angular/core'; import { AuthService } from 'app/services/auth.ser ...

The getImageData function is returning undefined RGB values

I am trying to create a feature where by clicking on an image, I can retrieve the RGB values of that specific pixel. Below is the code snippet I have implemented: <html> <body> <canvas id="kartina" width="500" height="500"></canvas&g ...

Is it possible to create this layout using Bootstrap 5? I want to stack spans, with one inside the container and one outside

Trying to achieve a specific design using Bootstrap 5.3. https://i.sstatic.net/STifm.jpg This design should be placed over the showcase, with the following code for the showcase displayed below: <div class="showcase d-flex flex-column"&g ...

Ways to trigger a click event on a dynamically added class utilizing $(this);

I am attempting to retrieve the text when clicking on dynamically appended HTML elements. After some research, I came across the following code snippet: $(document).on('click', '.myClass', function (). However, it seems to work only f ...

Customized icons for Contact Form 7 on your Wordpress website

Currently, I am in the process of customizing a contact form by incorporating placeholder icons and text using the 'Contact Form 7' plugin within Wordpress. This particular contact form is situated on a website that I am constructing with the &ap ...

What is the proper way to assign a class name to an animation state in Angular 2/4?

I am currently working with Angular Animations using version 4.1.3 Check out the code snippet below: @Component({ selector : 'my-fader', animations: [ trigger('visibilityChanged', [ state('true' , style({ opaci ...

Creating an Innovative Grid Design with Varying Column Widths for Each Row

Seeking advice on achieving a specific layout using HTML. I have attempted to use HTML tables but struggled with creating rows with varying columns. Does anyone have recommendations for grid tools that may be more helpful? Thank you ...

Successfully created a WCF AJAX enabled service, however, encountering a 404 error when trying to call

This is my inaugural WCF service endeavor, so please bear with me. :) I have a preexisting ASP.Net website that has been functioning adequately thus far. Utilizing VS 2017, I integrated a new WCF Ajax-enabled service. VS generated the .svc and .cs files a ...

Personalizing the design of Bootstrap

As a beginner in working with Bootstrap, I am currently focused on creating an index html page for an online shop. For reference, here is an example - https://jsfiddle.net/a393wtng/1/ Upon resizing the output frame, it becomes apparent that 4 products can ...

Tips for transferring input field values through the href attribute in HTML?

How can I pass an integer value from a link to an input field's value? Imagine example.com is a webpage with one input field. I would like to achieve something like this: Click here 1 Click here 2 If the user clicks on click here 1, then ...

Text not perfectly centered on the page

I'm working on a layout with 4 columns, and I want the first 3 columns to display text vertically. However, I'm having trouble aligning the text in the center of the column. Even after using justify-content and align-items properties, the text is ...

Issue with CSS: Dropdown menu is hidden behind carousel while hovering

I'm struggling with adjusting the position of my dropdown list. It keeps hiding behind the carousel (slider). When I set the position of the carousel section to absolute, it causes the navbar to become transparent and the images from the carousel show ...

I'm using Selenium XPATH or CSS to identify and select a checkbox within an HTML table based on the specified text. As a result, I have successfully

I am trying to select a checkbox from an HTML table with multiple columns. Specifically, I want to choose the checkbox that corresponds to the "CLEAN_AUDIT" option for the Name column. However, my current XPATH is selecting 4 checkboxes labeled CLEAN_AUDIT ...

Enhanced Bootstrap 4 button featuring custom design with text and image aligned on the right side, optimized for flexbox layout

I am looking to design a button-like div that features a flag image alongside the abbreviation of a country. Although I have some code in mind, it doesn't follow Bootstrap's guidelines and I am struggling to adapt it accordingly. <div cla ...

Populating Dropdown on Page Load

Is there a way to prevent my dropdown menu from resetting to the first index after pressing a button? I am populating the dropdown with queries on page load, but every time I select a value and press a button, it goes back to the initial index. Here is som ...