Vertical Center with Flexbox and alignment of child items [duplicate] - css

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Flexbox: center horizontally and vertically
(14 answers)
Closed 3 years ago.
Only getting around to trying out flexbox now and I'm using it to vertically center content inside a div.
.parent{
display: flex;
align-items: center;
justify-content: center;
}
<div class="parent">
<h1>Title</h1>
<p>Paragraph one.</p>
<p>Paragraph two.</p>
</div>
So when I do this it works but the child elements are placed beside each other like they're not block items anymore. How can I have it that they're vertically centered but stacked on top of each other?
Cheers!

If you want to show elements in column you need to use flex-direction: column style property.
.parent{
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
<div class="parent">
<h1>Title</h1>
<p>Paragraph one.</p>
<p>Paragraph two.</p>
</div>

Related

Place single child at bottom of flex container flex-direction colum? [duplicate]

This question already has answers here:
Center and bottom-align flex items
(3 answers)
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 8 months ago.
HTML:
<div class="wrapper">
<div class="child1"></div>
<div class="child2"></div>
</div>
CSS:
.wrapper {
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
I have two child, I want the first to be at center of page and only the second to be at bottom without using position absolute or affecting first child positioning

CSS flex end by default when only one item in container [duplicate]

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 1 year ago.
I have a container that uses flex like a navbar so i have left and right items using space between property. My issue is that sometimes the left items are non existent and I would like the right items to stay on the right side still. Here is an example:
<div class="container">
<div>
Stay on left
</div>
<div>
Stay on right
</div>
</div>
.container {
display: flex;
justify-content: space-between;
}
When I remove the stay on left div I still need the stay on right div to stay on the right side of the container
<div class="container">
<div>
Stay on right
</div>
<div>
Stay on left
</div>
</div>
.container {
display: flex;
flex-direction: row-reverse;
justify-content: space-between;
}

Sass function for flexbox layout using :nth-of-type(even) [duplicate]

This question already has answers here:
How can I select odd and even elements with CSS?
(7 answers)
Closed 2 years ago.
Good afternoon everyone!
I'm trying to create a layout for image section where every odd-numbered image container has an image on the right, text on the left and every :nth-of-type(even) is basically the opposite, image on the left and text on the right.
This is what I have so far, however the nth-of-type(even) selector completely messes up the layout. Without it the code works except the order of image and text does not alternate. Is there a neat Sass way to loop through these items and for each odd one add flex-direction: row-reverse, for each even one flex-direction: row?
This is my HTML for an individual image container, I have 6 in total:
<div class="image__container right">
<div class="image__container-img">
<img src="" alt='' />
</div>
<div class="image__container-text">
<h3 class="title"></h3>
<p></p>
</div>
</div>
And CSS:
.image__container {
#extend .col;
display: flex;
flex-direction: row-reverse;
align-items: center;
&:nth-of-type(even) {
flex-direction: row;
}
}
You can use > * to select every child of .image__container and with :nth-of-type(even) only the even:
.image__container {
display: flex;
flex-direction: row-reverse;
align-items: center;
&>*:nth-of-type(even) {
flex-direction: row;
}
}

Css - vertical centering of an image inside a div [duplicate]

This question already has answers here:
How to vertically align an image inside a div
(37 answers)
Flexbox: center horizontally and vertically
(14 answers)
How can I vertically align elements in a div?
(28 answers)
Closed 2 years ago.
here HTML:
<div class="user-image text-center">
<img class="avatars" src="css/img/avatar_1.jpg" alt="">
</div>
here CSS:
.user-image {
height: 10vh;
width: 15%;
float: left;
}
I'm trying to centering vertically my image inside my DIV (it's a square) but I'm a beginner and can't find the correct way. I tried vertical-align: middle; and margin: auto; but nothing. Can you suggest me some methods for that?
Use flex styles.
.user-image {
display: flex;
justify-content: center;
align-items: center;
}

Vertically centered: Parent element with 2 child elements [duplicate]

This question already has answers here:
Flexbox: center horizontally and vertically
(14 answers)
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 3 years ago.
Please have a look at this code:
<div class="parent">
<header class="child-1">
<h1 class="float-left">lorem...</h1>
<div class="float-right">lorem...</div>
</header>
<div class="child-2">lorem...</div>
</div>
CSS looks like:
.parent {
display: flex;
align-items: center;
}
Applying this causes both child-1 and child-2 centered vertically in one line. I need that child-2 should start from line2, i.e., right below child-1.
Thanks in advance
Set the flex-direction:
.parent {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 100%;
}
<div class="parent">
<header class="child-1">
<h1 class="float-left">lorem...</h1>
<div class="float-right">lorem...</div>
</header>
<div class="child-2">lorem...</div>
</div>
Then you probably want to adjust the align-items as I'm assuming you don't actually want to center everything horizontally.
.parent {
display: flex;
flex-direction: column;
align-items: center;
}

Resources