Bootstrap stacking columns incorrectly [duplicate] - css

This question already has answers here:
Empty vertical space between columns in Bootstrap 4
(2 answers)
Closed 3 years ago.
I'm having trouble stacking my columns correctly using bootstrap - as you can see from the image I need the black box positioned below green box but I can't get this to work:
Here is the code I am using:
<div class="container-fluid">
<div class="row">
<div class="col-lg-6 col-md-12" style="background-color:blue; height:600px;"></div>
<div class="col-lg-6 col-md-12 " style="background-color:green; height:300px;"></div>
<div class="col-lg-6 col-md-12" style="background-color:black; height:300px;"></div>
</div>
</div>
Can someone please tell me where I am going wrong here?
Thanks

You have 2 columns and you put first div into 1 column and second inside the 2 one and next div will go angain inside the 1 column, the easier way to place black div below the green is to create another blank div
<div class="container-fluid">
<div class="row">
<div class="col-lg-6 col-md-12" style="background-color:blue; height:600px;"></div>
<div class="col-lg-6 col-md-12 " style="background-color:green; height:300px;"></div>
<div class="col-lg-6 col-md-12" style="height:300px;"></div>
<div class="col-lg-6 col-md-12" style="background-color:black; height:300px;"></div>
</div>
</div>

Bootstrap 4 is mobile first. So first thing is invert the order of your classes.
The problem is that you are setting the width of your boxes to be the half of the width of the row on desktop, and rows are set to wrap by default in bootstrap.
Set the columns to full width with col-12.
<div class="container-fluid">
<div class="row">
<div class="col-12" style="background-color:blue; height:600px;"></div>
<div class="col-12 " style="background-color:green; height:300px;"></div>
<div class="col-12" style="background-color:black; height:300px;"></div>
</div>
</div>
EDIT:
If you want the columns to be half of the width of the row and still prevent wrapping, you have to add an offset:
<div class="container-fluid">
<div class="row">
<div class="col-12 col-lg-6 offset-lg-3" style="background-color:blue; height:600px;"></div>
<div class="col-12 col-lg-6 offset-lg-3" style="background-color:green; height:300px;"></div>
<div class="col-12 col-lg-6 offset-lg-3" style="background-color:black; height:300px;"></div>
</div>
</div>

You can nest the blue and green column in another column.
<div class="container-fluid">
<div class="row">
<div class="col-lg-6 col-md-12" style="background-color:blue; height:600px;"></div>
<div class="col-lg-6 col-md-12 ">
<div style="background-color:green; height:300px;"></div>
<div style="background-color:black; height:300px;"></div>
</div>
</div>
This way the green and black column are contained inside the "right" side. The other answers are working too but they are changing the bootstrap grid behaviour or splitting up the content, which I don't think is ideal.

Bootstrap 4 uses Flexbox so columns aren't going to "float" next to each other as they did in Bootstrap 3. You can override this behavior by enabling floats on lg and up screen widths.
Notice use of the d-lg-block and float-left classes.
<div class="container-fluid">
<div class="row d-lg-block">
<div class="col-lg-6 col-md-12 float-left" style="background-color:blue; height:600px;"></div>
<div class="col-lg-6 col-md-12 float-left" style="background-color:green; height:300px;"></div>
<div class="col-lg-6 col-md-12 float-left" style="background-color:black; height:300px;"></div>
</div>
</div>
https://www.codeply.com/go/7KLlzbAGGU
Also see: One tall div next to two shorter divs on Desktop and stacked on Mobile with Bootstrap 4

Related

How can I change Bootstrap columns sizing based on screen size?

