Space between elements in a horizontal arrangement

While working with Bootstrap 5 and using paddings in columns, I encountered issues when some of my columns had backgrounds or when adding an embed element. How can I create margins in such cases?

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

To address this issue, I implemented a workaround by assigning different classes to my three gray blocks and applying left and right margins as needed for each.

I initially wanted to structure it like this:

<div class="row">
        <div class="col-12">
            My blue section
        </div>
    </div>

    <div class="row">
        <div class="col-4">
            Grey section 1
        </div>
        <div class="col-4">
            Grey section 2
        </div>
        <div class="col-4">
            Grey section 3
        </div>
    </div>

However, I realized that I would not have margins between my gray items using this approach. So, I tried encapsulating each gray item like this:

<div class="row">
        <div class="col-4">
            <div class="item">
                Grey section 1
            </div>
        </div>
        <div class="col-4">
            <div class="item">
                Grey section 2
            </div>
        </div>
        <div class="col-4">
            <div class="item">
                Grey section 3
            </div>
        </div>
    </div>

However, this solution also presented challenges as the margins from the row affected the overall width difference between the gray sections and the blue section.

I faced similar issues with videos: if I don't wrap the video in a child div, there are no margins causing alignment problems.

Any suggestions on how to address these issues effectively?

Answer №1

If I've got it right, you're referring to the spacing between the column items.

With Bootstrap columns now being managed through CSS flexbox since version 4, achieving your objective is simple by utilizing flexbox utilities as well.

In this scenario, all you have to do is set the gap (or column-gap) property for your second row element. Instead of using fixed column size classes (col-x), opt for the auto-sized col class.

.my-gap-row {
    gap: 20px;
}
<div class="row">
    <div class="col">
        My blue section
    </div>
</div>

<div class="row my-gap-row">
    <div class="col">
        Grey section 1
    </div>
    <div class="col">
        Grey section 2
    </div>
    <div class="col">
        Grey section 3
    </div>
</div>

Answer №2

By incorporating the css tag, an alternative css solution can be explored. A property known as gap is available when utilizing flexbox, grid, and multiple columns, offering a simple way to create spaces between containers. You can observe the code snippet below where I have recreated your design layout. For further details on the gap property, visit this link.

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

.container {
  display: grid;
  place-content: center;
  grid-auto-flow: column;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  height: 100vh;
  padding: 1rem;
  background-color: #2B2954;
  gap: .5rem;
}

.blue {
  border-radius: 10px;
  background-color: #18161B;
  grid-column: span 3;
  grid-row: span 2;
}

div.box:nth-child(2) {
  border-radius: 10px;
  background-color: #393D41;
  grid-column: 1/2;
  grid-row: 3/4;
}

div.box:nth-child(3) {
  border-radius: 10px;
  background-color: #393D41;
  grid-column: 2/3;
  grid-row: 3/4;
}

