I'm trying to make three new columns inside the my center column. But when I nest, they do go inside the centered column, but they also stack at the bottom of it. I'm interested in the center column can work as a background kind of like a container for the other three columns.
Maybe this is not how to use Bootstrap and columns? Should I instead just created some three divs that are not columns, but regular divs inside the column?
I'm trying to design a website where it has tree major columns, in which inside the center columns there should be three evenly spaced boxes/images/blocks.
I'm using Bootstrap version: 4.6.1
<link href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.1/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row">
<div class="bg-secondary col-sm-2">
1
</div>
<div class="bg-primary col-sm-8">
2
<div class="row">
<div class="bg-warning col-sm-4">
2.1
</div>
<div class="bg-warning col-sm-4">
2.2
</div>
<div class="bg-warning col-sm-4">
2.3
</div>
</div>
</div>
<div class="bg-secondary col-sm-2">
3
</div>
</div>
</div>
I'm trying to get my column (bg-primary col-sm-8) in the center to get three more columns inside it:
For what you want, you need to use flex utility classes
.container-fluid {
height: 100vh
}
.box {
border: 5px solid red;
padding: 30px
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.1/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">
<div class="container-fluid">
<div class="row h-100">
<div class="bg-secondary col-sm-2">1</div>
<div class="bg-primary col-sm-8">
<div class="row justify-content-around align-items-center h-100">
<div class="box">2.1</div>
<div class="box">2.2</div>
<div class="box">2.3</div>
</div>
</div>
<div class="bg-secondary col-sm-2">3</div>
</div>
</div>
Related
I'm using Bootstrap 5 and I'm trying to get the first row to be at the top of the page and the second row to occupy the rest of the viewport and have its content vertically centered without scrollbars (assuming the viewport is high enough). Here's the basic code
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<div class="container min-vh-100">
<div class="row">
<div class="col-12 col-md-2">
Content
</div>
<div class="col-12 col-md-10">
Content
</div>
</div>
<div class="row min-vh-100 align-items-center">
<div class="col-12 col-lg-6">
Content
</div>
<div class="col-12 col-lg-6">
Content
</div>
</div>
</div>
The second row does correctly center its content, however, because I'm using the min-vh-100 class it produces scroll bars (removing the first row makes the scroll bars go away as expected). Changing the second row's min-vh-100 to h-100 class doesn't help (actually makes things worse). The problem seems to be how to tell the second row to occupy 100% of the parent's remaining height and not 100% of the viewport height.
You need the d-flex and flex-column classes on your container. Then, rather than forcing height on the second row, use flex-fill to get it to use the remaining space. Fixed sizing should be a last resort.
See https://getbootstrap.com/docs/5.0/utilities/flex.
.row:first-child {
background: thistle;
}
.row:last-child {
background: moccasin;
}
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/css/bootstrap.min.css" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<div class="container min-vh-100 d-flex flex-column">
<div class="row">
<div class="col-2">
Content
</div>
<div class="col-10">
Content
</div>
</div>
<div class="row flex-fill align-items-center">
<div class="col-6">
Content
</div>
<div class="col-6">
Content
</div>
</div>
</div>
In bootstrap 4 I have the following code:
<div class="container-fluid">
<div class="row no-gutter">
<div class="d-none d-md-flex col-md-12 flex-wrap">
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
<div style="width:100px;height:50px;float:left;display:block;background:red;">Div</div>
</div>
</div>
</div>
Issue I have is as soon as there is a 'wrapped item', it appears to get stuck to the bottom of the items above. When I try to apply a margin-bottom:5px to each of the divs, it appears to have no effect. What is the correct way to handle this?
A few things in your code...
why use float with flex? Bootstrap 4 uses flex instead of float (which was used by bootstrap 3)
the divs in pink are from your code with classes removed to show the effect in the browser/snippet
the divs in blue show the solution with flex (using bootstrap 4 classes)
margin-bottom works in both
working snippet below:
.flex-wrap>div {
width: 100px;
height: 50px;
display: block;
}
.my-divs {
float: left;
margin-bottom: 10px;
background: lightpink;
}
.my-flex-div {
background: lightblue;
margin-bottom: 5px;
}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
<div class="container-fluid">
<div class="row no-gutter">
<div class=" d-md-flex col-md-12 flex-wrap">
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
<div class="my-divs">Div</div>
</div>
<hr/>
<div class="d-flex flex-wrap">
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
<div class="my-flex-div">Div 2</div>
</div>
</div>
</div>
It is most likely your use of float that is causing the problem. However this whole layout can be achieved using only Bootstrap default classes and no additional CSS.
First, there is no need for a row/col layout if you are only using a col-12 full-width single column. Simply remove the row no-gutter and col-12 classes.
Second, BS4 includes utility classes for margin. Read about them here.
Try the example below. I have used multiple background colors for clarity.
.flex-wrap div {
width: 100px;
height: 50px;
}
<div class="container-fluid">
<div class="d-flex flex-wrap">
<div class="mb-5 bg-primary">Div</div>
<div class="mb-5 bg-secondary">Div</div>
<div class="mb-5 bg-warning">Div</div>
<div class="mb-5 bg-success">Div</div>
<div class="mb-5 bg-danger">Div</div>
</div>
</div>
Here is a working codepen example.
I have two rows which contain 3 column each , I would like at certain breakpoint to switch from 3 columns to 2 column and from 2 columns to 1 column (mobile breakpoint). so far this is what I have.
#media (min-width:768px) {
.col-md-4{
min-width: 50%;
}
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css">
<div class="row">
<div class="col-md-4">1</div>
<div class="col-md-4">2</div>
<div class="col-md-4">3</div>
</div>
<div class="row" >
<div class="col-md-4">4</div>
<div class="col-md-4">5</div>
<div class="col-md-4">6</div>
</div>
Question
This does not work , what am I missing in my code? help am newbie though
I would place them all in the same .row and use the appropriate grid classes. This should get you what you want:
<div class="row">
<div class="col-sm-6 col-md-4">1</div>
<div class="col-sm-6 col-md-4">2</div>
<div class="col-sm-6 col-md-4">3</div>
<div class="col-sm-6 col-md-4">4</div>
<div class="col-sm-6 col-md-4">5</div>
<div class="col-sm-6 col-md-4">6</div>
</div>
Note: I would not redefine .col-md-4 or any of the Bootstrap built-in grid glasses like that.
For more details, read the Bootstrap documentation on using grid glasses: https://getbootstrap.com/docs/4.0/layout/grid/#mix-and-match
Just use the different breakpoint column classes of bootstrap.
If you want to change order in bootstrap 3 (as requested in comments) you can easily use push and pull.
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row">
<div class="col-md-4 col-md-push-4 col-sm-6 col-sm-push-6">1</div>
<div class="col-md-4 col-md-pull-4 col-sm-6 col-sm-pull-6">2</div>
<div class="col-md-4 col-sm-6">3</div>
</div>
https://getbootstrap.com/docs/4.0/layout/grid/#mix-and-match
Use col-sm-6 to set 2 divs in 1 row on sm size...
(the calc is: 12/6=2 => 100% /2=50% means each div gets 50% to his width)
By default in xs size's screen will set 1 div in 1 row because you did not specify otherwise...
<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.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
<div class="row">
<div class="col-sm-6 col-md-4">1</div>
<div class="col-sm-6 col-md-4">2</div>
<div class="col-sm-6 col-md-4">3</div>
</div>
<div class="row" >
<div class="col-sm-6 col-md-4">4</div>
<div class="col-sm-6 col-md-4">5</div>
<div class="col-sm-6 col-md-4">6</div>
</div>
</div>
I have a problem, all I want to do is at a certain width (using the #media) I want to change my left div to the right side under the one that is already there. I tried everything, including changing the display, positioning using left margin but due to the parent container it doesn't work. The only thing that I thought about is to make the same left div on the right side and to make the display to none, but I am pretty sure that's not the way to deal with it. I use Bootstrap 4.
This is the HTML:
.main{
background-color: #aaa;
box-shadow: 3px 3px 20px #000000;
border-radius: 20px;
margin-left: 20px;
margin-right: 20px
}
.left{
background-color: #aaa;
max-width: 200px;
height: 1%;
border-radius: 20px;
}
.right{
background-color: #aaa;
max-width: 200px;
height: 1%;
border-radius: 20px;
}
.right-content{
width: 100%;
text-align: center;
float: center;
margin-top: 10px;
}
.parent-container {
display: flex;
}
<div class="parent-container">
<div class="container left">
<div class="row">
Left content
</div>
</div>
<div class="container card main py-3">
<div class="container card">
<div class="card-body">
<div class="row">
<div class="col-lg-12 text-center">
<h2>TITLE2</h2>
</div>
</div>
<div class="row">
<div class="col-md-12 text-center">
<a href="#" class="link1">
<img src="2.jpg">
<h3>Title3</h3>
</a>
</div>
</div>
</div>
</div>
<div class="container right">
<div class="row">
<div class="right-content">
Right content here
</div>
</div>
</div>
</div>
To achieve the effect you want, you need to use Bootstrap 4 order-* classes. order-last in the code snippet below re-orders the column to come last by default but the order-md-first makes the column appear first on screens that are medium (md) or larger.
There are other ways of using the order classes to achieve the same result. This is just one of the possible options.
But there are many other and fundamental mistakes in your code. Here are a few pointers:
Do NOT nest containers inside other containers. Nest row-column pairs inside columns instead
Do NOT put any content directly into a .row. Bootstrap rows aren't designed for that. Put one or more columns (.col-*) into a row and put all your content inside that column(s).
Bootstrap 4 is flexbox-based by default and has classes to do almost everything you'll ever need in terms of positioning and spacing. Use those native Bootstrap classes instead of unnecessary custom css hacks.
Here's a working code snippet (click the "run code snippet" button below and expand to full page):
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<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://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="container-fluid">
<div class="row">
<div class="col-12 col-md-3 col-xl-2 bg-light order-last order-md-first py-3">
Left div content <br>
Left div content <br>
Left div content <br>
</div>
<div class="col-12 col-md-9 col-xl-10 bg-primary py-3">
<div class="card py-3">
<div class="card-body">
<div class="row">
<div class="col text-center">
<h2>Right div TITLE</h2>
</div>
</div>
<div class="row">
<div class="col text-center">
<a href="#">
<img class="img-fluid" src="https://placeimg.com/888/88/tech">
<h3>Right div subtitle</h3>
</a>
</div>
</div>
</div>
<div class="row">
<div class="col text-center">
Right div content here
</div>
</div>
</div>
</div>
</div>
</div>
Notice: That code doesn't contain any of your custom css. If you want to add some custom css to that, start adding it line by line to see where your custom css breaks Bootstrap.
Also, the background classes bg-light and bg-primary in the code snippet above are just for making things easier to see.
I'm using Bootstrap 4 for the first time. I want to have a website with blank columns on left and right in large view but make them disappear in small view.
I use this HTML structure to create the 3 columns: blank ones and the grey one we can see in the middle:
<div class="container-fluid">
<div class="row">
<div class="col"></div>
<div class="col-sm-12 col-md-8"></div>
<div class="col"></div>
</div>
</div>
But because of the min-height: 1px; which is applied to all the .col-* by Bootstrap, when the blank columns should be hidden, they create a little space, breaking what I want to do.
How can I do what I want? Is the way I'm using Bootstrap 4 the good?
To center a column (.col) in a row (.row) you can add the class justify-content-center (see flex utilities) to the row:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col col-sm-12 col-md-8">Hello World</div>
</div>
</div>
So there is no need for empty columns to center a column.
Another solution would be to use the d-* classes (see utilities) to hide the columns:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid">
<div class="row">
<div class="col d-none d-md-block">1</div>
<div class="col-sm-12 col-md-8">2</div>
<div class="col d-none d-md-block">3</div>
</div>
</div>