I have 6 items to display...
On a cell phone (xs) I want two rows of three items to display and on bigger than cell phone size, I'd like to see all six on the same row.
I tried...
<div class='row'>
<div class='col-xs-4 col-sm-2'>1</div>
<div class='col-xs-4 col-sm-2'>2</div>
<div class='col-xs-4 col-sm-2'>3</div>
<div class='col-xs-4 col-sm-2'>4</div>
<div class='col-xs-4 col-sm-2'>5</div>
<div class='col-xs-4 col-sm-2'>6</div>
</div>
This displays fine on a large screen with all 6 items but when viewing in the xs screen (phone) it just shows 6 separate rows.
I want to see
1 2 3 4 5 6
on large screen and
1 2 3
4 5 6
on xs screens.
Any ideas? Thanks
col-xs- was for Bootstrap-3. For Bootstrap-4, for the smallest screen, use col-4, so your code would look like:
<div class='row'>
<div class='col-4 col-sm-2'>1</div>
<div class='col-4 col-sm-2'>2</div>
<div class='col-4 col-sm-2'>3</div>
<div class='col-4 col-sm-2'>4</div>
<div class='col-4 col-sm-2'>5</div>
<div class='col-4 col-sm-2'>6</div>
</div>
That should give you three columns on a smartphone and six columns on anything else.
Update for space between the elements:
If you want some spacing between your elements, you could put another column inside your first column, and then put your information in that column. The normal gutter is 15px wide, and with two gutters, the space between columns would be 30px. If that’s too wide, you can customize it as explained in this answer: https://stackoverflow.com/a/27005684/1011984
As an example, I revised my first solution to include a 5px gutter (so 10px total). I also put some spacing between the elements on a smartphone, and a block below the elements to illustrate that you can get right next to them, if you want. If you don’t, just delete the mb-n3 margin on the row.
<link href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<style>
.row.gutter {
margin-left: -5px;
margin-right: -5px
}
[class*="col-"].gutter {
padding-left: 5px;
padding-right: 5px;
}
</style>
<div class="container mt-5">
<div class='row mb-n3 gutter'>
<div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
<div class="col bg-primary">1</div>
</div>
<div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
<div class="col bg-secondary">2</div>
</div>
<div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
<div class="col bg-success">3</div>
</div>
<div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
<div class="col bg-danger">4</div>
</div>
<div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
<div class="col bg-warning">5</div>
</div>
<div class='col-4 col-sm-2 mb-3 gutter text-center text-light '>
<div class="col bg-info">6</div>
</div>
</div>
</div>
<div class="container-fluid bg-dark text-light" style="height: 200px;">
<p>A filler div to show that there can be a space between the rows of elements on a small screen, but no space below the row, if you don’t want one.</p>
</div>
use col-x instead of col-xs-x. It was depreciated on bootstrap 4.
<div class="row">
<div class="col-4 col-sm-2">1</div>
<div class="col-4 col-sm-2">2</div>
<div class="col-4 col-sm-2">3</div>
<div class="col-4 col-sm-2">4</div>
<div class="col-4 col-sm-2">5</div>
<div class="col-4 col-sm-2">6</div>
</div>

Bootstrap grid: How to change the place of a column?

I have a question regarding a bootstrap grid system I am trying to achieve - and have not been successful so far.
I am trying to do the following grid system:
I have tried a code that you'll find with this JSFiddle.
<div class="container mt-5">
<div class="row">
<div class="col-lg-6 col-md-6 col-12 border bg-primary" style="height: 200px">
<h3>Bloc 1</h3>
</div>
<div class="col-lg-6 col-md-6 col-12 border bg-light">
<h3>Bloc 2</h3>
</div>
<div class="col-lg-6 col-md-12 bg-danger">
<h3>Bloc 3</h3>
</div>
</div>
</div>
It works well for xs and md system. However, I have issues with the lg version, where I don't find a way to stack the bloc number 2 (grey) on top of the bloc number 3 (red). Is there a way to change the place of such a column? As indicated on the figure above, the bloc number 1 should fall to the bottom of the page.
Thanks a lot for your help!
Basically, you have to wrap a div for a second column around the gray and red divs
<div class="container ">
<div class="row">
<div class="col-lg-6 col-md-6 col-12 border bg-primary" style="height: 200px">
<h3>Bloc 1</h3>
</div>
<div class="col-lg-6 col-md-6 col-12"><!--NEW DIV, moved column info here-->
<div class=" border bg-light">
<h3>Bloc 2</h3>
</div>
<div class=" bg-danger">
<h3>Bloc 3</h3>
</div>
</div><!--new div end-->
</div>
</div>
You will need to work out the tweaks for spacing, but this is a start

bootstrap two column in same line all devices

I am trying to use two column divisions in same line.
Check my code:
<div class="col-md-12">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
Left Div
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
Right Div
</div>
</div>
</div>
this structure responsive for till 574px. but when i reduce browser size after 574px, divisions show in two rows. but my requirement show it in same line (left & Right)
please check above image. that the issue.
You can set same divided columns for every screen using col class
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col bg-success">
Left Div
</div>
<div class="col bg-warning">
Right Div
</div>
</div>
</div>
</div>
bg-success and bg-warning is just for color
<div class="col-12 ">
<div class="row">
<div class="col-6">
Left Div
</div>
<div class="col-6">
Right Div
</div>
</div>

Bootstrap 4 layout, grid [duplicate]

