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;
}
Related
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
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;
}
}
This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 2 years ago.
I have a flexbox container row and there are three items inside. I want one of them to be in the very center but I want the other two 2 to be slightly over it and to the most left and to the most right respectively. I want something like this:
I tried with aligning and justify content but it did not work.
Thanks in advance.
Here is an illustration of what you want. Just give the middle element a margin-top:
.container {
display: flex;
justify-content: space-between;
}
.container div {
width: 50px;
height: 100px;
background-color: red;
}
.center {
margin-top: 50px;
}
<div class="container">
<div class="right"></div>
<div class="center"></div>
<div class="left"></div>
</div>
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>
This question already has answers here:
Why don't flex items shrink past content size?
(5 answers)
Closed 4 years ago.
When using flexbox, sometimes, like in the example below, when the viewport is not wide enough to contain the content, the flexbox items get higher width than the container.
When and why does this happen?
How can I limit the items to never be wider than the container?
Code:
.container {
display: flex;
flex-flow: row wrap;
justify-content: center;
}
.item-1 {
flex: 1 1;
background-color: yellow;
}
.item-2 {
flex: 1 0 100%;
background-color: green;
}
<div class="container">
<div class="item item-1">
item1item11item111item1111item11111item111111item1111111item11111111item111111111item1111111111
</div>
<div class="item item-2">
item2item22item222item2222item22222item222222item2222222
</div>
</div>
Here is a code pen:
https://codepen.io/anon/pen/MqdaoB
try to give some space between character or give word-break to .item
.item {
word-break: break-word;
}
https://codepen.io/anon/pen/YObyjq