Tips for minimizing the gaps between cards in Bootstrap 5 through CSS

What is the best way to adjust card spacing in Bootstrap 5 using CSS? Here is the HTML code for the cards section:

<section id="services">
  <div class="container">
    <div class="row text-center">
    <div class="col-md-12">
    <img src="images/network.png" class="service-img">
    <h4 class="services-head mb-2">Search Facilities</h4>
    </div>
    </div>
    <div class="row row-cols-1 row-cols-md-2 g-0">
      <div class="col-md-3">
        <div class="card">
          <img src="images/network.png" class="service-list">
          <div class="card-body">
            <h4 class="card-title">Blood</h4>
          </div>
        </div>
      </div>
      <div class="col-md-3">
        <div class="card">
          <img src="images/network.png" class="service-list">
          <div class="card-body">
            <h4 class="card-title">Donation</h4>
          </div>
        </div>
      </div>
      <div class="col-md-3">
        <div class="card">
          <img src="images/network.png" class="service-list">
          <div class="card-body">
            <h4 class="card-title">People Needs</h4>
          </div>
        </div>
      </div>
      <div class="col-md-3">
        <div class="card">
          <img src="images/network.png" class="service-list">
          <div class="card-body">
            <h4 class="card-title">Education</h4>
          </div>
        </div>
      </div>
    </div>
    </div>
  </div>
</section>

Here is the associated CSS code:

#services{
    padding: 80px 0;

}
.service-img{
    width: 100px;
    margin-top: 20px;
}
.services-head{
    color: #2596be;
    padding-top: 10px;
    font-family: "Exo", sans-serif;
}
.card{
    text-align: center;
    margin-top: 10px;
    border-color: #2596be;
    border-radius: 5px;
    padding: 30px 0;
    width: 150px;
    height: 150px;
}
.card .card-title{
    color: #2596be;
    font-family: "Exo", sans-serif;
    font-size: medium;
}
.service-list{
    width: 70px;
    margin-left: auto;
    margin-right: auto;
    display: block;
}

I have exhausted my efforts trying to reduce the spacing between the cards. Click here to see the image. As shown in the image, there seems to be a fixed space between the cards that I cannot seem to remove.

Answer №1

There were two subtle issues that caused the problem you encountered. First, cards come with default margins and padding that need to be manually reset to zero. Second, the column layout specified in a separate div needed restructuring. Here's how I modified your code:

    #services {
        padding: 80px 0;
    }

    .service-img {
        width: 100px;
        margin-top: 20px;
    }

    .services-head {
        color: #2596be;
        padding-top: 10px;
        font-family: "Exo", sans-serif;
    }

    .container {
        margin-left: 0;
        margin-right: 0;
        padding-left: 0;
        padding-right: 0;
    }

    .card {
        text-align: center;
        margin-top: 10px;
        border-color: #2596be;
        border-radius: 5px;
        width: 150px;
        height: 150px;
        margin: 0;
    }

    .card .card-title {
        color: #2596be;
        font-family: "Exo", sans-serif;
        font-size: medium;
    }

    .service-list {
        width: 70px;
        margin-left: auto;
        margin-right: auto;
        display: block;
    }
<section id="services">
    <div class="container">
        <div class="row text-center">
            <div class="col-md-12">
                <img src="images/network.png" class="service-img">
                <h4 class="services-head mb-2">Search Facilities</h4>
            </div>
        </div>
        <div class="row d-flex flex-row justify-content-center">
            <div class="card">
                <img src="images/network.png" class="service-list">
                <div class="card-body">
                    <h4 class="card-title">Blood</h4>
                </div>
            </div>
            <div class="card">
                <img src="images/network.png" class="service-list">
                <div class="card-body">
                    <h4 class="card-title">Donation</h4>
                </div>
            </div>
            <div class="card">
                <img src="images/network.png" class="service-list">
                <div class="card-body">
                    <h4 class="card-title">People Needs</h4>
                </div>
            </div>
            <div class="card">
                <img src="images/network.png" class="service-list">
                <div class="card-body">
                    <h4 class="card-title">Education</h4>
                </div>
            </div>
        </div>
    </div>
</section>

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

Using Sencha Touch: What is the best method for populating an HTML panel with a JSON response?

I've exhausted all possible combinations in my attempts to load a JSON object from the server and use it to render a panel with video and other information. Despite my efforts, I haven't been able to make anything work. What could I be doing inco ...

Is it feasible to have a set number of character lines per <p> tag without relying on monospaced fonts?