This question already has answers here:
Make a div span two rows in a grid
(2 answers)
Closed 3 years ago.
I'm trying to make a layout like on the attached picture, so the column on the left span over multiple rows . At the moment my code looks like:
<div class="row my-row">
<div class="col-6 my-col">1</div>
<div class="col-6 my-col">
<div class="row my-row">
<div class="col-lg-12 my-col">2</div>
<div class="col-lg-12 my-col">3</div>
</div>
</div>
</div>
Is it any other/better way to do it?
As well I don't really understand how this code does what it does, so would be nice if someone can explain?
Thanks.
You're doing it correctly for the most part. Bootstrap applies CSS styles to components on your page based on the class name of the component. In order to get items aligned hoizontally across the page, bootstrap uses the "row" class to create a vertical row. Inside of the "row" element, bootstrap sizes elements that are tagged with "col-#" based on the weight of the # following col. col-12 is the maximum width of the container, therefore you can create up to 12 columns (12 col-1's). In your example col-6 is a column that takes up half the page. In order to split the right half of that column further, an additional row is applied, and two more columns are created. These two columns should be col-6 in order to get each column to take exactly half of that row.
After these components you just need one more row and to get 1/3 of a row you will need three col-4 elements to create the last row. (4+4+4 = col-12 = full row)
Furthermore, Bootstrap not has the ability to automatically detect the size of a column (if you want them all equal). To take advantage of this, you don't need to put any number after col, just provide an element with a class of col inside a row and bootstrap will auto-size them to be equally spaced columns.
Bootstrap has extensive documentation for more help!
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</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>
<div class="container">
<div class="row">
<div class="col-12">
<div class="row">
<div class="col-md-4 col-lg-4 col-12 col-sm-6 d-flex align-items-center">
1
</div>
<div class="col-md-8 col-lg-8 col-12 col-sm-6">
<div class="row">
<div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
2
</div>
<div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
3
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
4
</div>
<div class="col-md-6 col-sm-6 col-12 col-lg-6 d-flex align-items-center">
5
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-4 col-lg-4 col-sm-4 col-12 d-flex align-items-center">
6
</div>
<div class="col-md-4 col-lg-4 col-sm-4 col-12 d-flex align-items-center">
7
</div>
<div class="col-md-4 col-lg-4 col-sm-4 col-12 d-flex align-items-center">
8
</div>
</div>
</div>
</section>
</body>
</html>
Try this out! It'll also vertically align your contents as you've showed on the image.
For reference go through https://getbootstrap.com/docs/4.0/utilities/flex/ this link. Also if you want it to be centered horizontally use .justify-content-center
id use something like this
<div class="row">
<div class="col-4">
1
</div>
<div class="col-8">
<div class="row">
<div class="col-6">
2
</div>
<div class="col-6">
3
</div>
<div class="col-6">
4
</div>
<div class="col-6">
5
</div>
</div>
</div>
<div class="col-4">
6
</div>
<div class="col-4">
7
</div>
<div class="col-4">
8
</div>
</div>
The trick is to use rows withing another column that's already there. This will let you have 2-> 5 within what looks like a column from 1.
6->8 can just be 3 col-4's in one row / the same row

Bootstrap Responsive Columns Height

I'm using bootstrap 4 and trying to create a layout. The problem I'm having is with the responsive.
When the page size gets smaller I want the right-nav to go under the left-nav. The problem I run into is when the main body runs longer than the left nav. The right nave always goes under the main body, is there a way to force it under the left-nav without the space in between?
<div class="container-fluid">
<div class="row pt-5 pl-3 pr-3">
<div id="left-nav" class="d-none d-sm-none d-md-block col-md-4 col-lg-3 col-xl-2">
...
</div>
<div id="main-body" class="col-12 col-sm-12 col-md-8 col-lg-9 col-xl-8">
...
</div>
<div id="right-nav" class="d-none d-sm-none d-md-block col-md-3 col-lg-3 col-xl-2">
...
</div>
</div>
<section id="footer" class="footer">
<div class="container">
<div class="row">
</div>
</div>
</div>
</div>
Since Bootstrap 4 uses flexbox, cols across a row are always going to be the equal height (set by the height of the tallest).
You can workaround this by "disabling" flexbox at certain breakpoints using d-(breakpoint)-block on the row. By making the row display:block instead display:flex, floats can now be used to float cols to the right or left.
<div class="container-fluid">
<div class="row pt-5 d-md-block d-xl-flex">
<div id="left-nav" class="d-sm-none d-md-block col-md-4 col-lg-3 col-xl-2 border float-left">
left
</div>
<div id="main-body" class="col-md-8 col-lg-9 col-xl-8 border taller float-right">
main
</div>
<div id="right-nav" class="d-sm-none d-md-block col-md-4 col-lg-3 col-xl-2 border">
right
</div>
</div>
</div>
Demo: https://www.codeply.com/go/A0oYUlPOud
Related:
How to fix unexpected column order in bootstrap 4?
How to make this column ignore the vertical space

Resources