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

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;
}

Related

Five responsive columns in a row with Bootstrap [duplicate]

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>

html flexbox 2 elements vertical and one on the left [duplicate]

This question already has answers here:
Left column and stacked right column using flexbox CSS [duplicate]
(2 answers)
Closed 2 years ago.
I am totally lost with flexbox for css3, and I cannot seem to figure out how to get it correctly working :(.
I want a layout like:
|-----| |----|
------- |----|
|-----| |----|
(Where |----| on the left are 2 divs an on the right its one)
And on mobile I want the right div to below the other 2 divs...
And I cannot seem to figure it out.
It just will not line up correctly, or the 2 divs on the left will not be as large as the one on the right.
I am using bootstrap 4.0 for this... so maybe that is something?
The following code I am currently at:
There is a container div around it
<div id="container">
<div class="d-flex row">
<div class="flex-row flex-fill">
<div class="d-flex flex-column">
<div class="card d-flex flex-fill">
<div class="card-body">
<form>
<div class="form-group">
<label>xxx</label>
<input type="text" class="form-control date" id="birthdatepicker" data-toggle="daterangepicker" data-single-date-picker="true">
</div>
<button type="submit" class="btn btn-primary waves-effect waves-light">{{ __('Submit') }}</button>
</form>
</div>
</div>
<div class="card d-flex flex-fill mb-4 mb-md-0">
<div class="card-body">
<h4 class="card-title">{{ __('Progress') }}</h4>
<div class="progress mb-2">
<div class="progress-bar bg-success" role="progressbar" style="width: 25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>
</div>
</div>
</div>
<div class="flex-row col-12 col-md-4 col-lg-2">
<div class="d-flex flex-fill card mb-4 mb-md-0">
<div class="card-body">
<h5>xx</h5>
<p>
xx
</p>
</div>
</div>
</div>
</div>
</div>
here is a simple example of what i think you wish to achive.
.flexbox{
display: flex;
flex-direction: row;
width: 400px;
height: 400px;
}
#media only screen and (max-width: 478px){
.flexbox{
flex-direction: column;
}
}
.left{
width: 50%;
height: 100%;
}
.top-left{
height: 50%;
width: 100%;
position: relative;
background: red;
}
.bottom-left{
height: 50%;
width: 100%;
position: relative;
background: green;
}
.right{
width:50%;
height:100%;
}
.center-right{
width: 100%;
height: 100%;
background: blue;
}
<div class="flexbox">
<div class="left">
<div class="top-left">
</div>
<div class="bottom-left">
</div>
</div>
<div class="right">
<div class="center-right">
</div>
</div>
</div>

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>

Flexbox in Bootstrap 4 acting weird

I'm trying to use flexbox to align vertically a section.
So this is what I'm trying to align vertically:
HTML:
<!--Top Hero Section -->
<div class="d-flex hm-hero justify-content-center">
<div class="container">
<div class="row align-items-end">
<div class="col-12">
<h1 class="text-center">Fix Your Problem Today!</h1>
<p class="text-center">Start searching for a contractor or company around your area and get your problem fixed in a matter of hours!</p>
</div>
<!--Form Section -->
<div class="col-12">
<div class="form-group row justify-content-center">
<div class="col-12 col-md-6">
<input class="form-control" type="text" value="e.g. plumbing, carpentry" id="example-text-input">
</div>
<div class="col-12 col-md-4">
<input class="form-control" type="text" value="e.g. city name, postcode" id="example-text-input">
</div>
<div class="col-12 col-md-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</div>
</div>
</div>
Regarding CSS, I've only applied this:
.hm-hero {
background: url("../img/main_pic.png") no-repeat;
background-size: fill;
min-height: 600px;
}
I can't figure out why flexbox is not working. I've tried two classes, align-content/items-center in whatever div possible and still can't get it to work.
So since I have that full width div with the background image, inside it is a container div, which I'm trying to get it to stay vertically. I really want to avoid putting a 200px margin top to this. Is this is a Bootstrap 4 bug?
I added the align-items-center class to the flex div parent and it worked. As long as you have a height on the .hm-hero the content will be centered. Hope this helps. JSFiddle
.hm-hero {
background: url("https://placehold.it/960x600") no-repeat;
background-size: fill;
min-height: 600px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet"/>
<!--Top Hero Section -->
<div class="d-flex hm-hero justify-content-center align-items-center">
<div class="container">
<div class="row align-items-end">
<div class="col-12">
<h1 class="text-center">Fix Your Problem Today!</h1>
<p class="text-center">Start searching for a contractor or company around your area and get your problem fixed in a matter of hours!</p>
</div>
<!--Form Section -->
<div class="col-12">
<div class="form-group row justify-content-center">
<div class="col-12 col-md-6">
<input class="form-control" type="text" value="e.g. plumbing, carpentry" id="example-text-input">
</div>
<div class="col-12 col-md-4">
<input class="form-control" type="text" value="e.g. city name, postcode" id="example-text-input">
</div>
<div class="col-12 col-md-2">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</div>
</div>
</div>
</div>

Vertically middle align button in bootstrap column

I want to vertically middle align the buttons in bootstrap columns.
Fiddler: https://jsfiddle.net/ebwwvy6m/23/
HTML
<div class="row">
<div class="col-sm-3 vcenter">
<button type="button" class="btn btn-success btn-sm" id="BtnExport">Export</button>
</div>
<div class="col-sm-9">
<div class="row">
<div class="col-sm-6">
Comment
<textarea rows="2" class="form-control" id="TextAreaComment"></textarea>
</div>
<div class="col-sm-6 vcenter">
<button type="button" class="btn btn-primary btn-sm" id="BtnEntry">Abandon</button>
</div>
</div>
</div>
</div>
CSS:
.vcenter {
display:inline-block !important;
vertical-align:middle !important;
}
Expectation:
What I tried?
I tried following the solutions provided here,
Vertical align button in the middle of the column- bootstrap
Vertical align button middle next to text
Bootstrap - Vertically Align Button With Well
But, I am not getting the solution. Any suggestion will be much appreciated. Thanks.
.align-self-center will work.
<div class="col-sm-6 align-self-center">
</div>
You could use flexbox, just use the snippet on parent of the button in which you want to center the item.
(in your case .row div)
.row {
display: flex;
align-items: center;
}
Here is an example: https://jsfiddle.net/xsmnnLoc/
But this method doesn't work on IE 10 and lower