Tips for positioning div elements in Bootstrap 4 with consistent spacing

Is there a way to align these 3 divs with more margin?

I attempted using col-3, but it didn't have the desired effect. I want the first div on the left, the second in the center, and the third on the right. I tried incorporating CSS for margin, but it caused issues in responsive design. Do I need to address the responsiveness as well?

Here is an image of what I am trying to achieve:


    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="UTF-8>
            <title>General Announcement</title>
        </head>
        <body>

        <section class="activities>
            <div class="container>
                <div class="row>
                    <div class="col-md-12>
                        <h3 class="text-center text-primary text-uppercase font-weight-bold mb-4>Upcoming activities for October 2019</h3>
                    </div>
                    <div class="col-md-3 mb-sm-5>
                        <img src="images/img-5.jpg" class="img-fluid>
                            <span class="d-block text-uppercase font-weight-bold mt-3>October 20</span>
                            <h4 class="text-uppercase font-weight-bold>Run for charity</h4>
                        <a href="#" class="btn btn-default btn-primary>More info</a>
                    </div>
                    <div class="col-md-3 mb-sm-5>
                        <img src="images/img-6.jpg" class="img-fluid>
                        <span class="d-block text-uppercase font-weight-bold mt-3>October 11</span>
                        <h4 class="text-uppercase font-weight-bold>Book drive</h4>
                        <a href="#" class="btn btn-default btn-primary>More info</a>
                    </div>
                    <div class="col-md-3>
                        <img src="images/img-7.jpg" class="img-fluid>
                        <span class="d-block text-uppercase font-weight-bold mt-3>October 21</span>
                        <h4 class="text-uppercase font-weight-bold>Recycling initiative</h4>
                        <a href="#" class="btn btn-default btn-primary>More info</a>
                    </div>
                </div>
            </div>
        </section>

        </body>
    </html>

Answer №1

To optimize your layout, move the col-12 header section outside of the current row and convert the row to a flex column. Bootstrap 4 provides excellent support for flex properties, making it the ideal choice for this scenario.

For a visual representation, check out this JSFiddle demo

<section class="activities">
        <div class="container">
        <div class="col-md-12">
                    <h3 class="text-center text-primary text-uppercase font-weight-bold mb-4">Upcoming activities for october 2019</h3>
                </div>
            <div class="d-flex justify-content-between">

                <div class="col-md-3 mb-sm-5">
                    <img src="images/img-5.jpg" class="img-fluid">
                        <span class="d-block text-uppercase font-weight-bold mt-3">October 20</span>
                        <h4 class="text-uppercase font-weight-bold">Run for charity</h4>
                    <a href="#" class="btn btn-default btn-primary">More info</a>
                </div>
                <div class="col-md-3 mb-sm-5">
                    <img src="images/img-6.jpg" class="img-fluid">
                    <span class="d-block text-uppercase font-weight-bold mt-3">October 11</span>
                    <h4 class="text-uppercase font-weight-bold">Book drive</h4>
                    <a href="#" class="btn btn-default btn-primary">More info</a>
                </div>
                <div class="col-md-3">
                    <img src="images/img-7.jpg" class="img-fluid">
                    <span class="d-block text-uppercase font-weight-bold mt-3">October 21</span>
                    <h4 class="text-uppercase font-weight-bold">Recycling initiative</h4>
                    <a href="#" class="btn btn-default btn-primary">More info</a>
                </div>
            </div>
        </div>
    </section>

Answer №2

To properly style your webpage, make sure to add the Bootstrap css.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Important Notice</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>

<body>

    <section class="events">
        <div class="container">
            <div class="row justify-content-between">
                <div class="col-md-12">
                    <h3 class="text-center text-primary text-uppercase font-weight-bold mb-4">Upcoming events for
                        October 2019</h3>
                </div>
                <div class="col-md-3 mb-sm-5">
                    <img src="images/img-5.jpg" class="img-fluid">
                    <span class="d-block text-uppercase font-weight-bold mt-3">October 20</span>
                    <h4 class="text-uppercase font-weight-bold">Charity run</h4>
                    <a href="#" class="btn btn-default btn-primary">Learn more</a>
                </div>
                <div class="col-md-3 mb-sm-5">
                    <img src="images/img-6.jpg" class="img-fluid">
                    <span class="d-block text-uppercase font-weight-bold mt-3">October 11</span>
                    <h4 class="text-uppercase font-weight-bold">Book donation drive</h4>
                    <a href="#" class="btn btn-default btn-primary">Learn more</a>
                </div>
                <div class="col-md-3">
                    <img src="images/img-7.jpg" class="img-fluid">
                    <span class="d-block text-uppercase font-weight-bold mt-3">October 21</span>
                    <h4 class="text-uppercase font-weight-bold">Recycling project</h4>
                    <a href="#" class="btn btn-default btn-primary">Learn more</a>
                </div>
            </div>
        </div>
    </section>

</body>

</html>

Give this a try and hopefully it will work as expected :)

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

