Five responsive columns in a row with Bootstrap [duplicate] - html

This question already has answers here:
Five equal columns in twitter bootstrap
(47 answers)
Bootstrap - 5 column layout
(20 answers)
Closed 2 years ago.
I want to create a row which has 5 responsive columns which have equal padding from top to bottom and from the sides, but can't find the best solution how to achieve it with bootstrap.
Current html:
<div class="row five-col-row">
<div class="col col-md-2 mx-2">
<h6>Sources</h6>
<span>{{ allStats.sources }}</span>
</div>
<div class="col col-md-2 mx-2">
<h6>Installs</h6>
<span>{{ allStats.installs }}</span>
</div>
<div class="col col-md-2 mx-2">
<h6>Conversions</h6>
<span>{{ allStats.conversions }}</span>
</div>
<div class="col col-md-2 mx-2">
<h6>Amount</h6>
<span>{{ allStats.conversion_amount }}$</span>
</div>
<div class="col col-md-2 mx-2">
<h6>Churn rate</h6>
<span>{{ allStats.churn_rate }}%</span>
</div>
</div>
Is there a way to create a responsive five column grid with Bootstrap?
The result I want to achieve is:
Any help will be appreciated.

Do not use margin-x on .col-* elements.
Set your custom padding-x on .col-*
Set padding-y on row. It should be two times padding-x of .col-*. For instance, if you set a 10px padding-x on columns, the padding-y of row should be 20px becuase there is 20px space between the columns.
Wrap the content of the .col-* elements in a div and apply all the style to this element instead of the column.
div.col div {
border: 1px solid #000;
height: 200px;
}
.py-1rem {
padding-top: 1rem;
padding-bottom: 1rem;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<div class="row py-1rem">
<div class="col col-md-2 px-2">
<div></div>
</div>
<div class="col col-md-2 px-2">
<div></div>
</div>
<div class="col col-md-2 px-2">
<div></div>
</div>
<div class="col col-md-2 px-2">
<div></div>
</div>
<div class="col col-md-2 px-2">
<div></div>
</div>
</div>
</div>

Related

Columns Nested in columns

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>

Boostrap grid aligment [duplicate]

This question already has answers here:
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
Closed 1 year ago.
I have three elements and I want to center two in the middle of the page and the last item completely to the left.
I still don't understand the col system.
Thanks in advance.
html
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<div class="container w-100">
<div class="row">
<div class="col d-flex justify-content-start">
item 1 // align this element to the left
</div>
<div class="col">
item 2 // center this element in the middle of the page
</div>
<div class="col">
item 3 // center this element in the middle of the page
</div>
</div>
</div>
To accomplish what you are looking for we need to create a row that contains 3 columns with the same width and leave the third column empty. Then we use Bootstrap d-flex class to position the elements inside each column exactly where we want.
In this case I'm using justify-content-start to position the flex element at the start (left) of the container.
.m-1{border:1px solid black}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css">
<div class="row w-100">
<div class="col">
<div class="d-flex justify-content-start">
<div class="m-1 p-1"> item one</div>
</div>
</div>
<div class="col">
<div class="d-flex justify-content-center">
<div class="flex-fill m-1 p-1">item two</div>
<div class="flex-fill m-1 p-1">item three</div>
</div>
</div>
<div class="col">
</div>
</div>
You can read more about this here and here

How to align elements vertically center in header with flex [duplicate]

This question already has answers here:
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
Vertical Align Center in Bootstrap 4 [duplicate]
(20 answers)
Closed 3 years ago.
The community reviewed whether to reopen this question 12 months ago and left it closed:
Original close reason(s) were not resolved
In my site header, I have 3 divs. For a logo, for search input and for a cart link. I want to align these 3 .col-md-4 divs vertically centered.
The divs have an align-items-center class, but it's not doing what I want, the divs arent centered vertically.
<div class="header">
<div class="container">
<div class="row">
<div class="col-md-4 d-flex justify-content-center justify-content-md-start align-items-center">
<img src="images/assets/logo.png" alt="" class="img-fluid"</a>
</div>
<div class="col-md-4 d-flex justify-content-center justify-content-md-center align-items-center">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Termékek keresése..." aria-label="Recipient's username" aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button">Keresés</button>
</div>
</div>
</div>
<div class="col-md-4 d-flex justify-content-center justify-content-md-end align-items-center">
<b>Kosár</b>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
I attach a photo of that how is the header looking now. The header has a grey border at its bottom.
align-items: center; is not enough to center vertically because you don't have the height defined. In other words, you have to define the header height as 100% of the page, if you want it to be centered vertically. See the example bellow:
body{
height: 100vh;
}
.header{
height:100%;
}
.container{
height: 100%;
}
.row{
height: 100%;
display:flex;
align-items: center;
}
<div class="header">
<div class="container">
<div class="row">
<div class="col-md-4 d-flex justify-content-center justify-content-md-start align-items-center">
<img src="images/assets/logo.png" alt="" class="img-fluid"</a>
</div>
<div class="col-md-4 d-flex justify-content-center justify-content-md-center align-items-center">
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Termékek keresése..." aria-label="Recipient's username" aria-describedby="basic-addon2">
<div class="input-group-append">
<button class="btn btn-outline-secondary" type="button">Keresés</button>
</div>
</div>
</div>
<div class="col-md-4 d-flex justify-content-center justify-content-md-end align-items-center">
<b>Kosár</b>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
simple add to your flex container:
.row{
align-items: center;
}
Better if you find a more specific class (like add classname center and target .row.center)
your code is correct, the problem is that the row height is based on the tallest child element (logo div for example) so you need to set the row height and the elements will align . Codepen
.header .container .row {
height : 200px;
background-color : grey;
}

What's the best way to vertically align content within Bootstrap 4 grid columns? [duplicate]

This question already has answers here:
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
Closed 3 years ago.
I have the following elements with Bootsrap 4(.3.1) classes:
<footer class="container-fluid">
<div class="row">
<div class="col">
Copyright ©
</div>
<div class="col text-right">
<a class="">Back to top</a>
</div>
</div>
</footer>
The height of <footer> is 100px and I'd like the content fo the cols to sit veritcally in the center of the element.
I've tried adding d-flex align-items-center to the elements (trying container, row and cols in turn) but didn't get any results.
Will I have to scrap the grid classes all together and re-do this with flex classes? Or does anyone know of a way Bootstrap can acheive this out-of-the-box using it's own classes?
In addition to adding d-flex align-items-center to the footer and flex-grow-1 to the row class - see demo below:
footer {
height: 100px;
border: 1px solid;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" />
<footer class="container-fluid d-flex align-items-center">
<div class="row flex-grow-1">
<div class="col">
Copyright ©
</div>
<div class="col text-right">
<a class="">Back to top</a>
</div>
</div>
</footer>
You should add the height to the row. and use align-items-center to vericaly align the flex-items i.e, col
.footer {
height: 100px;
background: #dedede;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet"/>
<footer class=" container-fluid">
<div class="footer row align-items-center justify-content-center">
<div class="col">
Copyright ©
</div>
<div class="col text-right">
<a class="">Back to top</a>
</div>
</div>
</footer>

How to center content in a Bootstrap column?

I am trying to center column's content. Does not look like it works for me. Here is my HTML:
<div class="row">
<div class="col-xs-1 center-block">
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
JSFiddle Demo
Use:
<!-- unsupported by HTML5 -->
<div class="col-xs-1" align="center">
instead of
<div class="col-xs-1 center-block">
You can also use bootstrap 3 css:
<!-- recommended method -->
<div class="col-xs-1 text-center">
Bootstrap 4 now has flex classes that will center the content:
<div class="d-flex justify-content-center">
<div>some content</div>
</div>
Note that by default it will be x-axis unless flex-direction is column
[Updated Apr 2022]: Tested and Included the 5.1 version of Bootstrap.
I know this question is old. And the question did not mention which version of Bootstrap he was using. So I'll assume the answer to this question is resolved.
If any of you (like me) stumbled upon this question and looking for answers using the current bootstrap 5.0 (2020) and 4.5 (2019) framework, then here's the solution.
Bootstrap 4.5, 5.0, and 5.1
Use d-flex justify-content-center on your column div. This will center everything inside that column.
<div class="row">
<div class="col-4 d-flex justify-content-center">
// Image
</div>
</div>
If you want to align the text inside the col just use text-center
<div class="row">
<div class="col-4 text-center">
// text only
</div>
</div>
If you have text and image inside the column, you need to use d-flex justify-content-center and text-center.
<div class="row">
<div class="col-4 d-flex justify-content-center text-center">
// for image and text
</div>
</div>
Bootstrap naming conventions carry styles of their own, col-XS-1 refers to a column being 8.33% of the containing element wide. Your text, would most likely expand far beyond the specified width, and couldn't possible be centered within it. If you wanted it to constrain to the div, you could use something like css word-break.
For centering the content within an element large enough to expand beyond the text, you have two options.
Option 1: HTML Center Tag
<div class="row">
<div class="col-xs-1 center-block">
<center>
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</center>
</div>
</div>
Option 2: CSS Text-Align
<div class="row">
<div class="col-xs-1 center-block" style="text-align:center;">
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
If you wanted everything to constrain to the width of the column
<div class="row">
<div class="col-xs-1 center-block" style="text-align:center;word-break:break-all;">
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
UPDATE - Using Bootstrap's text-center class
<div class="row">
<div class="col-xs-1 center-block text-center">
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
FlexBox Method
<div class="row">
<div class="flexBox" style="
display: flex;
flex-flow: row wrap;
justify-content: center;">
<span>aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
http://jsfiddle.net/usth0kd2/13/
Bootstrap 4/Bootstrap 5, horizontal and vertical align contents using flex box
<div class="page-hero d-flex align-items-center justify-content-center">
<h1>Some text </h1>
</div>
Use bootstrap class align-items-center and justify-content-center
.page-hero {
height: 200px;
background-color: grey;
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<div class="page-hero d-flex align-items-center justify-content-center">
<h1>Some text </h1>
</div>
No need to complicate things. With Bootstrap 4, you can simply align items horizontally inside a column using the margin auto class my-auto
<div class="col-md-6 my-auto">
<h3>Lorem ipsum.</h3>
</div>
Use text-center instead of center-block.
Or use center-block on the span element (I did the column wider so you can see the alignment better):
<div class="row">
<div class="col-xs-10" style="background-color:#123;">
<span class="center-block" style="width:100px; background-color:#ccc;">abc</span>
</div>
</div>
This is a simple way.
<div class="row">
<div class="col-md-6 mx-auto">
<p>My text</p>
</div>
</div>
The number 6 controls the width of the column.
//add this to your css
.myClass{
margin 0 auto;
}
// add the class to the span tag( could add it to the div and not using a span
// at all
<div class="row">
<div class="col-xs-1 center-block">
<span class="myClass">aaaaaaaaaaaaaaaaaaaaaaaaaaa</span>
</div>
</div>
col-lg-4 col-md-6 col-sm-8 col-11 mx-auto
1. col-lg-4 ≥ 1200px (popular 1366, 1600, 1920+)
2. col-md-6 ≥ 970px (popular 1024, 1200)
3. col-sm-8 ≥ 768px (popular 800, 768)
4. col-11 set default smaller devices for gutter (popular 600,480,414,375,360,312)
5. mx-auto = always block center
You can add float:none; margin:auto; styling to centerize column content.
If none of the above work (like in my case trying to center an input), I used Boostrap 4 offset:
<div class="row">
<div class="col-6 offset-3">
<input class="form-control" id="myInput" type="text" placeholder="Search..">
</div>
</div>