How to vertically align one of the flex element childs [duplicate] - css

This question already has answers here:
Align an element to bottom with flexbox
(10 answers)
Closed 5 years ago.
I have 3 vertically centered divs in one flex container with flex-direction:column and I need to place 3rd div to the bottom of the container.
This is an explanation of what I want: https://jsfiddle.net/tom8p7be/
How can I make this?

You can add margin-top: auto on both first and last child divs. When you add margin-top: auto to last-child div it will push that div to end of parent but it will also push other two divs to top of the parent. That is why you also need to add margin-top: auto to first-child div, and that will position them in the center of space from top to last-child div.
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
}
.container div {
margin: 5px;
padding: 5px 10px;
border: 1px solid;
}
.container > div:last-child,
.container > div:first-child{
margin-top: auto;
}
<div class="container">
<div>This div should be vertically centered</div>
<div>This one too</div>
<div>And this div shoud be placed at the bottom of the flex container</div>
</div>

Related

Flex layout takes up full width when a cell's contents break to a second line [duplicate]

This question already has answers here:
Make container shrink-to-fit child elements as they wrap
(4 answers)
CSS when inline-block elements line-break, parent wrapper does not fit new width
(2 answers)
Closed 2 years ago.
I have a layout where I have a number next to a name and I want that layout to be centered in its container. It works fine if the name stays on one line, but if the name requires two lines then the entire flex element takes up the full width even though the broken lines visually don't take up the full space.
Notice how "Aaron Gray" stays on one line and the flexbox (yellow BG) remains centered in it's container (which is visually centered relative to the blue box on top of it, which is what I want)?
But notice how the "Peja Stojakowi" layout takes up the full width even though visually there's a lot of empty space on the right and consequently the num/name combo does not look centered relative to the blue box above it?
Here's the markup and css I'm using:
.LinkMap-playerWrap {
position: relative;
background-color: blue;
.LinkMap-playerWrapNameArea {
position: absolute;
bottom: -10px;
left: -20px;
right: -20px;
transform: translateY(100%);
display: flex;
justify-content: center;
background-color: pink;
.LinkMap-playerWrapNameAreaInner {
display: flex;
align-items: center;
justify-content: center;
background-color: yellow;
.LinkMap-playerWrapNum {
margin-right: 5px;
padding-right: 5px;
border-right: 1px solid #000;
align-self: start;
height: 100%;
}
}
}
}
<div class="LinkMap-playerWrap">
<div class="LinkMap-playerWrapNameArea">
<div class="LinkMap-playerWrapNameAreaInner">
<div class="LinkMap-playerWrapNum">16</div>
<div class="LinkMap-playerWrapName">Peja Stojakowi</div>
</div>
</div>
<div class="Player" style="width: 90px; height: 90px;">
<div class="Player-innerWrap">
</div>
</div>

Flexbox 2 columns but spaced out as 3 [duplicate]

This question already has answers here:
Center one and right/left align other flexbox element
(11 answers)
Closed 4 years ago.
I am looking into a way
I have a main div and 2 other div's inside.
.main {
display: flex;
justify-content: space-between;
}
<div class="main">
<div>Goes in the Middle</div>
<div>Goes on the right</div>
</div>
What I need is the the space-between to space the 2 div's out as if it has 3 div's but only with 2.
How can I do this with flex without having 3 divs?
Just give the divs a width of 33.33333%:
.main {
display: flex;
justify-content: space-between;
}
.main>div {
width: 33.3333%;
/* below for test only */
border: 1px solid red;
box-sizing: border-box;
}
/* to push first div into middle - if you want it on left, remove this */
.main>div:first-child {
margin-left:auto;
}
<div class="main">
<div>Goes in the Middle</div>
<div>Goes on the right</div>
</div>

How to center flex block in center page? [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
Closed 5 years ago.
I have div block with property flex CSS.
This block has 80% width and 50% height of display.
How to display this block in center by horizontal and vertical?
Flex alignment properties apply to the children of a flex container.
So if you have a div with display: flex, and you want to center this div, you make the parent a flex container. Then you can apply flex alignment properties to the div.
div {
display: flex;
width: 80%;
height: 50vh;
border: 1px solid black;
}
body {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
}
<div></div>
Here's a more complete explanation: https://stackoverflow.com/a/33049198/3597276

Center a div to the page while inline with another div [duplicate]

This question already has answers here:
How to place two divs next to each other? [duplicate]
(13 answers)
Closed 5 years ago.
I need to align an element to the center of the screen while having an element to the left of it.
I can achieve this with float left and setting the padding of the center element to match the width of the floated element. But, in this case I will not always know the width.
I do not know how better to explain this. Searchs have failed me.
You can use css flex to get the same flex Guide
body,
html {
margin: 0;
padding: 0;
height: 100%;
}
.wrapper {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;
height: 100%;
}
.box-1 {
padding: 2%;
background: tomato;
flex: 0 0 10%;
}
<div class="wrapper">
<div class="box-1">1</div>
<div class="box-1">2</div>
</div>

Vertically align a child div element with parent div having dynamic height [duplicate]

This question already has answers here:
How can I vertically align elements in a div?
(28 answers)
Closed 6 years ago.
How do I vertically align a child element with the parent having a dynamic height.
CSS code:
parent{
height: 400px;
}
.child{
position:relative;
top:50%;
transform: translateY(-50%;);
}
This sets the child once, then doesn't change. How do I change it so that it changes with the dynamic height of the parent?
You can use display: flex.
.parent {
display: flex;
justify-content: center;
align-items: center;
height: 100px;
width: 100px;
margin: 1em;
background-color: tomato;
}
.child {
width: 10px;
height: 10px;
background-color: yellow;
}
You can use `displa: flex`.
The following doesn't rely on parent's height.
<div class="parent">
<div class="child">
</div>
</div>

Resources