I'm using Twitter Bootstrap 3, and I have problems when I want to align vertically two div
, for example — JSFiddle link:
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<div class="row">
<div class="col-xs-5">
<div style="height:5em;border:1px solid #000">Big</div>
</div>
<div class="col-xs-5">
<div style="height:3em;border:1px solid #F00">Small</div>
</div>
</div>
The grid system in Bootstrap uses float: left
, not display:inline-block
, so the property vertical-align
doesn't work. I tried using margin-top
to fix it, but I think this is not a good solution for the responsive design.
OK, accidentally I've mixed a few solutions, and it finally works now for my layout where I tried to make a 3x3 table with Bootstrap columns on smallest resolution.
There are several ways you can do it
1.
and the css of the container to
Use padding along with media-screen for changing the padding relative to the screen size. Google @media screen to know more
Use relative padding i.e. specify padding in terms of %
Hope it helps
Flexible box layout
With the advent of the CSS Flexible Box, many of web designers' nightmares1 have been resolved. One of the most hacky ones, the vertical alignment. Now it is possible even in unknown heights.
Flexible Box (or in short, Flexbox), is a new layout system that is specifically designed for layout purposes. The specification states:
How can it help in this case? Well, let's see.
Vertical aligned columns
Using Twitter Bootstrap we have
.row
s having some.col-*
s. All we need to do is to display the desired.row
2 as a flex container box and then align all its flex items (the columns) vertically byalign-items
property.EXAMPLE HERE (Please read the comments with care)
The Output
Colored area displays the padding-box of columns.
Clarifying on
align-items: center
Big Alert
Important note #1: Twitter Bootstrap doesn't specify the
width
of columns in extra small devices unless you give one of.col-xs-#
classes to the columns.Therefore in this particular demo, I have used
.col-xs-*
classes in order for columns to be displayed properly in mobile mode, because it specifies thewidth
of the column explicitly.But alternatively you could switch off the Flexbox layout simply by changing
display: flex;
todisplay: block;
in specific screen sizes. For instance:Or you could specify
.vertical-align
only on specific screen sizes like so:In that case, I'd go with @KevinNelson's approach.
Important note #2: Vendor prefixes omitted due to brevity. Flexbox syntax has been changed during the time. The new written syntax won't work on older versions of web browsers (but not that old as Internet Explorer 9! Flexbox is supported on Internet Explorer 10 and later).
This means you should also use vendor-prefixed properties like
display: -webkit-box
and so on in production mode.If you click on "Toggle Compiled View" in the Demo, you'll see the prefixed version of CSS declarations (thanks to Autoprefixer).
Full-height columns with vertical aligned contents
As you see in the previous demo, columns (the flex items) are no longer as high as their container (the flex container box. i.e. the
.row
element).This is because of using
center
value foralign-items
property. The default value isstretch
so that the items can fill the entire height of the parent element.In order to fix that, you can add
display: flex;
to the columns as well:EXAMPLE HERE (Again, mind the comments)
The Output
Colored area displays the padding-box of columns.
Last, but not least, notice that the demos and code snippets here are meant to give you a different idea, to provide a modern approach to achieve the goal. Please mind the "Big Alert" section if you are going to use this approach in real world websites or applications.
For further reading including browser support, these resources would be useful:
1. Vertically align an image inside a div with responsive height 2. It's better to use an additional class in order not to alter Twitter Bootstrap's default
.row
.I elaborated a bit on zessx's answer, in order to make it easier to use when mixing different column sizes on different screen sizes.
If you use Sass, you can add this to your scss-file:
Which generates the following CSS (that you can use directly in your stylesheets, if you are not using Sass):
Now you can use it on your responsive columns like this:
HTML
CSS
Following the accepted answer, if you do not wish to customize the markup, for separation of concerns or simply because you use a CMS, the following solution works fine:
The limitation here is that you cannot inherit font size from the parent element because the row sets the font size to 0 in order to remove white space.