What's causing my Bootstrap cards to overlap on smaller screens?

When using 3 bootstrap cards in the following code, they are perfectly displayed side by side on large screens. However, on medium screens, the cards overlap, and on small screens, they completely overlap. I tried adjusting the

class="col-lg-4 col-md-4 col-sm-2"
properties by changing the md-4s to 3s and the sm-2s to 3s and 4s, but saw no change.

Upon resizing the window and inspecting the elements with the class="card center classes, it became clear that they do not shrink in size on smaller devices, they simply overlap.

Any assistance would be greatly appreciated.

https://i.sstatic.net/CTbLv.jpg

body {
  /* border: solid 2px red; */
  background-color: #EDEFEE;
  font-family: 'Markazi Text', serif;
  margin-top: 3rem;
  margin-bottom: 3rem;
  margin-left: 5%;
  margin-right: 5%;
}

.container {
  background-color: lightpink;
}

h1 {
  font-size: 3rem;
}

h2 {
  font-size: 2rem;
}

@media only screen and (max-width: 912px) {
  /* For mobile phones: */
  [class*="col-"] {
    width: 33%;
    padding: 0;
  }
  .container {
    overflow: hidden;
    padding-top: 25px;
  }
}
<!-- <link rel="stylesheet" href="style.css"> -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bad8d5d5cec9cec8dbcafa8f9489948a97dbd6cad2db8b">[email protected]</a>/dist/css/bootstrap.min.css">

<main>
  <div class="container">
    <div class="row">
      <div class="col-lg-4 col-md-4 col-sm-2">
        <div class="card center" style="width: 18rem; height: 90%;">
          <img src="assets/img/ragout.jpg" class="card-img-top" alt="..." style="height: 245px;">
          <div class="card-body">
            <h2 class="card-title bg-warning text-danger">Ragout de pomme de terre<span class="badge bg-primary">New</span></h2>
            <p class="card-text">Potato cooked with beef and carrot.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>
      <div class="col-lg-4 col-md-4 col-sm-2">
        <div class="card center" style="width: 18rem; height: 90%;">
          <img src="assets/img/table.jpg" class="card-img-top" alt="..." style="height: 245px;">
          <div class="card-body">
            <h2 class="card-title">Book a table</h2>
            <p class="card-text">Potato cooked with beef and carrot.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>
      <div class="col-lg-4 col-md-4 col-sm-2">
        <div class="card center" style="width: 18rem; height: 90%;">
          <img src="assets/img/open.jpg" class="card-img-top" alt="..." style="height: 245px;">
          <div class="card-body">
            <h2 class="card-title">Open hours</h2>
            <p class="card-text">Potato cooked with beef and carrot.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</main>

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c9aba6a6bdbabdbba8b989fce7fae7f9e4a8a5b9a1a8f8">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

Answer №1

To create a layout with three columns, utilize a column size of 4 (12/3). Refer to the grid documentation to understand that column classes should be arranged from smallest to largest (mobile-first approach), and there is no need to duplicate breakpoints with the same quantity. For instance, instead of using col-lg-4 col-md-4 col-sm-2, simplify it to col-sm-2 col-md-4.

Avoid using inline styles as they lead to repetitive code and make maintenance challenging. Opt for custom styles where Bootstrap classes are insufficient, like the padding classes employed on the container element in this scenario.

Furthermore, applying margin directly to the body element may not produce the desired result since the body typically matches the viewport size. In such cases, use padding on the body or apply margin to inner elements.

body {
  background-color: #EDEFEE;
  font-family: 'Markazi Text', serif;
}

.container {
  background-color: lightpink;
}

h1 {
  font-size: 3rem;
}

h2 {
  font-size: 2rem;
}
<!-- <link rel="stylesheet" href="style.css"> -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7d5d8d8c3c4c3c5d6c7f782998499879ad6dbc7dfd686">[email protected]</a>/dist/css/bootstrap.min.css">

<main>
  <div class="container pt-3 pt-md-0">
    <div class="row">
      <div class="col-4 col-md-3">
        <div class="card center">
          <img src="https://via.placeholder.com/400x200" class="card-img-top" alt="...">
          <div class="card-body">
            <h2 class="card-title bg-warning text-danger">Ragout de pomme de terre<span class="badge bg-primary">New</span></h2>
            <p class="card-text">Potato cooked with beef and carrot.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>

      <div class="col-4 col-md-3">
        <div class="card center">
          <img src="https://via.placeholder.com/400x200" class="card-img-top" alt="...">
          <div class="card-body">
            <h2 class="card-title">Book a table</h2>
            <p class="card-text">Potato cooked with beef and carrot.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>

      <div class="col-4 col-md-3">
        <div class="card center">
          <img src="https://via.placeholder.com/400x200" class="card-img-top" alt="...">
          <div class="card-body">
            <h2 class="card-title">Open hours</h2>
            <p class="card-text">Potato cooked with beef and carrot.</p>
            <a href="#" class="btn btn-primary">Go somewhere</a>
          </div>
        </div>
      </div>
    </div>
  </div>
</main>

<script src="https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="88eae7e7fcfbfcfae9f8c8bda6bba6b8a5e9e4f8e0e9b9">[email protected]</a>/dist/js/bootstrap.bundle.min.js"></script>

Answer №2

