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

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;
}

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

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;
}
}

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

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>

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;
}

Flexbox row: doesn't grow according to content? [duplicate]

This question already has answers here:
What are the differences between flex-basis and width?
(6 answers)
Closed 4 years ago.
I have the following structure, and I'd like to understand why my row does not grow with its inner content.
.row {
border:solid red;
display: flex;
flex-direction: row;
}
.cell {
border: solid green;
flex: 0 0 400px;
}
<div class="row">
<div class="cell">
cell 1
</div>
<div class="cell">
cell 2
</div>
<div class="cell">
cell 3
</div>
</div>
JsFiddle
I don't want the cells to grow or shrink, they should always be 400px width. The parent should grow and overflow the window, and an horizontal scrollbar is expected.
In the above example, the red border should be around the green border. The green border has the expected dimensions, not the red one.
I don't want to set a static width to my row for maintainability reasons related to my usecase.
I'm open for non-flexbox solutions (see inline-block attempt), but would appreciate more a flexbox one (because I need align-items: center behavior on the row)
If you want your container to always match the width of it's children, you'll need to look into display: inline-flex.
display: flex behaves more like a container with a width of 100%
Here's a fiddle that should work:
http://jsfiddle.net/hnrs64fm/

Resources