Exploring the relationship between offset and margin in Bootstrap

I'm currently experimenting with offset and margins in Bootstrap 4, trying to achieve a layout similar to the image provided. Everything is working as expected except for the white spaces between the boxes. When I adjust the margins using mr-md-2, the blue box moves down instead of staying next to the yellow box like in the picture. How can I ensure consistent white spaces between all boxes at different screen sizes?

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"></script>
 <link rel="stylesheet" href="css/bootstrap.css">

  <style>

  </style>

</head>

<body>
<div class="container-fluid">
  <div class="row grid " >
    <div class="col-xs-12 col-sm-6 col-md-4 mr-md-2 col-lg-3 offset-lg-3 "  style="background-color:burlywood; height:200px;"></div>
    <div class="col-xs-12 col-sm-6 col-md-4 ml-md-2 col-lg-3 "  style="background-color:orange; height:200px;"></div>
  </div>

  <div class="row grid">
    <div class="col-xs-12 col-sm-6 mt-md-2 mt-md-3 col-md-4 col-lg-6 " style="background-color:black;height:200px; "></div>
    <div class="col-xs-12 col-sm-6 col-md-4 mt-md-2 offset-md-4 col-lg-3 offset-lg-3 " style="height:200px;background-color:red; "></div>
  </div>

  <div class="row grid">
    <div class="col-xs-12 col-sm-6 col-md-4 mt-md-2 offset-md-4 col-lg-3 offset-lg-0 "  style="background-color:yellow;height:200px; "></div>
    
    <div class="col-xs-12 col-sm-6 col-md-4 mt-md-2 mr- col-lg-3 mr-lg-0 offset-lg-6 " style="background-color:blue; height:200px;"></div>
  </div>
</div>
  <script src="js/jquery-3.3.1.min.js"></script>
<script src="js/bootstrap.js"></script>
</body>
</html>

Answer №1

Keep in mind that the BS Grid system does not have any margins between columns (or rows for that matter), but there are gutters:

Try running this code snippet in full screen and resize your window to see the effect:

.row > div {
  /* background-color: lightsteelblue; */
  margin-top:20px;
}