How do I input text into a Google Doc using an XPATH or element that is difficult to manipulate?

I'm currently working on a Python code that is designed to automatically open a Google Doc and input a specific set of combinations for a predetermined length. While I've successfully created the code to navigate to my Google Docs and open a new ...

After exiting the PWA, the IOS camera displays a black screen

I have implemented an HTML file input to open the camera and capture photos for my Progressive Web App (PWA). <input type="file" accept="image/*" capture="camera" name="photo" id="photo-input-js" data-project-id="<?php echo $projectId ?>"> // ...

Maintain visibility of Bootstrap side menu

I noticed on the Bootstrap website there is a sidebar with sticky links that stay in view as you scroll. They also highlight each menu item when it's in view. I've tried replicating this on my own website, but can't figure out how to make it ...

How can you change the width of <td> and <th> elements?

I am trying to keep a table header fixed at the top of a window, but for some reason, setting the widths of the <td> and <th> elements as shown below is not resulting in them having the same width (the column headers are misaligned with other c ...

HTML forms are larger in size on web pages than in Internet Explorer

https://i.stack.imgur.com/bUPtm.png I have posted the issue, and it is predominantly visual in nature. When viewed on a web browser within a C++ interface, the HTML appears significantly distorted. ...

Establishing connection to the database

I am in the process of developing a small-scale project on creating Universal Identification Numbers, similar to that of college level projects. My goal is to design a page with tabs for different databases, so that when I press the key 'A' for a ...

Code for creating a multiselect box using jQuery

Looking to create a drop-down menu with multiple checkboxes similar to this example: Are there any other sources that offer something like this? ...

Different options exist for top layers with flexible widths aside from using position: absolute

The only method I'm aware of to place a layer on top is by using position: absolute. https://i.sstatic.net/R4j8i.jpg (favoring top, disliking bottom) However, this approach often prevents dynamic scaling with the rest of the page. You can attempt ...

Ways to declare a function within the events onMouseOver and onMouseOut

<div align="right" style="border:1 #FF0 solid; background-color:#999" onMouseOver="javascript: function(){this.style.backgroundColor = '#DDD';}" onMouseOut="javascript: function(){this.style.backgroundColor = '#999';}"> Register & ...

Navigating through cards in HTML: A guide to filtering techniques

I'm currently in the process of developing a platform that enables users to browse through different profiles based on their first names. I've attempted to utilize bootstrap's "data-title" for searching purposes, but unfortunately, it's ...

Transmitting a form using an AJAX request with a jQuery script

My main objective is to develop a form that can send an image to the server using Ajax via jQuery. I previously posted this question on Stack Overflow (Problem sending a form with a jquery file component by ajax), but unfortunately, it was closed and I st ...

Launch the game within the gaming application

I am looking to incorporate a game within another game... Here's what I mean: Open the app: Pandachii Next, navigate to the 4th button (game pad). When we click on the game icon, I want to launch another game (located below the Pandachii window - c ...

"Enhancing Your Website with Dynamic CSS3 Div Animations

Seeking assistance in displaying an empty div after a CSS3 Animations loading circle has completed. Any guidance is welcome! jsFiddle ...

Guide to center align fields in Ionic and Angular

I am working on creating a login screen that resembles the image provided. I have managed to set the background image, input fields, and buttons successfully. However, I am encountering a few issues: The width of my input field is taking up the entire spa ...

Determine the total of all input values

My challenge is to calculate the total sum of all inputs, with the twist that they are hidden initially and randomly revealed by visitors. Imagine there are 30 inputs in total. So far, I have successfully identified the revealed inputs without the "hidden ...

Learn the effective way to style ul elements within @mui/material/Menu using CSS

When the button is clicked, I want to apply the following styles to the ul: &.MuiList-root { padding-top: 0px; padding-bottom: 0px; } View the issue in action on this live sandbox: https://codesandbox.io/s/react-typescript-forked-tyd8n8?fil ...

Utilize image maps for dynamically displaying and concealing div elements

Here is the current state of my code... VIEW DEMO I am aiming to display the blue text to the user only when they click on the blue circle, etc... I believe I need to modify the attributes of the div elements, but I am not very familiar with jQuery and ...

Eliminate the use of burger icons in a theme inspired by the

Is there a way to eliminate the burger menus in the "Eighties" Wordpress theme and substitute it with a classic navigation menu (logo on the left & menu on the right) positioned above the header image? I've attempted to modify the header.php and func ...

Challenges with adjusting the dimensions of varying-sized fluid squares that are floated

I've been facing a roadblock in my coding project since yesterday afternoon. I'm trying to transform a classic unordered list menu into a tiled "gallery" within a 12 column grid, with fluid square tiles. When the squares are floated, the first s ...

Conditions must fail for window.location to execute

<a href="www.google.com" class="social" id="social">Click here for Link one</a> <a href="www.cnn.com" class="social" id="social">Visit Link two now!</a> <a href="www.facebook.com" class="social" id="social">This is Link thre ...