How to wrap 6 columns of col-3 on 2 rows - css

Is it possible to make 6 col-3 columns wrap the last 3 onto a second row and have them centered?
Basically, I need a way to offset the first column by col-1.5
<div class="row mx-n2 d-flex justify-content-start justify-content-lg-center">
<div class="col-6 col-lg-3"></div>
<div class="col-6 col-lg-3"></div>
<div class="col-6 col-lg-3"></div>
<div class="col-6 col-lg-3"></div>
<div class="col-6 col-lg-3"></div>
<div class="col-6 col-lg-3"></div>
</div>

You should be able to do something like this:
.center {
margin: 0 auto;
}
<div class="row">
<div class="center">
<div class="col-md-3">Div 1</div>
<div class="col-md-3">Div 2</div>
<div class="col-md-3">Div 3</div>
</div>
</div>
<div class="row">
<div class="center">
<div class="col-md-3">Div 1</div>
<div class="col-md-3">Div 2</div>
<div class="col-md-3">Div 3</div>
</div>
</div>

An alternative to adding multiple .rows would be using a .w-100 div that is only visible for large devices (.d-none for generally hiding it and .d-lg-block) for displaying it, resulting in breaking the line for large screens).
Combining this with .justify-content-center results in your desired behavior. A minimal working example is listed below:
.col-6 {
border: 1px solid gray;
}
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js#1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap#4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.0.0/dist/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="row justify-content-center">
<div class="col-6 col-lg-3">1</div>
<div class="col-6 col-lg-3">2</div>
<div class="col-6 col-lg-3">3</div>
<div class="w-100 d-none d-lg-block"></div>
<div class="col-6 col-lg-3">4</div>
<div class="col-6 col-lg-3">5</div>
<div class="col-6 col-lg-3">6</div>
</div>
Result for small devices:
Result for large devices:

You can add this in between:
<div class="w-100"></div>
Like:
<div class="row mx-n2 d-flex justify-content-start justify-content-lg-center">
<div class="col-6 col-lg-3"></div>
<div class="col-6 col-lg-3"></div>
<div class="col-6 col-lg-3"></div>
<div class="w-100"></div>
<div class="col-6 col-lg-3"></div>
<div class="col-6 col-lg-3"></div>
<div class="col-6 col-lg-3"></div>
</div>
Check this: https://getbootstrap.com/docs/4.1/layout/grid/#equal-width

