Having trouble defining the width of cards in Bootstrap 4 using precise percentage values

Take a look at this example. I believe I should be able to manually set the width of card elements using CSS. Despite my efforts, the width seems to be ignored and the final computed value is not what I expected. For instance, on larger screens, all 6 cards are displayed in a single row instead of following the specified width: 25%. On smaller screens, the cards do not align side by side as intended.

<div class="container">
    <div class="card-deck">
        <div class="card mb-4">
            <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
            <div class="card-body">
                <h4 class="card-title">1 Card title</h4>
                <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
            </div>
        </div>
        [other card elements...]
    </div>

.card {
    width: 50%;
}

@media (min-width: 768px) {
    .card {
        width: 33%;
    }
}

@media (min-width: 1200px) {
    .card {
        width: 25%;
    }
}

Answer №1

When dealing with the width property for .card-deck .card, it's important to note that simply using width won't work as expected. Instead, you should utilize both flex-basis and max-width for optimal results. Below is the code snippet:

.card {
  flex-basis: 100%;
  flex-grow: 0;
  flex-shrink: 0;
  max-width: 100%;
}
@media (min-width: 576px){
  .card {
    flex-basis: calc(50% - 30px);
    flex-grow: 0;
    flex-shrink: 0;
    max-width: calc(50% - 30px);
  } 
}

@media (min-width: 768px) {
  .card {
    flex-basis: calc(33.3333% - 30px);
    max-width: calc(33.3333% - 30px);
  }
}

@media (min-width: 1200px) {
  .card {
    flex-basis: calc(25% - 30px);
    max-width: calc(25% - 30px);
  }
}

Bootstrap applies the css properties

margin-left: 15px;margin-right: 15px;
to .card-deck .card, causing the cards not to go full width. To resolve this issue, you need to eliminate the margin width from the total card width.

You can find an example on CodePen.

If you try to mimic the design without modification, it may not look good compared to the default Bootstrap card design. Consider altering the html structure to achieve your desired layout without resorting to custom CSS properties.

Here is the link to the altered CodePen.

Edited Version

Bootstrap removes margin from the .card class when the window width is less than or equal to 575.98px. Similarly, .card-deck class eliminates

display: flex;flex-flow: row wrap;
. If you want two cards per row on smaller devices, follow this approach:

.card {
  flex-basis: calc(50% - 30px);
  flex-grow: 0;
  flex-shrink: 0;
  margin-left: 15px;
  margin-right: 15px;
  max-width: calc(50% - 30px);
}

Add the classes d-flex flex-wrap to the card-deck element.

<div class="card-deck d-flex flex-wrap"></div>

The updated version of the previous CodePen can be found here: CodePen Link. Below is the complete HTML and CSS code:

<div class="container">
    <div class="card-deck d-flex flex-wrap">
        ... // Card elements here
    </div>
</div>

CSS

.card-deck .card {
  flex-basis: calc(50% - 30px);
  flex-grow: 0;
  flex-shrink: 0;
  margin-left: 15px;
  margin-right: 15px;
  max-width: calc(50% - 30px);
}

@media (min-width: 768px) {
  .card-deck .card {
    flex-basis: calc(33.3333% - 30px);
    max-width: calc(33.3333% - 30px);
  }
}

@media (min-width: 1200px) {
  .card-deck .card {
    flex-basis: calc(25% - 30px);
    max-width: calc(25% - 30px);
  }
}

Answer №2

When working with the card-deck card, the default flexbox property flex:1 0 0% prevents the cards from expanding in width. To override this, utilize the flex-fill class on the cards and ensure to include the specificity .card-deck .card in the CSS selectors.

<div class="container">
    <div class="card-deck>
        <div class="card mb-4 flex-fill"><
            <img class="card-img-top img-fluid" src="//placehold.it/500x280" alt="Card image cap">
            <div class="card-body">
                <h4 class="card-title">1 Card title</h4>
                <p class="card-text">This is a longer card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.</p>
                <p class="card-text"><small class="text-muted">Last updated 3 mins ago</small></p>
            </div>
        </div>
        ... (more cards)
    </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

Parent div not properly adjusting its height

I am currently working on developing a fluid layout, however I have encountered a minor issue with the height of the container. The outer <div> (highlighted in yellow, ip_A-1) is not adjusting its height according to its child elements. You can view ...

Adjusting the background width results in the container position being altered as well

Currently, I am working with two background images, each accompanied by a red box positioned absolutely inside a bootstrap container. The first red box is located on the right side and the second red box is on the left side. So far, everything is running ...

Using jQuery to temporarily disable a div element

