flexbox vertical align child top, center another
No, not really, not without another wrapper which is a flex-container.
As flexbox is, to a certain extent based on manipulting margins, there is no method (AFAIK, although I'd be interested to find out if there is) to justify-content: center
and then align-self
a child element to somewhere else other than center.
I'd go with something like this: Add a wrapper to the "content" div, give it flex:1
to fill the remaining space below the header, then make that wrapper display:flex
with justify-content:center
.
This seems to be the most logical method
.col { height: 150px; width: 80%; margin: 1em auto; border: 1px solid grey; display: flex; flex-direction: column;}.header { background: lightblue;}.content { background: orange;}.flexy { flex: 1; display: flex; flex-direction: column; justify-content: center; background: plum;}
<div class="col"> <div class="header">Header #2</div> <div class="flexy"> <div class="content">Lorem Ipsum <br />Dolor</div> </div></div>
align flex child at top-center and bottom-center, flexbox
What you are looking for is justify-content: space-between;
to align the items until the corners.
Add that to the .container
and there you go.
.container{
/*justify-content: center;*/
justify-content: space-between;
}
On .item-4
you have align-self: flex-start;
but you don't need that. just remove it.
https://jsfiddle.net/q5cw4xvy/2/
To better help you understand flexbox, there is a really nice css-tricks article.
Flexbox vertical align specific content within a wrapper?
This is a sort of a hack, but it works:
Remove
align-items: center
from thewrapper
andflex:1
to the flex childrenleft
andright
Make the inner
left
andright
container acolumn flexbox
and center theh3
andcontent
usingjustify-content:center
Use
margin-bottom:auto
to push bothh3
to the top and allowcontent
to stay at the middle.
See demo below:
.wrapper { overflow: auto; border: 1px solid #ccc; text-align: center; display: -ms-flexbox; display: -webkit-flex; display: flex;}.left,.right { padding: 5px; display: flex; flex-direction: column; justify-content: center; flex: 1;}.left h3, .right h3 { margin-bottom: auto;}.content { margin-bottom: auto;}
<div class="wrapper">
<div class="left"> <h3> Title (should be aligned to top) </h3> <div class="content"> left <br>left <br>left <br>left <br> </div> </div>
<div class="right"> <h3> Title (should be aligned to top) </h3> <div class="content"> right </div> </div>
</div>
How to vertically center second child in column of divs
There are several methods, and it depends whether you want to make it work on Internet Explorer 11.
Position absolute: https://jsfiddle.net/trcmba5z/
Let them be two flex items, top and bottom, with the first one about 0.8 unit tall and the second one 1.2 unit tall: https://jsfiddle.net/Lh5t9zoa/ and let flexbox expand them for you.
Add an extra
div
orh1
at the bottom, and now, tell flexbox to expand the top and bottom flex items as much as possible vertically (usingflex: 1 0 auto
), and don't expand the middle flex item (usingflex: 0 0 auto
), and now your links are centered in the page: https://jsfiddle.net/87ohkz0q/
The third method works best on IE 11. Note that the font with 56px is really big on my Mac, so I changed it to 26px.
Vertical align the child elements with different height
Replace your .container
style with.
height: 120px;
display: flex;
border: 1px solid gray;
align-items: center;
justify-content: space-between;
Check the snippet
.container { height: 120px; display: flex; border: 1px solid gray; align-items: center; justify-content: space-between;}
.title { font-size: 18px;}
.action-button { float: right; height: 50px; width: 100px;}
.small-screen { width: 160px; display: inline-block;}
<div class="container">
<span class="title"> Card header </span>
<button class="action-button"> Action </button>
</div>
<br />
<div class="container">
<span class="title small-screen"> Card header title here is longer </span>
<button class="action-button"> Action </button>
</div>
How to vertically center text inside flex element
To center the flex items along the main axis (in your case column/vertically), add justify-content: center;
to the container (and erase the flex settings from the children):
#elem1 { height: 180px; border: 1px solid #ddd; display: flex; flex-direction: column; justify-content: center;}
.inner_element { text-align: center;}
<div id="elem1"> <div class="inner_element">aaaa</div> <div class="inner_element">bbbb</div></div>
Related Topics
Distorted Spacing Between Div Elements After Sorting with Jqui_Sortable
How to Render Bootstrap Dropdown Above the Object
White-Space: Nowrap Is Not Working in Ie in a Horizontally Scrolling Box
CSS Background Color Keyframes Animation
CSS Negative Margins for Positioning
Symfony2, Loading CSS Files Without Cache
Preventing Hover Event of a Div Triggering on Parent Div
Ng-Animate: Conditionally Switching "Back" Transition (Bug)
Selecting an Element That Doesn't Have a Child with a Certain Class
Nth-Child to Alternate by 2 Rows
Css: Problems When Using Object-Fit and Transform Together on Webkit