All you need is just use justify-content-lg-center.
Bootstrap use 12 grid system so all column which expand that limit for example here, last three column will automatically goes to new row.
div.col-lg-3 {
border: 1px solid;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" />
<div class="container">
<div class="row justify-content-lg-center">
<div class="col-6 col-lg-3">1</div>
<div class="col-6 col-lg-3">2</div>
<div class="col-6 col-lg-3">3</div>
<div class="col-6 col-lg-3">4</div>
<div class="col-6 col-lg-3">5</div>
<div class="col-6 col-lg-3">6</div>
</div>
</div>

Related

Inter-column padding in Bootstrap 4 without side margins

I'm using Bootstrap 4.1. I'd like to have a grid of cells (Box A-E) with some spacing in between them. If I set padding on each column, I also get padding on the left and right side that breaks alignment with other elements; I would like Box A and Box D to align with the left edge of Top Box, for example.
The documentation talks about using a negative margin to offset this but I couldn't figure out where to put it.
The set of boxes is dynamic (there could be dozens) and I'd like to apply consistent styling to all of them, rather than make modifications to specific boxes.
I also want to avoid making modifications to Top Box, which is a stand-in for many existing UI elements in my real page that cannot easily be changed.
Here's a JSFiddle:
https://jsfiddle.net/u5094cjb/2/
body {
margin: 1rem;
}
.box {
border: 1px solid black;
background-color: white;
margin-bottom: 0.1rem;
}
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<div class="container">
<!-- ideally don't change this part -->
<div class="row">
<div class="col">
<div class="box">Top Box</div>
</div>
</div>
<!-- change this part only -->
<div class="row">
<div class="col">
<div class="container">
<div class="row">
<div class="col-4 px-1">
<div class="box">Box A</div>
</div>
<div class="col-4 px-1">
<div class="box">Box B</div>
</div>
<div class="col-4 px-1">
<div class="box">Box C</div>
</div>
<div class="col-4 px-1">
<div class="box">Box D</div>
</div>
<div class="col-4 px-1">
<div class="box">Box E</div>
</div>
</div>
</div>
</div>
</div>
</div>
For what you're trying to accomplish it seems like you're using a bit "too much" code. I cleaned it up a bit so now that you only have two rows and added px-1 to your first col
body {
margin: 1rem;
}
.box {
border: 1px solid black;
background-color: white;
margin-bottom: 0.1rem;
}
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
</head>
<div class="container">
<div class="row">
<div class="col px-1">
<div class="box">Top Box</div>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-4 px-1">
<div class="box">Box A</div>
</div>
<div class="col-4 px-1">
<div class="box">Box B</div>
</div>
<div class="col-4 px-1">
<div class="box">Box C</div>
</div>
<div class="col-4 px-1">
<div class="box">Box D</div>
</div>
<div class="col-4 px-1">
<div class="box">Box E</div>
</div>
</div>
</div>
Use the Bootstrap classes pr-1 & pl-0 instead of px-1 to Box A and Box D
For example,
<div class="col-4 px-1">
<div class="box">Box B</div>
</div>
<div class="col-4 px-1">
<div class="box">Box C</div>
</div>
<div class="col-4 pr-1 pl-0">
<div class="box">Box D</div>
</div>
<div class="col-4 px-1">
<div class="box">Box E</div>
</div>
Fiddle: https://jsfiddle.net/kongallis/cebqw7y8/2/

Bootstrap responsive container (like columns)

This might be a bit of a silly question - but I could not find an answer to what I'm trying to do..
I'm trying to make the entire container div responsive (like a column inside a row).
e.g. I want to have the effect that this would produce:
<div className="container">
<div className="row justify-content-center">
<div className="col-lg-6 cold-sm-10">
...
</div>
</div>
</div>
but in one line... something like:
<div className="container-lg-6 container-sm-10">
...
</div>
Is there something in Bootstrap that could achieve this?
I tried the classes container-sm but they are not working like what I want.
That is not how the containers work. But you can easily achieve the layout by just nesting everything inside of a .col-12.col-sm-10.col-lg-6 inside a .row inside a .container-fluid
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-KyZXEAg3QhqLMpG8r+8fhAXLRk2vvoC2f3B09zVXn8CA5QIVfZOJ3BCsw2P0p/We" crossorigin="anonymous">
<div class="container-fluid p-0">
<div class="row justify-content-center">
<div class="col-12 col-sm-10 col-lg-6">
<div class="row">
<div class="col bg-primary">
<h1>1</h1>
</div>
<div class="col bg-secondary">
<h1>2</h1>
</div>
<div class="col bg-info">
<h1>3</h1>
</div>
</div>
<div class="row">
<div class="col bg-info">
<h1>4</h1>
</div>
<div class="col bg-primary">
<h1>5</h1>
</div>
</div>
</div>
</div>
</div>

Best practice to center a div on Bootstrap 4 and use rows within it

I am practising in Bootstrap 4 and I do not know what rule I have violated for the responsive design. I am looking for a best practice to carry out the image. My idea is to learn.
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet" />
<body>
<div class="container-fluid h-100" style="border: solid red 1px">
<div class="row h-100 d-flex justify-content-center align-items-center contenedor_centrado">
<div class="col-6">
<div class="row">
<div class="col-12 border">
1
</div>
<div class="col-12 border">
2
</div>
</div>
</div>
<div class="col-6 border">
3
</div>
<div class="row">
<div class="col-6 border">4</div>
<div class="col-6 border">5</div>
</div>
</div>
</div>
</body>
Solution with just classes.
my-auto class to the inner block will place it in the center.
One problem will still be there:
You will have to stretch the outer container using height:100vh; so it's stretched to it's screen size.
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/css/bootstrap.css" rel="stylesheet" />
<div class="d-flex flex-column" style="height:100vh; border:5px solid red;">
<div class="d-flex flex-column my-auto" style="border: solid blue 1px">
<div class="row">
<div class="col-6">
<div class="row">
<div class="col-12 border">
1
</div>
<div class="col-12 border">
2
</div>
</div>
</div>
<div class="col-6 border">
3
</div>
</div>
<div class="row">
<div class="col-6 border">4</div>
<div class="col-6 border">5</div>
</div>
</div>
</div>
This is an easy way to solve it:
<body>
<div class="h-50">
</div>
<div style="border: solid blue 1px" >
<div class="row">
<div class="col-6">
<div class="row">
<div class="col-12 border">
1
</div>
<div class="col-12 border">
2
</div>
</div>
</div>
<div class="col-6 border">
3
</div>
</div>
<div class="row">
<div class="col-6 border">4</div>
<div class="col-6 border">5</div>
</div>
</div>
</body>
I hope it is usefull.
Here is the simplest Bootstrap way using the flexbox classes...
<div class="container-fluid h-100 d-flex flex-column justify-content-center" style="border: solid red 1px">
<div class="row d-flex justify-content-center contenedor_centrado">
<div class="col-6">
<div class="row">
<div class="col-12 border">
1
</div>
<div class="col-12 border">
2
</div>
</div>
</div>
<div class="col-6 border">
3
</div>
<div class="row w-100">
<div class="col-6 border">4</div>
<div class="col-6 border">5</div>
</div>
</div>
</div>
https://www.codeply.com/go/aZnnbWrMRE
Also see:
Vertical Align Center in Bootstrap 4
Bootstrap 4. Center Vertical and Horizontal Alignment

How to align divs with bootstrap

I have trouble aligning my divs with bootstrap. The big div in the middle is pushing down the boxes at the sides. How can I achieve this layout?
The layout of divs I want to achieve:
I've been working with this code, tell me if it's what you need:
HTML:
<div class="row">
<div class="col-md-3">
<div class="row ">
<div class="col-md-12 medium">
</div>
</div>
<div class="row">
<div class="col-md-12 medium">
</div>
</div>
</div>
<div class="col-md-6">
<div class="row ">
<div class="col-md-12 large">
</div>
</div>
</div>
<div class="col-md-3">
<div class="row ">
<div class="col-md-12 small">
</div>
</div>
<div class="row">
<div class="col-md-12 small">
</div>
</div>
<div class="row">
<div class="col-md-12 small">
</div>
</div>
</div>
</div>
CSS:
.small{
height:100px;
border: 1px solid;
}
.medium{
height:200px;
border: 1px solid;
}
.large{
height:400px;
border: 1px solid;
}
Hope it helps!
Based on the image you have provided i have created below sample design please check it. In order to achieve your design i have written some inline css please check it as well
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-lg-3 col-sm-3 col-xs-3" >
<div class="row" style="min-height:200px;border:1px solid;margin-bottom:60px">
This is testing data
</div>
<div class="row" style="min-height:360px;border:1px solid;margin-bottom:10px">
This is testing data
</div>
</div>
<div class="col-lg-5 col-sm-5 col-xs-5" style="min-height:620px;border:1px solid;margin-left:10px;margin-right:10px" >
This is center content
</div>
<div class="col-lg-3 col-sm-3 col-xs-3" >
<div class="row" style="min-height:200px;border:1px solid;margin-bottom:10px">
This is testing data
</div>
<div class="row" style="min-height:200px;border:1px solid;margin-bottom:10px">
This is testing data
</div>
<div class="row" style="min-height:200px;border:1px solid;margin-bottom:10px">
This is testing data
</div>
</div>
</div>
</div>
</body>
</html>
Declare three main divs and place children divs in it. Check below image. In responsive status parent 100% children side by side
https://i.stack.imgur.com/1qTPG.png
.border_black{
border: thin black solid
}
.margin_top_10px{
margin-top: 10px;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 ">
<div class="row border_black">
<p>Left Side Contents 1</p>
</div>
<div class="row border_black margin_top_10px">
<p>Left Side Contents 2</p>
<p>Left Side Contents 2</p>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 center_div text-center">
<div class="row">
<div class="col-md-10 col-md-offset-1 col-sm-10 col-sm-offset-1 col-xs-10 col-xs-offset-1 border_black">
<p>Center Contents</p>
<p>Center Contents</p>
<p>Center Contents</p>
</div>
</div>
</div>
<div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 ">
<div class="row border_black">Right Side Contents 1</div>
<div class="row border_black margin_top_10px">Right Side Contents 2</div>
<div class="row border_black margin_top_10px">Right Side Contents 3</div>
</div>
</div>
</div>
Here is JSFiddle
Hope this helps.

Bootstrap 3 two responsive rows next to each other?

I'm using Bootstrap 3 and trying to solve this what's mocked on the image. I used one row with two cols, and they are next to each other like in the image, but when I resize to mobile I want the right side to fall beneath the left. Any help is appreciated, thank you.
Code sample:
<div className="container">
<div className="row">
<div className="col-xs-4 col-offset-xs-1">
<h1>1</h1>
</div>
<div className="col-xs-4">
<div className="row">
<h1>2</h1>
</div>
</div>
.black{background-color:#000; height:320px;}
.red{background-color:#F00; height:100px;margin-bottom:10px;}
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<div class="container">
<div class="row">
<div class="col-xs-6">
<div class="row">
<div class="col-xs-12 black">
black
</div>
</div>
</div>
<div class="col-xs-6">
<div class="row">
<div class="col-xs-12 red">
one
</div>
</div>
<div class="row">
<div class="col-xs-12 red">
two
</div>
</div>
<div class="row">
<div class="col-xs-12 red">
three
</div>
</div>
</div>
</div>
</div>
Bootstrap allows nesting your elements easily, here you go: https://jsfiddle.net/denea/DTcHh/25594/
<div className="container">
<div class="row">
<div class="col-sm-6 col-xs-12" id="first">
<h1>1</h1>
</div>
<div class="col-sm-6 col-xs-12" id="second">
<div class="row">
<h1>2</h1>
</div>
<div class="row">
<h1>3</h1>
</div>
<div class="row">
<h1>4</h1>
</div>
</div>
</div>
and read this: http://getbootstrap.com/css/#grid-nesting
Bootstrap allows for nesting which will provide what you need:
<div class="row">
<div class="col-sm-6">
Left column
</div>
<div class="col-sm-6">
<div class="row">
<div class="col-sm-12">
Right column, row 1
</div>
</div>
<div class="row">
<div class="col-sm-12">
Right column, row 2
</div>
</div>
<div class="row">
<div class="col-sm-12">
Right column, row 3
</div>
</div>
</div>
</div>
More info here.
You can achieve this by using flexbox and media query
#media (max-width: 360px) {
.container > .row {
display:flex;
flex-flow: row-reverse wrap;
}
}
Demo: https://jsfiddle.net/xs3joev8/1/

Resources