Bootstrap Cards with text that stacks perfectly for any screen size

I've been struggling to properly stack items in Bootstrap layout. Using containers and rows to stack the items works well until a certain breakpoint is reached, after which the alignment is off. Despite searching through numerous StackOverflow questions related to cards and text, I couldn't find a similar problem.

Below is the design that works almost correctly before the 990px breakpoint (may need to add negative padding for proper alignment):

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

Once the breakpoint is crossed, the layout looks like this:

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

Even on the mobile version, where there's enough space, the design is completely wrong:

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

My code is in react/nextjs, so feel free to ignore the className if you're not familiar with it.

<div className="container">
      <div className="card-deck">
        {/*     CARD START */}
        <div className="card mb-4">
          <img
            className="card-img-top img-fluid"
            src="//placehold.it/500x280"
            alt="Card image cap"
          />
          <div className="card-body">
            <h4 className="card-title">Test information</h4>
            <div className="fee-stats">
              <div className="container">
                <div className="row">
                  <div className="col-sm">
                    <p className="card-text">
                      TAKER
                      <br />
                      0.05%
                    </p>
                  </div>
                  <div className="col-sm">
                    <p className="card-text">
                      MAKER <br /> 0.05%
                    </p>
                  </div>
                  <div className="col-sm">
                    <p className="card-text">
                      WITHDRAWAL
                      <br /> 0.0005
                      <span className="bitcoin-icon">&#8383;</span>{" "}
                    </p>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div className="card-footer border-0 rounded-bottom">
            <small className="text-muted">Intermediate</small>
          </div>
        </div>

CSS:

.card-style {
          margin: auto;
          width: 80%;
          padding: 10px;
        }
        .card-img-top {
          margin: auto;
          width: 30%;
          padding-top: 20px;
        }
        .card-title {
          text-align: center;
        }
        .card-footer {
          background-color: #9effaf;
        }
        .rounded {
          border-radius: 0.6rem !important;
        }
        .rounded-bottom {
          border-bottom-left-radius: 0.6rem !important;
          border-bottom-right-radius: 0.6rem !important;
        }
        .card-text {
          text-align: center;
          font-size: 65%;
        }

Answer №1

Unclear about your specific issue or desire, but by setting the column widths at each breakpoint, you can achieve your desired outcome. You can maintain inline content by specifying width = 12 / # of elements and make them stack by using width = 12....

<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">Test information</h4>
        <div class="fee-stats">
            <div class="container">
                <div class="row">
                    <div class="col-4  px-1">
                        <p class="card-text"> TAKER <br /> 0.05% </p>
                    </div>
                    <div class="col-4  px-1">
                        <p class="card-text"> MAKER <br /> 0.05% </p>
                    </div>
                    <div class="col-4  px-1">
                        <p class="card-text"> WITHDRAWL <br /> 0.0005 <span className="bitcoin-icon">&#8383;</span>{" "} </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="card-footer border-0 rounded-bottom">
        <small class="text-muted">Intermediate</small>
    </div>
</div>
 <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">Test information</h4>
        <div class="fee-stats">
            <div class="container">
                <div class="row">
                    <div class="col-4  px-1">
                        <p class="card-text"> TAKER <br /> 0.05% </p>
                    </div>
                    <div class="col-4  px-1">
                        <p class="card-text"> MAKER <br /> 0.05% </p>
                    </div>
                    <div class="col-4  px-1">
                        <p class="card-text"> WITHDRAWL <br /> 0.0005 <span className="bitcoin-icon">&#8383;</span>{" "} </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="card-footer border-0 rounded-bottom">
        <small class="text-muted">Intermediate</small>
    </div>
</div>
 <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">Test information</h4>
        <div class="fee-stats">
            <div class="container">
                <div class="row">
                    <div class="col-4  px-1">
                        <p class="card-text"> TAKER <br /> 0.05% </p>
                    </div>
                    <div class="col-4  px-1">
                        <p class="card-text"> MAKER <br /> 0.05% </p>
                    </div>
                    <div class="col-4  px-1">
                        <p class="card-text"> WITHDRAWL <br /> 0.0005 <span className="bitcoin-icon">&#8383;</span>{" "} </p>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="card-footer border-0 rounded-bottom">
        <small class="text-muted">Intermediate</small>
    </div>
</div>

edit: Check out the Bootstrap grid system here. Here, col-6 means it occupies size 6 (out of 12) by default. And col-md-4 indicates it's size 4 once the screen is larger than medium.

edit2: View a working example here

Answer №2

When setting the container width to col-sm, remember to include the specific number. For example, use col-sm-4 if you want to divide the row into 3 columns.

If you want the layout to display as 3 columns on all devices, you can use col-3.

Check out this working example

 <div class="row">
              <div class="col-3">
                <p class="card-text">
                  TAKER
                  <br />
                  0.05%
                </p>
              </div>
              <div class="col-3">
                <p class="card-text">
                  MAKER <br /> 0.05%
                </p>
              </div>
              <div class="col-3">
                <p class="card-text">
                  WITHDRAWL
                  <br /> 0.0005
                  <span class="bitcoin-icon">&#8383;</span>{" "}
                </p>
              </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

Vertical scrollbar in iframe unexpectedly appears immediately after the submit button is pressed

