same height Bootstrap columns with vertical align - css

I am using Bootstrap 3, I need to have same height of the columns with vertical align able in the content of the columns
<div class="row">
<div class="col-xs-12 col-sm-3">
vertical align top
</div>
<div class="col-xs-12 col-sm-3">
vertical align middle
</div>
<div class="col-xs-6 col-sm-3">
<img src="http://mspmentor.net/site-files/mspmentor.net/files/archive/mspmentor.net/wp-content/uploads/2009/10/free_managed_services.jpg" />
</div>
<div class="col-xs-6 col-sm-3">
vertical align bottom
</div>
</div>
Please check here https://jsfiddle.net/4d2m74nc/4/

This is achievable but will take away from some of bootstrap's built in functionality. Mainly, the media queries for mobile rendering of the grid system.
The best way to achieve this is probably with the flexbox approach. Demo.
<div class="row row-eq-height">
<div class="col-sm-3">
vertical align top
</div>
<div class="col-sm-3 vcenter">
vertical align middle
</div>
<div class="col-sm-3">
<img src="http://mspmentor.net/site-files/mspmentor.net/files/archive/mspmentor.net/wp-content/uploads/2009/10/free_managed_services.jpg" />
</div>
<div class="col-sm-3 vbottom">
vertical align bottom
</div>
</div>
CSS
.row-eq-height {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.vcenter{
display:flex;
flex-direction:column;
justify-content:center;
}
.vbottom{
display:flex;
flex-direction:column;
justify-content:flex-end;
}
Second way is to use absolute positioning for the vertical alignment and flexbox for matching heights.
Check out the demo.
<div class="row row-eq-height">
<div class="col-sm-3">
vertical align top
</div>
<div class="col-sm-3">
<div class="vcenter">
vertical align middle
</div>
</div>
<div class="col-sm-3">
<img src="http://mspmentor.net/site-files/mspmentor.net/files/archive/mspmentor.net/wp-content/uploads/2009/10/free_managed_services.jpg" />
</div>
<div class="col-sm-3">
vertical align bottom
</div>
</div>
CSS
.row-eq-height {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.vcenter {
position: absolute;
top: 50%;
}
Another option would be to use JavaScript to fix the heights and vertical align for you. You could use matchHeight.js.
It's fairly simple to use. You just wrap all of your column content that you want to match in height with another <div> with the class .box
<div class="row">
<div class="col-md-4">
<div class="box">
Content
</div>
</div>
<div class="col-md-4">
<div class="box">
Content
</div>
</div>
<div class="col-md-4">
<div class="box">
Content
</div>
</div>
</div>
and then instantiate it on document load in your JavaScript file or <script></script> tags after your JQuery link.
$('.box').matchHeight();
Sticking with JavaScript for the vertical align you could add .vcenter to the .box element you want vertically aligned and do something like:
var height = $('.vcenter').height();
$('.vcenter').css('margin-top', (height/2));
$('.vcenter').css('margin-bottom', -(height/2));
var $window = $(window);
var windowsize = $window.width();
function checkWidth(){
if (windowsize < 440) {
$('.vcenter').removeAttr("style");
}
}
$(window).resize(checkWidth);
Which calculates the height of the .box and then divides by 2 to determine a top and bottom margin for the content. We then determine the width of the window and on resize we remove the margins. This part is untested but I believe will work in theory. Here's a demo of matchHeight.js. And here's a useful blog on column height matching in bootstrap.
The last two approaches to vertical alignment are poor but should work.

<div class="row flex-container">
<div class="col-xs-12 flex-item col-sm-3">
vertical align top
</div>
<div class="col-xs-12 flex-item col-sm-3">
vertical align middle
</div>
<img class="col-xs-6 flex-item col-sm-3" src="http://mspmentor.net/site-files/mspmentor.net/files/archive/mspmentor.net/wp-content/uploads/2009/10/free_managed_services.jpg" />
<div class="col-xs-6 flex-item col-sm-3">
vertical align bottom
</div>
</div>
CSS that support the boxes the flex item properties it just give for your understanding
.flex-container {
display: -webkit-flex;
display: flex;
}
.flex-item {
border: 1px solid #000;
padding: 20px;
}

Related

Align content center don't work on my columns [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Bootstrap Center Vertical and Horizontal Alignment
(17 answers)
Closed 2 years ago.
I have this layout and css
<div class="container-fluid header">
<div class="row text-center">
<div class="col-md-6 red">
<h1>First</h1>
</div>
<div class="col-md-6 red">
<h1>Second</h1>
</div>
</div>
</div>
css
.header {
background-color: blue;
}
.col-md-6 {
min-height: 300px;
}
.red {
border:2px solid red;
}
i gave min-height on col 6 because i want to have extra space there.I center the text of the content of each column with text-center on the row. But i can't find a way to center the content of the columns vertically.
I tried for example
<div class="container-fluid header">
<div class="row text-center d-flex align-content-center">
<div class="col-md-6 red align-self-center">
<h1>First</h1>
</div>
<div class="col-md-6 red align-self-center">
<h1>Second</h1>
</div>
</div>
</div>
but without success
Bootstrap Utility Classes
You can center the contents of the column by adding the bootstrap utility classes d-flex, align-items-center and justify-content-center.
Raw CSS
These utility classes are just inline shorthand for:
some-element {
display: flex;
align-items: center;
justify-content: center;
}
Flex Explaination
When a flex container has direction row (default) then align-items handles the vertical alignment and justify-content handles the horizontal alignment.
I found this css-tricks post on flexbox super useful when I was still getting my head around flex layouts.
Example
.header {
background-color: blue;
}
.col-md-6 {
height: 300px;
}
.red {
border:2px solid red;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container-fluid header">
<div class="row">
<div class="col-md-6 red d-flex align-items-center justify-content-center">
<h1>First</h1>
</div>
<div class="col-md-6 red">
<h1>Second</h1>
</div>
</div>
</div>
You need to be applying 'd-flex' to the div class="col".
By default, row is already a flexbox, but cols are NOT. It wasn't aligning the content because it isn't a flexbox until you give it the d-flex class.

How to change the size of mat-card in angular material

I want both Components the same height (the size of left form) tried margin , padding nothing works
below is the code for its parent element HTML
<mat-tab label="Update Profile">
<div class="container">
<mat-card class="mat-card-container">
<div class="vertical-center row" >
<div class="col-lg-1">
</div>
<div class="col-lg-6">
<app-register [user]="user"></app-register>
</div>
<div class="col-lg-4 password-change-form">
<app-password-change [id]="user.id"></app-password-change>
</div>
<div class="col-lg-1">
</div>
</div>
</mat-card>
</div>
</mat-tab>
CSS
.vertical-center {
margin-top: 2rem;
margin-bottom: 1rem;
display: flex;
align-items: center;
}
.password-change-form{
padding: inherit;
}
.spacer{}
Better you can use flex layout. Refer: [https://github.com/angular/flex-layout/wiki/fxLayoutAlign-API][1]
Set fxFlexAlign="stretch" to have all mat-card in same height.
The problem is not with the mat card, but with the div with password-change-form if you were asking to make them the same height:
.password-change-form{
height:100%
}
but if you want them to have the same width, you should probably add col-lg-5 to both of them instead of 6 and 4
<div class="col-lg-5">
<app-register [user]="user"></app-register>
</div>
<div class="col-lg-5 password-change-form">
<app-password-change [id]="user.id"></app-password-change>
</div>

Center elements vertically in Bulma

I just started setting up my blog prototype with Bulma. There is a footer section with two equally divided columns.
I'd like the three items (Twitter, Email, etc.) to be vertically centered in the yellow area. Is there any special class for that available in Bulma?
(Please see the full example on codepen.io.)
<footer class="footer" style="background-color: lightpink;">
<div class="columns">
<div class="column has-text-centered-touch" style="background-color: cyan;">
<p>Some copyright stuff...</p>
<p>Templated with Bulma. Published with Hugo.</p>
</div>
<div class="column has-text-right" style="background-color: yellow;">
<div class="level">
<div class="level-left"></div>
<div class="level-right">
<a class="level-item" href="#">Twitter Icon</a>
<a class="level-item" href="#">Email Icon</a>
<a class="level-item" href="#">LinkedIn Icon</a>
</div>
</div>
</div>
</div>
</footer>
You could add the following CSS so the right side column so it is vertically centered.
https://codepen.io/anon/pen/XzmEgr
.footer .has-text-right {
display: flex;
justify-content: center;
}
Make the height of the .level on the right side 100%
.right-side > .level {
height: 100%;
}
JSFiddle

Bootstrap equal height boxes with nested colored divs

I have a problem with equal height cols/boxes in bootstrap in combination with nested colored divs. Have a look here how it looks like: https://www.dropbox.com/s/ko6zmqwup4wgasb/bootstrap-equal-height-cols.jpg?dl=0
Actually to set cols to equal heights isn't that problem with flex boxes anymore. The difficulties I encountered is the set it up for the proper look. The bootstrap cols can't have the colored background due to their setup with margins and paddings. Because I have 2 divs within the row wrapper it seems to be difficult to fix this problem. The reduced HTML code:
<div class="row row-eq-height">
<div class="col-sm-6 col-md-4">
<div class="portfolio-el bg-dark-gray">
<div class="view">
<img src="images/image_360x180_1.jpg" alt="dummy">
</div>
<div class="portfolio-text-content">
here goes text which causes different heights of those boxes...
</div>
</div>
</div>
<div class="col-sm-6 col-md-4">
<div class="portfolio-el bg-dark-gray">
<div class="view">
<img src="images/image_360x180_2.jpg" alt="dummy">
</div>
<div class="portfolio-text-content">
here goes text which causes different heights of those boxes...
</div>
</div>
<div class="col-sm-6 col-md-4">
<div class="portfolio-el bg-dark-gray">
<div class="view">
<img src="images/image_360x180_3.jpg" alt="dummy">
</div>
<div class="portfolio-text-content">
here goes text which causes different heights of those boxes...
</div>
</div>
</div><!-- end row -->
CSS:
.row-eq-height {
display: -webkit-box;
display: -webkit-flex;
display: -ms-flexbox;
display: flex;
}
.bg-dark-gray {
background: #efefef;
}
all the rest is done by bootstrap classes > grid, etc.
I have seen examples giving the cols flex: 1, but I need a height of 100% for the divs with the gray background. Any ideas or workarounds? Cheers

Bootstrap grid with even vertical and horizontal spaces

When using the bootstrap grid, the vertical spaces and the most left & right are 15px but the spaces between the columns are double: 30px.
Is there a way to make them also 15px without changing Bootstrap CSS?
It's very easy to change the gutter without affecting the core bootstrap.css and still be able to use the same classes to push pull and offset.
Just make sure your .row around the new gutters has negative left and right margin equal to the padding on the left and right of the columns. Just like all floated elements this grid, like the Bootstrap grid, is exactly the same and will still require no more than 12 columns per row, if exceeded all heights need to be equal or you will need to clear them or use some other means such as jQuery or making them all the same height.
There is no vertical spacing on the grid, any vertical spacing comes from the children inside the column and it's usually the bottom margin value.
https://jsbin.com/wonuni/1/
CSS
.row.grid-15-gutter {
margin-left: -7.5px;
margin-right: -7.5px;
}
.row.grid-15-gutter [class*="col-"] {
padding-left: 7.5px;
padding-right: 7.5px;
}
.panel {
border: 1px solid red;
padding: 10px;
margin-bottom: 15px;
}
HTML
<div class="container">
<h2>Modified Grid</h2>
<div class="row grid-15-gutter">
<div class="col-sm-5">
<div class="panel">1</div>
</div>
<div class="col-sm-7">
<div class="panel">2</div>
</div>
</div>
<div class="row grid-15-gutter">
<div class="col-sm-5">
<div class="panel">1</div>
</div>
<div class="col-sm-7">
<div class="panel">3</div>
</div>
</div>
<hr>
<h2>Regular Grid</h2>
<div class="row">
<div class="col-sm-5">
<div class="panel">1</div>
</div>
<div class="col-sm-7">
<div class="panel">2</div>
</div>
</div>
<div class="row">
<div class="col-sm-5">
<div class="panel">1</div>
</div>
<div class="col-sm-7">
<div class="panel">3</div>
</div>
</div>
</div>

Resources