Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vertical-align with Pure CSS (from YUI)

How can I add vertical-align: middle to elements using PureCSS, from YUI?

I have three elements with have the same height, and I want that, "inside", they have vertical-align: middle.

=> Check jsfiddle: http://jsfiddle.net/kn88b/1/

The closest I get was with display: table; width: 100%, but this BUGs the "mobile version", so I want a solution for all versions (tablet/desktop/mobile).

enter image description here

like image 518
Hugo M. Avatar asked Apr 07 '14 06:04

Hugo M.


People also ask

How do I align vertically in CSS?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.

How do you vertically align text in a block in CSS?

You can just set text-align to center for an inline element, and margin: 0 auto would do it for a block-level element.

How to center an element vertically in CSS?

Then we can add the clearfix hack to the containing element to fix this problem: There are many ways to center an element vertically in CSS. A simple solution is to use top and bottom padding: I am vertically centered. To center both vertically and horizontally, use padding and text-align: center: I am vertically and horizontally centered.

What does the vertical-align property do in CSS?

The vertical-align property sets the vertical alignment of an element. yes. Read about animatable Try it The numbers in the table specify the first browser version that fully supports the property. The element is aligned with the baseline of the parent.

How do I center align text on a block level element?

You can just set text-align to center for an inline element, and margin: 0 auto would do it for a block-level element. But issues arise on multiple fronts if you're trying to combine both vertical and horizontal alignments.

How do I Align elements in HTML?

Another method for aligning elements is to use the float property: Note: If an element is taller than the element containing it, and it is floated, it will overflow outside of its container. You can use the "clearfix hack" to fix this (see example below). Then we can add the clearfix hack to the containing element to fix this problem:


2 Answers

Use flexboxes! Here is a JSFiddle that I've tested in Chrome.

http://jsfiddle.net/kn88b/6/

I specified display: flex on .a,.b,.c and specified margin: auto on .inside

To make this cross browser, just make sure you use all the vendor prefixes.

You can learn more about flexboxes here: http://coding.smashingmagazine.com/2013/05/22/centering-elements-with-flexbox/

like image 167
Eli White Avatar answered Oct 04 '22 09:10

Eli White


There you go: http://jsfiddle.net/kn88b/3/ . Used display:table for parent element, and display:table-cell; vertical-align:middle; for children content

like image 27
n1kkou Avatar answered Oct 04 '22 07:10

n1kkou