Looking under the hood of Bootstrap gutters led me to realize that the space between columns is added using padding, not a margin.
This is problematic because if you want to use Bootstrap 5's default flexbox to get your columns of equal height, you have to apply things like box shading or background to the column itself. This means that a gutter using padding gives you an awkward white gap within your column, rather than space between the columns.
Here's a fiddle that demonstrates my issue:
.red {
background-color: red;
color: white;
}
.blue {
background-color: blue;
color: white;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.0/dist/css/bootstrap.min.css">
<h2>
No Gutters
</h2>
<div class="container px-5">
<div class="row ">
<div class="col-6 red">
<p>Column content</p>
<p>is different</p>
<p>heights.</p>
</div>
<div class="col-6 blue">
<p>See? Shorter.</p>
</div>
</div>
</div>
<h2>
Gutters via Padding
</h2>
<div class="container px-5">
<div class="row gx-4">
<div class="col-6 red">
<p>Column content</p>
<p>is different</p>
<p>heights.</p>
</div>
<div class="col-6 blue">
<p>See? Shorter.</p>
</div>
</div>
</div>
<h2>
Gutters fine but BG Wonky
</h2>
<div class="container px-5">
<div class="row gx-4">
<div class="col-6">
<div class="red">
<p>Column content</p>
<p>is different</p>
<p>heights.</p>
</div>
</div>
<div class="col-6">
<div class="blue">
<p>See? Shorter.</p>
</div>
</div>
</div>
</div>
Is there any way to get space between columns that acts as a margin not padding? If you add a straight-up margin it pushes the whole grid layout out of whack.
Related
Okay, this is my 3rd question and hopefully the last one. I get some problems with my page layout that looked like this before:
[navbar]
[1][2][3]
I wanted to achieve this effect when resizing:
[navbar]
[2][3]
[1]
(1st div under the 3rd one)
However, when I use (in the main container that holds all the 3 divs) class container mt-3 I get this layout:
[navbar]
[1][2]
[3]
Now, when I change the margin-left of the 3rd div it doesn't go up to the top row but just moves to the left - something happens like it would be in another row class.
When I use parent-container container mt-3
[navbar]
[1][2] [3]
And they are really small and I can't change the width no matter what I do. I even tried changing the bootstrap's "container" class with both px and %.
container-fluid does the job a bit because all the divs are bigger (and on 100% width of the page) but I would want to have it just like 90%, but still, the [3] div is on the right side mile from the [2]. When I resize the window, the [2] div goes in this white space between [1] and [3] under the [3]. So it looks similar to this:
[navbar]
[2] [3]
[1]
CSS CODE:
.main{
background-color: red;
box-shadow: 3px 3px 20px #000000;
border-radius: 20px;
}
.left{
background-color: green;
max-width: 200px;
border-radius: 20px;
}
.right{
background-color: blue;
max-width: 200px;
border-radius: 20px;
}
.parent-container {
display: flex;
}
.right-content{
width: 100%;
text-align: center;
float: center;
margin-top: 10px;
}
HTML
<div class="parent-container container mt-3">
<div class="row">
<div class="left col-4 col-lg-3 order-last order-lg-first offset-8 offset-lg-0">
<div class="col-12 text-center" style="margin-bottom: 15px">
<h3>TITLE LEFT</h3>
</div>
</div>
<div class=" col-8 col-lg-6">
<div class="container card">
<div class="card-body">
<div class="row">
<div class="col-12 text-center">
<h2>TITLE CENTER</h2>
</div>
</div>
<div class="row">
<div class="col-12 text-center">
<h3>heading 3</h3>
<h6>heading 6</h6>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12 mx-auto">
<a href="#" class="article-link">
<h3>Heading 3</h3>
<p>Text</p>
</a>
</div>
</div>
<hr>
</div>
</div>
<div class="right col-4 col-lg-3">
<div class="right-content">
<h2>TITLE RIGHT</h2>
</div>
</div>
</div>
You had improper nesting for the columns. Here's the right way to do it (all main columns must sit within the main row):
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<style>
.left{
background-color: green;
max-width: 200px;
border-radius: 20px;
}
.right{
background-color: blue;
max-width: 200px;
border-radius: 20px;
}
</style>
<div class="parent-container container mt-3">
<div class="row">
<div class="left col-4 col-lg-3 order-last order-lg-first offset-8 offset-lg-0">
<h3>TITLE LEFT</h3>
</div>
<div class=" col-8 col-lg-6">
<div class="container2 card">
<div class="card-body">
<div class="row">
<div class="col-12 text-center">
<h2>TITLE CENTER</h2>
</div>
</div>
<div class="row">
<div class="col-12 text-center">
<h3>heading 3</h3>
<h6>heading 6</h6>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-12 mx-auto">
<a href="#" class="article-link">
<h3>Heading 3</h3>
<p>Text</p>
</a>
</div>
</div>
<hr>
</div>
<div class="right col-4 col-lg-3">
<div class="right-content">
<h2>TITLE RIGHT</h2>
</div>
</div>
</div>
</div>
Note: Right now the columns aren't stacking on the smallest screens. That's because they aren't specified to do so. To make them stack, the responsive column classes for the smallest screens (col-*) all need to be changed to col-12.
You'd also need to adjust the offset classes because right now the offset-8 class kicks in for all screens that are smaller than lg. The offset-8 class is what causes the offset of 8 units (on the left) on all screens that are smaller than lg.
Bootstrap 4 - Safari Version 10.1.2 display:table; and display:table-cell; vertical-align:center; not working on a 100% height div.
<div class="test-slider">
<div class="slide">
<div class="row no-gutters">
<div class="col-12 col-xs-12 col-sm-12 col-md-7 col-lg-7">
<img src=""/>
</div>
<div class="col-12 col-xs-12 col-sm-12 col-md-5 col-lg-5">
<div style="display:table; height:100%;">
<div style="display:table-cell; vertical-align: middle;">
<h2>Test center</h2>
<p>Should be centered</p>
</div>
</div>
</div>
</div>
</div>
</div>
If I render in chrome the height is inherited from the .test-slider class that has a fixed height whilst in safari it takes the contents height rather than filling the height of the div. Any ideas? I am using slick slider.
Well, currently I have Safari 11.0.1, but if I set the below stlye, it does center the text vertically to me.
.test-slider .row {
height: 500px;
}
Setting the height only to .test-slider is not enough, since the children do not know that they should fill in that height. The 100% height set on the "table" is calculated from the height of it's parent, the .col-md-5 div. Which, in your case does not know about the height of the .test-slider.
Other than that, please note that col-12 col-xs-12 col-sm-12 col-md-7 col-lg-7 is totally equivalent to simply writing col-md-7. The same stands for col-md-5 of course.
Also, instead of using tables, I would recommend to use the flexbox utility classes already available in Bootstrap. That makes the markup lighter. This is an example:
.test-slider .row {
height: 300px;
}
.test {
background-color: lightgray;
}
<div class="test-slider">
<div class="slide">
<div class="row no-gutters">
<div class="col-md-7">
<img src=""/>
</div>
<div class="test col-md-5 d-flex align-items-center">
<div>
<h2 class="mb-0">Test center</h2>
<p class="mb-0">Should be centered</p>
</div>
</div>
</div>
</div>
</div>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css">
I have been using html/css for a while now. And I have never ran into this kind of grid problem with Bootstrap 3. I am trying to find a way to combine a 50% width (inside a <div class="container-fluid">) with a 50% width div (inside a <div class="container">). Like the grid in the following picture:
Example of the template
Yellow overlay: <div class="container"></div>
The problem is that they won't work together cause of the widths.
So how do I simulate this?
<section>
<div class="container-fluid">
<div class="col-sm-12 col-md-6 no-padding"></div>
<div class="container">
<p>content</p>
</div>
</div>
<div class="col-sm-12 col-md-6 no-padding photo"></div>
</div>
</section>
If I'm understanding what you're trying to achieve, there are a couple of problems. Firstly, you still need a row div to wrap the columns, which is in turn nested in the container. Also, by default, the container would have some padding, so that would need to be removed with some basic CSS as well.
Here is the HTML structure
<div class="container-fluid">
<div class="row">
<div class="col-lg-6" id="left">
Left Side
</div>
<div class="col-lg-6" id="right">
Right Side
</div>
</div>
</div>
Also, here's a live example you can play with: http://www.bootply.com/MKe7aJwKuc
Let me know if I missed the mark on what you're trying to do and I can try to rework it and help you out.
Should be this schema
<section>
<div class="container-fluid">
<div class="col-sm-12 col-md-6 no-padding"></div>
<div class="col-sm-12 col-md-6 no-padding photo"></div>
<div class="container">
<div class= "row">
<div class="col-sm-12 col-md-6 no-padding">Content</div>
<div class="col-sm-12 col-md-6 no-padding photo"></div>
</div>
</div>
</div>
</section>
on Bootstrap 4
<div class="container">
<div class="row">
<div class="col-lg-6">
<div class="elem left">Left Side</div>
</div>
<div class="col-lg-6">
<div class="elem right">Right Side</div>
</div>
</div>
</div>
sass
#mixin fluidwidth($widths: $container-max-widths, $breakpoints: $grid-breakpoints) {
#each $breakpoint, $container-max-width in $widths {
#include media-breakpoint-up($breakpoint, $breakpoints) {
width: calc(100% + ((100vw - #{$container-max-width})/2) );
}
}
}
.elem.right{#include fluidwidth;}
I tried using the following code, but can't make equal margin to each box
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 box"></div>
</div>
<div class="row">
<div class="col-xs-6 box"></div>
<div class="col-xs-6 box">
<div class="row">
<div class="col-xs-12 box"></div>
</div>
<div class="row">
<div class="col-xs-12 box"></div>
</div>
</div>
</div>
</div>
Space between rows
There isn't anything built-in (for version 3 according to the provided tag on question) for that much vertical space, so you'll want to stick with a custom class.
HTML
<div class="row vertical-space">
...
</div>
CSS
.vertical-space{
margin-bottom: 15px;
}
Please note that, if you are putting those images as CSS background-image for the <div class="row">, then you need to replace padding width margin in the above snippet.
Space between columns
You should work with padding on the inner container to gain space between columns.
HTML
<div class="col-xs-6 box">
<div class="row horizontal-space">
...
</div>
</div>
CSS
.horizontal-space{
padding: 0 15px;
}
All bootstrap columns have 15 pixel left and right padding and the rows have -15 pixel margins.
This means that by default the gap between columns will be 30 pixels but the columns at either end will have their outer edges offset into the rows.
What you can do is remove the right (or left) padding from each of your boxes, however you would need to adjust the row to make sure that the padding is compensated for or it will cause horizontal scrolling. You'd also need to add 15 pixels to the bottom of each box to get your vertical gaps. See below.
<div class="container-fluid">
<div class="row box-row">
<div class="col-xs-12 box"></div>
</div>
<div class="row box-row">
<div class="col-xs-6 box"></div>
<div class="col-xs-6 box">
<div class="row box-row">
<div class="col-xs-12 box"></div>
</div>
<div class="row box-row">
<div class="col-xs-12 box"></div>
</div>
</div>
</div>
</div>
And the css would be like this.
.box-row {
margin-right:0;
}
.box {
padding-right:0;
padding-bottom:15px;
}
Im a little confused why bootstrap wont apply left and right margins to the columns, all i want is a simple row with 3 columns that have like 20px horizontal margin in between each other. If try to do that bootstrap simply clips one of the divs to the next line.
html
<div class="container">
<div class="row">
<div class="col-md-4"><p>Box 1</p></div>
<div class="col-md-4"><p>Box 1</p></div>
<div class="col-md-4"><p>Box 1</p></div>
</div>
</div>
css
.col-md-4 {
background-color: tomato;
margin: 20px 5px;
}
Left and right margins to the columns would destroy Bootstrap's grid behaviour. Don't modify Bootstrap's classes. Add your own classes instead.
In your example put an extra <div> in the column. And give it a margin.
HTML
<div class="container">
<div class="row">
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
<div class="col-md-4">
<div class="spacer">
<p>Box 1</p>
</div>
</div>
</div>
</div>
CSS
.spacer {
background-color: tomato;
margin: 20px 5px;
}
Demo
for a little more spacing you could do:
<div class="col-md-3 col-md-offset-1"><p>Box 1</p></div>
<div class="col-md-3 col-md-offset-1"><p>Box 1</p></div>
<div class="col-md-3 col-md-offset-1"><p>Box 1</p></div>
Bootstrap slips one in new row because it column layout is 12 columns max.
When you have 3 divs * (4 col width + margin-right) you excess the provided 12 columns.
You can set your div a little bit thinner, i.e. 3 medium bootstrap columns, like this:
<div class="col-md-3 custom-box"><p>Box 1</p></div>
<div class="col-md-3 custom-box"><p>Box 1</p></div>
<div class="col-md-3 custom-box"><p>Box 1</p></div>
and apply your css rule like this:
.custom-box{
margin-right: 20px 5px!important;
background-color: tomato;
}
here is a FIDDLE