.content {
  background-color: burlywood;
  height: 200px;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous">

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js" integrity="sha384-smHYKdLADwkXOn1EmN1qk/HfnUcbVRZyYmZ4qpPea6sjB/pTJ0euyQp0Mk8ck+5T" crossorigin="anonymous"></script>


<div class="container-fluid">
  <div class="row">
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 offset-lg-3">
      <div class="content">r1c1</div>
    </div>
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3">
      <div class="content">r1c2</div>
    </div>
  </div>

  <div class="row">
    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-6">
      <div class="content">r2c1</div>
    </div>
    <div class="col-xs-12 col-sm-6 col-md-4 offset-md-4 col-lg-3 offset-lg-3 ">
      <div class="content">r2c2</div>
    </div>
  </div>

  <div class="row">
    <div class="col-xs-12 col-sm-6 col-md-4 offset-md-4 col-lg-3 offset-lg-0 ">
      <div class="content">r3c1</div>
    </div>

    <div class="col-xs-12 col-sm-6 col-md-4 col-lg-3 offset-lg-6">
      <div class="content">r3c2</div>
    </div>
  </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

Having trouble with a nonresponsive link on a website? Unable to hover over it

The website can be found at: I've been trying to click on the "Home" link on the left side, but it doesn't seem to work for me. Any thoughts on why this might be happening? ...

Using percentages for sizing and margins in CSS

Looking to create a visually appealing page that always fills the entire screen? Check out this code snippet that does just that: #posts{ width:100%; height:auto; background:#666; } .entry{ float:left; margin-left: 4%; margin-top:10 ...

Display sub navigation when clicked in WordPress

I currently have the default wordpress menu setup to display sub navigation links on hover, but I am interested in changing it so that the sub navigation only appears when the user clicks on the parent link. You can view my menu here https://jsfiddle.net/f ...

Tips for saving an image file using the source I've generated from my drawing on an HTML 5 canvas

Recently, I utilized HTML 5 canvas to draw an image using JavaScript. After completing the drawing, I needed to save the canvas on my hard disk. To accomplish this, I followed a method to obtain the image source: var img = canvas.toDataURL(); Subsequentl ...

ways to utilize inline styling in react using javascript

When repurposing an array of React components, I am looking to modify the inline styles generated with them. How can I access and log the inline styles of a React component? UPDATE: see the code snippet below: const pieces = this.props.pieces.map((decl, ...

Minifying HTML, CSS, and JS files

Are there any tools or suites that can minify HTML, JavaScript, and CSS all at once? Ideally, these tools should be able to: Identify links from the HTML document and minify the associated JavaScript and CSS. Remove any unused JavaScript functions and CS ...

Choosing a nested object within a JavaScript object

Is there a way to select the style contents of an object within another object? The data consists of a json encoded array. In my scenario, I am using data[i].replies and getting 2. How can I access data[i].userstyles instead? It seems to be an object. h ...

Form comments in Bootstrap are causing rows to shift

Currently, I am working on an Angular-powered horizontal form that utilizes various bootstrap components. However, I have encountered an issue with a "Comment field" that is causing the adjacent columns to shift downwards. I initially suspected the problem ...

The outline of a box with the title "CSS, FrontEnd Dev - UX" is a key

Struggling with UX design here. Working on an app in React with Material UI, aiming for a look similar to this: https://i.sstatic.net/h1alm.png My goal is to make the "Some Title" section dynamic from my database, along with the contents. Can't seem ...

Printing tables in PHP

I'm currently displaying a table with categories and subcategories using a looping function to navigate through the hierarchy. Below is the script markup: <table border="1" width="100%" cellspacing="0" cellpadding="2"> <tr class="dataTab ...

Conceal pseudo-elements using JavaScript

How can I control the visibility of the :before and :after pseudo elements using JavaScript? I currently have arrows displayed on both sides of a div. Here is the CSS styling for the arrows: .edit-wrap:before, .edit-wrap:after { @media (max-width: ...

Does setting white-space:nowrap enlarge the div?

My CSS code for div .name sets the width to 75% to truncate text if it doesn't fit. However, resizing the window causes the text to remain the same size and my entire website structure falls apart. This is the HTML code snippet: <tr> <t ...

Facing a challenge with implementing cascading dropdown lists in asp.net using C#

Hello everyone, I am currently working on implementing a cascading dropdown list in Razor Pages using PostgreSQL. The first dropdown displays the location, and the second dropdown shows the room corresponding to the selected location. However, I have reach ...

Is it possible to convert the text.json file into a jQuery animation?

Looking to extract information from text.json and integrate it with my jquery.animation(). My goal is similar to the one demonstrated here. Instead of using "data" attributes like in that example, I want to parse the text based on the "ID" property as a k ...

What is the optimal resolution for a background image in a Cordova app?

Currently working on a Cordova application that requires different background images for various screens. Despite using media queries to provide the background image in different resolutions, we are facing an issue where the background image is not displ ...

Having trouble with clearing divs function not working as expected

I'm facing a challenge in properly aligning 3 div elements without resorting to adding padding at the bottom of the wrapping div, which isn't the most practical solution. Check out the issue demo here How would you suggest addressing this parti ...

Receive alerts in Swift from WKWebView when a particular screen is displayed in HTML

Check out this sample HTML file I have. I'm currently using the WKWebView to display this HTML. My goal is to receive a notification in our Swift code when the user finishes the game and the "high score" screen appears, so we can dismiss the view and ...

Updating a URL in an HTML form with JavaScript

Currently, I am faced with the task of manipulating a variable's value in a JS file within a function that is responsible for dynamically updating values in an HTML table without necessitating a full website reload. The function code snippet appears b ...

What are your thoughts on using inline PHP to assign values to JavaScript variables?

Imagine you have a PHP document and need to determine if the request was made with or without query parameters using JavaScript. Since there is no built-in function to extract values from the URL, you may need to resort to some sort of workaround. While it ...

Troubleshooting: jQuery Drop event is not triggering

I am attempting to transfer links (.link) from one div (.folder) to another, but the drop event is not triggering. To make all .link divs droppable areas, I have disabled the default behavior in the dragenter and dragover events. Here is the code snippet: ...