div.box:nth-child(4) {
  border-radius: 10px;
  background-color: #393D41;
  grid-column: 3/4;
  grid-row: 3/4;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="style.css">
</head>

<body>
  <div class="container">
    <div class="blue"></div>
    <div class="box"></div>
    <div class="box"></div>
    <div class="box"></div>
  </div>
</body>

Answer №3

Your attempt at creating space between columns was a step in the right direction, but there are some adjustments that need to be made. Replace row with d-flex justify-content-between and col-4 with something like col-3. This will create spacing between the columns and ensure that the entire element fills 100% of the parent element's width.

<div class="d-flex justify-content-between">
        <div class="col-3">
            <div class="item">
                Grey section 1
            </div>
        </div>
        <div class="col-3">
            <div class="item">
                Grey section 2
            </div>
        </div>
        <div class="col-3">
            <div class="item">
                Grey section 3
            </div>
        </div>
    </div>
</div>

To add vertical gap between rows, simply include any mt class to the main 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

Loading content dynamically into a div from an external or internal source can greatly enhance user experience on a website. By

As I work on my website, I am incorporating a div structure as shown below: <div class="container"> <div class="one-third column"> <a id="tab1" href="inc/tab1.html"><h2>tab1</h2></a> </div> & ...

The dropdown in MaterializeCSS does not display any data retrieved from VUE

Good evening. I am currently utilizing VUE along with MaterializeCSS. I have been trying to populate a selection menu without success. Although I am receiving the data from the database correctly, the options in the select tag remain blank when using V-F ...

Help needed with debugging CSS for IE6 >_<

I have been working on the following website: www.darksnippets.com While the design looks good on Firefox and Chrome, it appears terrible on IE6. For example, on the home page and other pages like , the width is too wide in IE6. I've been unable to ...

Collapse the Bootstrap Menu upon selecting a Link

I am working on a side menu with an offcanvas display feature. The current functionality is such that when the button is clicked, the canvas pushes left and opens the menu. However, I want the menu to collapse and close automatically when a link is selecte ...

Adjust color in real-time with JavaScript

I am using a json file to store data for generating a diagram, and I want to change the color of the diagram conditionally based on an attribute in the json. If the attribute is false, the color should be red, and if true, it should be green. Here is a sni ...

The scrollbar track has a habit of popping up unexpectedly

Is there a way to prevent the white area from showing up in my divs where the scroll bar thumb appears? The issue seems to occur randomly on Chrome and Safari. https://i.sstatic.net/jFzTf.png This is the div in question .column-content{ height:60vh; ...

Issue with specific selectors causing React CSS module malfunction

Currently, I am a beginner in learning React and have been experimenting with CSS modules. Even though Menu.module.css is mostly functioning correctly, it seems to be having an issue applying styles to the .menu a.last selector for some reason (although i ...

Enhance the appearance of the standard black rectangle displaying the messages "No video source" or "No video permissions"

Is there a way to change the styling of the video element that appears as a black rectangle with a strikethrough play button when the user is prompted for permission on Safari? Does it have a specific ID, class, or tag that can be targeted? I'm curre ...

Ignore the initial children of the highest level divs exclusively

Is there a way to apply a style to all top-level divs within a div, excluding the first one? I have tried using not(:first-child) but it seems to be recursive. How can this be achieved? <div class='want-to-skip-first-a'> <div class= ...

Experience the magic of a customized cursor that disappears with a simple mouse movement in your website,

I have been experimenting with designing a custom cursor for my website. After finding a design I liked, I made some adjustments to suit my needs. However, an issue I encountered is that when I scroll, the custom cursor also moves away from its original po ...

styling the search input element in webkit with right-to-left text direction

I have included a search input in my code like this: <input type="search" placeholder="search" /> Webkit has different styles for an input field with the type 'search'. One of these styles is displaying a cancel ('x') button on ...

At a specific media query breakpoint, links are disabled while the rest continue to function without any

Today I encountered a puzzling issue on my website with a responsive layout that utilizes @media queries and breakpoints. At one specific breakpoint (or resolution, as I am still learning the terminology), the links in the navigation bar are inexplicably d ...

HTML: Unable to align text in the center using the <div> tag

Working on a website for our Roblox group I've been attempting to center this text (and the button below), but it just doesn't look right... Check out this image Although it technically "centers", something still seems off. I've searched h ...

Strategies for making a child div fade out when the parent div is hovered over

I have a div with the class name ordershape and inside it, there is another div called fad-res. My goal is to display the corresponding fad-res when I hover over a specific ordershape, while hiding the other divs. <div class="ordershape"> & ...

What could be causing a CSS transition to fail when hovering over a different image source?

Looking to change the image source of an <a> tag when hovering over it, while also incorporating a transition effect. It seems that the transition effect is effective with the background-image property for a <div> tag, but not with the conten ...

What is the relationship between html, body, and window when scrolling through code?

I've been working on adding a scroll-to-top button to my website. After doing some research online, I came across this code snippet: $('html, body').animate({scrollTop:0}, 'slow'); It's interesting that they are trying to scr ...

Are you curious about how jQuery can be used to group buttons together?

In my ASP.NET MVC application, I incorporate JQUERY to enhance the user experience. I am curious if there is a way to group buttons together, not radio buttons but regular buttons. Can I have buttonA + buttonB + buttonC grouped together with some space i ...

Ensuring that a header one remains on a single line

Within this div, I have set the width and height to be 250px. Specifically, the h1 element inside the div has a height of 50px. Users who are updating content on my website can input any text they desire within this box. However, when the text within the ...

What is the best way to remove the box-shadow from a React-Bootstrap Dropdown Button?

I'm attempting to remove the box shadow or second border from the bootstrap dropdown button when it is clicked. I have been unable to identify the specific class responsible for this change. https://i.sstatic.net/9wxRm.png ...

Ways to adjust the font size of mat-menu-item?

I came across a query regarding this matter on another platform: How can the font size of mat-menu-item be changed to small in Angular? Unfortunately, the solution provided did not work for me. I attempted to implement the suggested code in my Angular a ...