I need to implement a timer in a div that will disable it for a specific duration, such as 3 seconds. Here is the HTML code snippet: <div class="answ-container" align="center"> <div class="c_answer" id="1316" name="1" data-rcat-no="1"> ...

Identifying the origin of the link or button clicked to switch to a different webpage

Imagine you have a button and a link on your website that both direct users to the same next page. Once a user reaches that next page, how can you determine whether they clicked on the button or the link on the previous page? Is there a way to access this ...

What is the best way to extract value from an input text that is being repeated using ng-repeat within a Tr

Having a table with repeated rows in angular, you can also dynamically add new rows by clicking a button. If there are already 3 repeated rows and 2 extra rows are added with data entered, how can we retrieve all the data from the table in the controller ...

The background image does not have the body padding applied

When creating a fixed top nav bar, I added padding to the body tag so that the nav bar always stays on top like this: body { padding-top: 70px; } Now, I want to set a background image for the body that covers the entire screen, so I added the foll ...

hover effect with fading transition while mouse is still hovering

Is there a way to create a fade-in/fade-out effect on a div without the mouse needing to leave the area? Let me provide a clearer explanation: When the mouse enters the object The object gradually fades in Then, after a delay, the object fades out while ...

Determine whether a click event originated from within a child window

Currently, I am utilizing window.open to initiate a new window in javascript and my goal is to identify clicks made within the child window. Essentially, if a click event occurs in the child window, I aim to modify the parent window accordingly. I have a ...

Is it possible to customize the default styling options in Tailwind?

I am currently working on a blog using NextJS, and I have encountered an issue with Tailwind's list style type. It seems that the default styling for list style type is set to list-none, resulting in my <ul> <li> elements not being styled ...

What could be causing the absence of HTML attribute suggestions in my Visual Studio Code?

I'm experiencing an issue in my VS Code where the suggestion feature is not working properly. When I try to type "onClick, href, src, etc", the suggestions do not show up as they should. Interestingly, this problem doesn't occur in my older code ...

Reveal and conceal using CSS animations

I am working on adding animation effects to show and hide HTML elements triggered by a button click. The button toggles a "hide" class to display or hide the element. Check out my code snippet: const toggleButton = document.querySelector('button ...

Unable to dismiss keyboard in iPhone search bar

I recently added a search bar to my website and I've encountered a strange issue: when pressing the search button on an iPhone, the keyboard does not dismiss even though the search function works perfectly fine. Essentially, after typing in the search ...

Switching out one block of HTML with another in JavaScript is a powerful way to dynamically update

I am working on creating a feature on a webpage where clicking a button will change the HTML code inside a specific div. Essentially, I want to be able to update the content of a div by simply clicking a link. Here are two different sets of HTML code: Co ...

Mastering the Art of Restricting an IP Address Range with FILTER_VALIDATE_IP

In order to restrict access to certain resources, I need to implement a system that filters IP addresses. To validate the IP address, I currently use FILTER_VALIDATE_IP and filter_var. However, I am facing an issue where the starting and ending IP address ...

Creating intentional blank space before the <Span> element using CSS in HTML

Currently working on a table with some spanned elements for customization. One issue I am facing is the persistent blank space above the blue spanned element in grid 6. Even when adding more spans to grid 6, they just jump below the blue one. How can I mak ...

Side Menu with Fixed Position Created with Tailwind CSS

Can anyone help me create a social media sidebar that stays fixed on the bottom right side of the screen, regardless of its size? Currently, the sidebar moves when I scroll down, but I want it to remain in place. How can I achieve this fixed positioning? ...

Initiating a horizontal scroll menu at the center of the screen: Step-by

I need assistance setting up a horizontal scrolling menu for my project. The requirement is to position the middle button in the center of the .scrollmenu div. Currently, I am working with HTML and CSS, but open to suggestions involving javascript as well ...

"Enhance your website with a unique Bootstrap 5 carousel featuring multiple

As a beginner in Bootstrap, I am currently working on an ecommerce template to learn about Bootstrap 5. I am interested in creating a carousel that displays multiple slides at once, like a products slider with left and right arrows. Is this possible in Bo ...

What is preventing this code from successfully altering the opacity of an element based on its id?

I'm curious as to why the element's opacity is not temporarily reduced when the corresponding button is pressed. Any assistance would be greatly appreciated. function pause(miliseconds) { var currentTime = new Date().getTime(); while (cur ...

Do not use Express.js to serve the index.html file

Encountered an issue when attempting to run my Express.js solution. Instead of opening my desired index.html file located in the client directory, it kept opening the index.jade file from the views folder with the message "Welcome to Express" on the browse ...