I am facing the challenge of breaking a large text into paragraphs of equal size, with the same number of string lines in order to maintain uniformity. Currently, I am using PHP and the following code to achieve this: function arrangeText(){ if (have_ ...

Allow the div to break out of the overflow: hidden property without disrupting the overall page layout

Currently, I am attempting to separate a child element from its parent that has overflow: hidden applied. The main reason for restricting the dimensions of the parent is for a JavaScript workaround. I have experimented with positioning the child element, ...

Tips for inserting a button under a div when clicked

I am working on a layout where I have several div cards aligned side by side using the display: inline-block property. What I want to achieve is that when I click on a card, a button is added below the respective div. To accomplish this, I tried using jqu ...

Is it possible to include more details in the document?

Is it possible to include additional metadata, such as a record ID, within a file without causing corruption? I am looking for ways to add extra information to files. Can anyone offer some guidance? Your help would be greatly appreciated. Thank you! ...

Exploring file directories in your web browser using Node.js - a guide to embedding a file system path in your HTML response

I am currently facing an issue where I am trying to retrieve content from my nodejs express controller that includes a file system link for viewing logs in the browser. However, when I click on this link, nothing happens. The logs specified in the href li ...

Failure to receive results from $.getJSON request

I am currently working on developing a web page that allows users to search and retrieve Wikipedia pages based on their search results. Below is the relevant HTML code for the search feature: <form class='search-form' onsubmit='return f ...

jQuery Autocomplete API Issue: Undefined

I've been attempting to implement a basic text box using the jQuery API from DevBridge. I followed instructions in this video tutorial to create my code. However, despite properly declaring scripts before the JS code and ensuring proper mappings, I&a ...

What could be causing the uneven application of CSS padding when it is applied to a div that serves as the parent of an HTML form?

Padding has been added only to the top and bottom of a div. The padding attribute is displaying consistently for the form element, however, it is not behaving uniformly for the parent div of the form as illustrated in the image provided below. Output:- h ...

Safari's Styling Struggles

I've been diligently working on a website for some time now, conducting the majority of my testing in Chrome, Firefox, and IE. However, as I near completion, I've run into an issue when viewing the site in Safari on Mac, iPad, and iPhone. I' ...

output a portion of the present operational folder

Currently, the directory I am focusing on is C:\xampp\htdocs\example\profile\john_doe I am curious about how I can display john_doe in this scenario. My coding language of choice is PHP. ...

An issue occurred while the request was being transported or processed, resulting in Error code 10 at Path /wardeninit

Currently, I am attempting to pass an object (specifically the contents of a row from a sheet) to an apps script template. The screenshot displays the actual row. https://i.stack.imgur.com/IzMrn.png The function in my apps script consists of the followin ...

Designing a dynamic webpage using JavaScript to seamlessly display the latest updates in real-time

I am interested in developing a web page that enables users to input new data and perform calculations without refreshing the entire page. I prefer a table format for simplicity, but ease of coding is also important. What would be the most efficient approa ...

What is the best way to ensure that the draggable element remains visible on top of all other elements on the screen while being dragged?

Currently, I am working on implementing the drag and drop feature in SAPUI5 Table and Tree Table. In my project, there is a table containing names that need to be draggable, and a Tree Table where these names should be droppable. To achieve this function ...

My form does not receive the Bootstrap classes when using the jQuery script

**Why isn't my jQuery script coloring the rows as expected when certain conditions are met (I italicized the specific part of the code)?** Here is the HTML CODE for the POLL: <form id="pollForm" class="mb-4"> <d ...

CSS not being applied to Next.js HTML pages

Currently, I am utilizing Nextjs and including the flaticon css in _app.js as follows: import '../public/assets/css/flaticon.css'; In the upcoming section, an error is being encountered: @font-face { font-family: 'Flaticon'; src: ...

Spin a sphere within a semicircle using html and css

I have been working on a project to animate a ball inside a crescent shape using HTML and CSS. You can check out my codepen here. However, I am facing an issue where the ball is currently stuck at the top of the crescent. I would greatly appreciate any as ...

Is there a way to set up an automatic pop-up for this?

Experience this code function AutoPopup() { setTimeout(function () { document.getElementById('ac-wrapper').style.display = "block"; }, 5000); } #ac-wrapper { position: fixed; top: 0; left: 0; width: 100%; height: 100%; back ...

Mix div borders with varying border radius for child elements

I'm currently working on a project that involves creating border lines between divs to achieve a design similar to the one shown in this design jpeg. However, I've only managed to reach an output jpeg where the merging is not quite right. The C ...

Is there a way to transfer information from an HTML form directly into an Excel file using web services?

Recently, I created an email newsletter using HTML and CSS. This newsletter includes 4 text boxes where users can input their name, family name, phone number, and email address. I am wondering if it is possible to use a web service to transfer this data fr ...