Currently my "flex" items look like this (vertically aligned: top)...
_____________________________________
1
_____________________________________
2
_____________________________________
3
_____________________________________
4
_____________________________________
My Goal is to make them look like this (vertically aligned: middle)...
_____________________________________
1
_____________________________________
2
_____________________________________
3
_____________________________________
4
_____________________________________
My requirements:
- The flex container must stay at 100% in height
- The flex items must stay 25% in height (equaling 100%, which is its default anyway).
- The flex items must be vertically centered.
- This must be responsive and be smart enough to stay in the middle per device (so no line-height/padding)
http://jsfiddle.net/oneeezy/p4XtA/
HTML
<!-- Flex Box -->
<section>
<!-- Flex Items -->
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
</section>
CSS
/* Flex Box */
section { padding: 1em; background: black; display: flex; flex-flow: column; height: 100%; justify-content: space-around; }
/* Flex Items */
div { border-top: 1px solid #ccc; background: #f2f2f2; height: 25%; }
div:first-child { border-top: 1px solid transparent; }
div:hover { background: white; }
To vertical align content of flexbox child, you will need to apply some other techniques.
I believe there will be content there , wrapped in tag, You can then again use
flex
and set child inmargin:auto;
DEMOCSS updated :
HTML structure :
Maybe a fallback with
display:table
, can be usefull : DEMO 2Perhaps I'm misunderstanding, but can't you just do:
HTML (Slim)
CSS
Here's a Codepen
Your problem is not really related to the so-called flex-box, in fact what you want to align is the content of the div (not the
div
), so just use some trick to align it normarlly. Here is one of the tricks:Demo.
This is the Flex-way of solving your problem:
HTML
CSS