By using the class "col-sm-2" for the cards, you are indicating that it will display 1/6 of the cards on small screens. Is this the desired outcome? To adjust the number of cards displayed on small screens, consider using the class "col-lg-4 col-md-4 col-sm-10" or "col-lg-4 col-md-4 col-sm-6" for two or one card display, respectively.

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

Struggling with a Bootstrap v5.0.2 Modal glitch? Let's dive into a real-life case study to troub

I am seeking assistance with a problem that I am encountering. I am currently using Bootstrap version 5.0.2 (js and css). Despite coding all the required parameters, I am unable to make the modal functionality work. I have been trying to figure out what&ap ...

Placement of navigation bar on top of picture

Creating a gaming website and encountering challenges with text alignment. The goal is to have the navbar text positioned on top of a customized navbar background. Current appearance navbar example <!DOCTYPE html> <html> <head> ...

Utilize PHP to transform various forms of smart quotes

I have been working on a function to convert all types of smart quotes to regular quotes within text. However, the function I have put together still appears to be lacking proper support and design. How can I successfully convert all quote characters? fun ...

Change the Background of Your Body?

Here's the deal. I'm looking to create a cool effect where the background of the body slowly fades out and changes periodically in a loop using CSS and jQuery. Any suggestions on how I can make this happen? Appreciate your help! ...

I am encountering issues with an HTML table that is in need of fixing, and I must find a solution without relying on

Can someone help me troubleshoot the structure of this table in the given image? It seems like my rowspan and colspan attributes are not working as expected. I've only used CSS to specify the width and height of the cells. https://i.sstatic.net/VtHbH ...

Is there a way to deactivate or dim a specific hour in an HTML time form?

I am currently facing a challenge with my booking form. I need to find a way to grey-out or disable the hours/times that have already been booked by previous customers. This will help ensure that the next person can select a different time slot. However, I ...

Is it possible to have a never-ending slideshow with inline-blocks?

After spending countless hours trying to troubleshoot my code, I am now seeking help from the wonderful people of the internet! ;) It seems like only a small portion of my code is incorrect. What I'm attempting to do is move the leftmost object in the ...

Display a pop-up when hovering over a layer with react-leaflet

I am attempting to display a popup when hovering over a layer in react leaflet. Utilizing GeoJson to render all layers on the map and onEachFeature() to trigger the popup on hover, I encountered an issue where the popup only appeared upon click, not hover. ...

Implement custom styles to enhance the appearance of users' Magento websites

Is it possible to add additional CSS only to users/customers who are logged into our webshop? If so, how should I go about it - through XML or PHTML? Stian ...

Refreshing ng-model within a radio input

Currently, I am utilizing a jQuery library for radio/checkbox components. Although I am aware that combining jQuery with Angular is not ideal, the decision to use this particular library was out of my control and cannot be altered. One issue I have encount ...

What is the best way to add spacing between the fields within a Bootstrap 4 form row that spans across 2 rows when viewed on small displays?

Trying to create a bootstrap 4 form with fields in one row for larger screens and two rows for smaller screens. The attempt was made using the following code: <div class="form-group row"> <label class="col-sm-2 col-form-label">File:</lab ...

What is the best way to apply custom styles in reactJs to toggle the visibility of Google Maps?

There are three radio buttons that correspond to school, restaurant, and store. Clicking on each button should display nearby locations of the selected type. Displaying Google Map and nearby places individually works fine without any issues. class Propert ...

How can I remove the color of a button in jquery after it's been clicked?

<button style="background-color:lightblue" id="extractv"> <b> Extract<br>v </b> </button> ... $("#extractv").click(function () { $("#extractv").removeAttr("style"); }); Upon clicking the button, my ...

Switching up the styles in PHP-embedded HTML by tweaking the CSS

I used PHP to create HTML elements by echoing them out, but I ran into an issue when trying to change the CSS for those HTML elements. Can you help me figure out how to do this? The PHP } else { include("steamauth/SteamCo ...

Parse JSON data, iterate through dictionaries, and convert into markup using Python

I'm working with a JSON file that contains information about different people: { "PersonA": { "Name": "Woman A", "Age": 23, "Info": "Likes cats ..." }, "PersonB": { "Name": "Man B", "Age": 32, ...

Guide to changing the content zoom factor of UWP WebView

Looking to reset the content zoom factor of a webview in C# UWP to 1 or 100%. How can this be achieved? Is there a way to instruct the webview itself to scale back to 1.0 using C#? Something like webView.ContentScale = 1.0; Alternatively, what code ne ...

Forecasting text input demands

Many websites display suggestions in a drop-down menu as you type, some even showing a preview of the most likely result in a lighter-gradient font next to the cursor. For example, Spotlight on my Mac does this: https://i.sstatic.net/hDyxG.png Spotlight ...

Leveraging the power of HTML5 alongside Angularjs and Node/Express within the MEAN.js boilerplate framework

I've decided to kickstart my app using the mean.js () boilerplate because of its built-in authentication/authorization features. However, I've hit a roadblock when it comes to incorporating HTML5 into my project. In my Angular app, I've en ...

Include the background image on each page of the long read HTML document

Is there a way to add a background image to every page when printing a longread html document? The current code I have only applies the image to the first page. @media print { body { background-image: url("something.jpg"); } } ...

What are the best tools to develop a browser-based 2D top-down soccer simulation?

I'm looking to create a 2D top-down soccer simulation game for web browsers using modern technologies and without the need for additional plugins like Flash or Silverlight, making it compatible with mobile devices as well. The game will be AI-controll ...