I have designed a contact form that is displayed in an iframe within an overlay div. I have carefully adjusted the dimensions of this div to ensure that there are no scrollbars visible when the form initially appears. However, after filling out the form an ...

Having trouble customizing the Material UI Badge style?

I am having trouble customizing the appearance and position of the Badge component by overriding the root CSS style. The Card component is responding to the styling passed via the className prop, but the Badge component is not reflecting any changes. I am ...

Encountering a problem with scrolling the modal to the top

I am currently working on a code snippet that involves scrolling the modal to the top position. The purpose of this is to display form validation messages within the modal popup. Below are the jQuery code snippets that I have attempted: //Click event t ...

I'm puzzled as to why my media query for `max-width: 470px` isn't functioning correctly

I'm struggling to get my code to work properly. I've tried adjusting some parts to be more specific and others more general, but it's not functioning as expected. Please review my code and any advice or assistance would be greatly appreciat ...

Why isn't setInterval set to a duration of one thousand milliseconds?

While trying to use some code from w3schools, I noticed that the setInterval in the example is set to 5 instead of 5000. Shouldn't it be in milliseconds? If not, how can I speed up this animation? When I try reducing it to decimals like 0.01, the anim ...

Struggling with incorporating a lightbox within an accordion feature in an .html file

I'm currently attempting to integrate this lightbox into this accordion feature. Individually, I've managed to get both elements up and running smoothly. However, when trying to combine them on the same page (regardless of nesting one inside the ...

The background image causes the scrollbar to vanish

As a beginner, I am in the process of creating a web page that features a consistent background image. However, I have encountered an issue where the scroll bar does not appear on a specific page called "family details" due to the background image. I atte ...

What is the best way to restore a component's state to its default settings when it receives new props?

I have a Next.js project in development with a custom Layout already set up. I want the header component to reset whenever a new page is navigated to, so that the menu reverts back to its default state. Does anyone know how I can achieve this? import { F ...

Can someone explain the functionality of the CSS Selector > * when used inside a Vue.js component?

While exploring the Vuestic admin dashboard tool, I noticed a glitch in the UI where the donut chart appeared larger than its container. I tried to troubleshoot and fix it for submission as a PR, but I encountered an unfamiliar CSS selector > * while de ...

Arranging images and text side by side with varying breakpoints using Bootstrap classes

In my layout, I have an image and text side by side, with the image appearing first and the text to its left. My goal is to display the text above the image when the screen size is reduced to a small breakpoint, and then switch back to the original layou ...

Exploring the integration of mixins from .scss files within node_modules into our .tsx or .jsx files for enhanced styling

I am currently facing a challenge with importing a .scss file from one of the installed node_modules into my .tsx file. The scss file contains a mixin named @mixin make-embedded-control($className: embedded-control){.....} which has all the necessary css ...

Is it possible to create a visual representation (such as a jpg file) of a website page?

Looking to generate an image of a web page, like creating a miniature version of the HTML and images. It doesn't need to be exact (no need for Flash/JavaScript rendering). I plan to use this on Linux - preferably with a Java library, but a command li ...

Adding a button to the right of a text-field input in Vuetify app

I need advice on the best way to use Vuetify in order to display a button to the right of a text-field (attached). I've checked the main site for information but couldn't find anything related to this specific scenario. https://i.sstatic.net/Pp ...

Tips on modifying the maxlength attributes for all "field answer" class elements

Looking for some help with adjusting the "maxlength" attribute in a class called "field answer." The current maxlength is set to 250, but I need it changed to 9999. Can someone guide me through this process? <div class="field answer"> &l ...

Invoke a function within the React library through ReactDOM.render

Within my index.js file, I have the following code: const store = createStore( combineReducers({ i18n: i18nReducer }), applyMiddleware(thunk) ); syncTranslationWithStore(store) store.dispatch(loadTranslations(translationsObject)); stor ...

Incorporating HTML elements within VBScript within a single document

Here is a code snippet that mixes VBScript and HTML: IF (x.name="name") THEN n=x.value response.write("<tr>") response.write("<th>Name:</th>") response.write("<td><input name=""n2"" value=" & n & ">< ...

Creating multiple Google markers based on addresses retrieved from a database using PHP

I have a requirement to display multiple Google markers on a map based on addresses stored in a database. I have already successfully loaded an XML file to retrieve latitude and longitude coordinates, but now I need assistance on how to create multiple m ...

Using Jquery Chosen Plugin to Dynamically Populate One Chosen Selection Based on Another

Good evening to all, please excuse any errors in my English. I have successfully integrated a jQuery Chosen plugin with my 'estado' field (or province). My goal is to populate another jQuery Chosen plugin with the cities corresponding to that s ...

An error occurs in the console stating 'Maximum call stack size exceeded' when trying to add data to the database using JavaScript

I've been struggling with a JavaScript error for the past few days and despite scouring through numerous answers on StackOverFlow, none seem to address my specific issue. My goal is to simply submit a record to the database using a combination of Jav ...

Optimal approach for vertically aligning elements in CSS

I'm looking to arrange my header ('Sail away today with Starboard Rentals.') and link buttons on top of each other. I want the navigation buttons to be positioned below the h1 on the lower half of the 'home-jumbo' div. What's ...