Align all item to left except last in a flexbox container [duplicate] - html

This question already has answers here:
In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
(6 answers)
Closed 3 years ago.
Following is my code in which I am trying to align the last div (class="four") to the right and I am using align-self: flex-end; but still its not going to the right. Let me know what I am doing wrong here.
.container {
display: flex;
flex-direction: row;
border: 2px solid blue;
}
.one {
background: red;
}
.two {
background: yellow;
}
.three {
background: pink;
}
.four {
background: teal;
display: inline-block;
align-self: flex-end;
}
<div class="container">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>

margin-left:auto; will do the job.
One use of auto margins in the main axis is to separate flex items
into distinct "groups"...
.container {
display: flex;
flex-direction: row;
border: 2px solid blue;
}
.one {
background: red;
}
.two {
background: yellow;
}
.three {
background: pink;
}
.four {
background: teal;
display: inline-block;
margin-left:auto;
}
<div class="container">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>

use margin-left: auto
.container {
display: flex;
flex-direction: row;
border: 2px solid blue;
}
.one {
background: red;
}
.two {
background: yellow;
}
.three {
background: pink;
}
.four {
background: teal;
display: inline-block;
margin-left: auto;
}
<div class="container">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>

Align self property is used to adjust the flex items on the cross axis.
Please try this code.
.container {
display: flex;
flex-direction: row;
border: 2px solid blue;
}
.one {
background: red;
}
.two {
background: yellow;
}
.three {
background: pink;
}
.four {
background: teal;
margin-left: auto;
}
<div class="container">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>

Another way to do.
.container {
display: flex;
flex-direction: row;
border: 2px solid blue;
position: relative;
}
.one {
background: red;
}
.two {
background: yellow;
}
.three {
background: pink;
}
.four {
background: teal;
right: 0;
position: absolute;
}
<div class="container">
<div class="one">One</div>
<div class="two">Two</div>
<div class="three">Three</div>
<div class="four">Four</div>
</div>

Related

Align first flex item to flex end

I can't touch the HTML file and the non-grey items should not change it's position
I'm trying to have the .grey item be at the bottom of the other 4 items. I know there is another question very similar to this one but they are working with columns and I'm not so none of the answers there helped me.
.red {
background-color: #900;
}
.green {
background-color: #090;
}
.blue {
background-color: #00F;
}
.purple {
background-color: #63C;
}
.grey {
background-color: #666;
}
.container {
background-color: #FFF;
width: 50%;
height: 70%;
min-height: 400px;
margin: auto;
border: 2px solid black;
}
.container {
display: flex;
flex-wrap: wrap-reverse;
justify-content: space-between;
}
.item {
width: 45%;
margin-bottom: 5%;
margin-left: 2%;
margin-right: 2%;
margin-top: 0;
}
.item:last-child {
width: 100%;
}
<div class="container">
<div class="item red"></div>
<div class="item green"></div>
<div class="item blue"></div>
<div class="item purple"></div>
<div class="item grey"></div>
</div>
I need the grey rectangle to be on the bottom
You can add order to the item. It will align wherever you want.
<div class="container">
<div class="item red"></div>
<div class="item green"></div>
<div class="item blue"></div>
<div class="item purple"></div>
<div class="item grey"></div>
</div>
.red { background-color: #900; }
.green { background-color: #090; }
.blue { background-color: #00F; }
.purple { background-color: #63C; }
.grey { background-color: #666; }
.container {
background-color: #FFF;
width: 50%;
height: 70%;
min-height: 400px;
margin: auto;
border: 2px solid black;
}
.container {
display: flex;
flex-wrap: wrap-reverse;
justify-content: space-between;
}
.item {
width: 45%;
margin-bottom: 5%;
margin-left: 2%;
margin-right: 2%;
margin-top: 0;
}
.item:last-child{
width: 100%;
order: -1; // It will make this item as first. Since it is reversed, this will be the last.
}
you can easily do it with add an order to .grey class
.grey{
order: -1;
width: 100%;
}
because it's justify-content is wrap-reverse ,
in the case you use wrap , you can add order: 1; or higher.
If You want to have div class .item grey on the bottom, In this specific case, You have to change flex-wrap: wrap-reverse to flex-wrap: wrap that's it ;-) Best regards !
.red { background-color: #900; }
.green { background-color: #090; }
.blue { background-color: #00F; }
.purple { background-color: #63C; }
.grey { background-color: #666; }
.container {
background-color: #FFF;
width: 50%;
height: 70%;
min-height: 400px;
margin: auto;
border: 2px solid black;
}
.container {
display: flex;
flex-wrap: wrap;
justify-content: space-between;
}
.item {
width: 45%;
margin-bottom: 5%;
margin-left: 2%;
margin-right: 2%;
margin-top: 0;
}
.item:last-child{
width: 100%;
}
<div class="container">
<div class="item red"></div>
<div class="item green"></div>
<div class="item blue"></div>
<div class="item purple"></div>
<div class="item grey"></div>
</div>

Position absolute but relatif

Currently I use this CSS to place a div (.child_bottom) at the bottom of its parent and another div above it (.child) because I know the height of (.child_bottom).
The parent at a variable height.
.parent
{
position:relative;
}
.child
{
position:absolute;
bottom:250px;
}
.child_bottom
{
position:absolute;
bottom:0;
height:250px;
}
<div class="parent">
<div class="child"></div>
<div class="child_bottom"></div>
</div>
But I would like to obtain the same thing with a variable height of .child_bottom, how to do?
Thank you in advance for your help.
Using flex would allow you to remove all of the absolute positioning:
.parent {
height: 400px;
outline: 1px solid blue;
display: flex;
flex-direction: column;
justify-content: flex-end;
}
.child {
background: green;
}
.child_bottom {
background: orange;
height: 250px;
}
<div class="parent">
<div class="child">CHILD</div>
<div class="child_bottom">CHILD_BOTTOM</div>
</div>
If you did wish to add content before .child, remove the justify-content and make this content flex: 1:
.parent {
display: flex;
flex-direction: column;
/* justify-content: flex-end; */
height: 400px;
outline: 1px solid blue;
}
.child {
background: green;
}
.child_bottom {
background: orange;
height: 250px;
}
.other_content {
background: yellow;
flex: 1;
}
<div class="parent">
<div class="other_content">
OTHER_CONTENT (Fills the space)
</div>
<div class="child">CHILD</div>
<div class="child_bottom">CHILD_BOTTOM</div>
</div>

CSS: Items at the bottom of parent element

I can override align-items: flex-start with align-self: flex-end for a specific flex item, is there any way to override justify-content for a
specific flex item?
I have a box with three child items and I need the last item (item-3) at the bottom of my box (flex_wrapper)
like this image:
Check my code please:
.item-1{
background: red;
}
.item-2{
background: blue;
}
.item-3{
background: yellow;
}
.flex_wrapper{
background: #ddd;
width: 400px;
height: 300px;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.flex_items{
padding: 10px;
}
<div class="flex_wrapper">
<div class="flex_items item-1">Item 01</div>
<div class="flex_items item-2">Item 02</div>
<div class="flex_items item-3">Item 03</div>
</div>
Note: It is possible by using margin property or position property,
but I have to use flex-box.
Yes, this is actually very simple, just give the element you want at the bottom margin-top:auto. No wrappers or extra elements required.
.item-3{
margin-top:auto;
}
.item-1 {
background: red;
}
.item-2 {
background: blue;
}
.item-3 {
background: yellow;
margin-top: auto;
}
.flex_wrapper {
background: #ddd;
width: 400px;
height: 300px;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.flex_items {
padding: 10px;
}
<div class="flex_wrapper">
<div class="flex_items item-1">Item 01</div>
<div class="flex_items item-2">Item 02</div>
<div class="flex_items item-3">Item 03</div>
</div>
You need to add an invisible item in between with flex-grow: 1 to occupy the space and push your 3rd item down:
.item-1{
background: red;
}
.item-2{
background: blue;
}
.item-3{
background: yellow;
}
.flex_wrapper{
background: #ddd;
width: 400px;
height: 300px;
display: flex;
flex-direction: column;
justify-content: flex-start;
}
.flex_items{
padding: 10px;
}
.separator {
flex-grow: 1;
}
<div class="flex_wrapper">
<div class="flex_items item-1">Item 01</div>
<div class="flex_items item-2">Item 02</div>
<div class="separator"></div>
<div class="flex_items item-3">Item 03</div>
</div>

Accomplishing this grid with css

Can I accomplish this grid layout with flexbox? To have the first element take up 2 rows height and then continue after it?
Check image.
You can achive it by dividing this layout in 2 columns while the 2nd column will have a nested flexbox layout as well.
HTML Structure:
<div class="container">
<div class="col box1">1</div>
<div class="col col2">
<div class="box2">2</div>
<div class="box3">3</div>
<div class="box4">4</div>
<div class="box5">5</div>
</div>
</div>
Necessary Styles:
.container {
min-height: 100vh;
display: flex;
}
.col {
flex-grow: 1;
color: #fff;
}
.col2 {
flex-wrap: wrap;
display: flex;
}
.col2 > div {
flex-basis: 50%;
flex-grow: 1;
}
.box1 {
display: flex;
}
* {box-sizing: border-box;}
body {
margin: 0;
}
.container {
min-height: 100vh;
display: flex;
}
.col {
flex-grow: 1;
color: #fff;
}
.col2 {
flex-wrap: wrap;
display: flex;
}
.col2 > div {
flex-basis: 50%;
padding: 10px;
flex-grow: 1;
}
.box1 {
background: brown;
padding: 10px;
display: flex;
}
.box2 {
background: pink;
}
.box3 {
background: black;
}
.box4 {
background: yellow;
}
.box5 {
background: royalblue;
}
<div class="container">
<div class="col box1">1</div>
<div class="col col2">
<div class="box2">2</div>
<div class="box3">3</div>
<div class="box4">4</div>
<div class="box5">5</div>
</div>
</div>
You can use this HTML structure but you need to set fixed height on parent div. Then you just use flex-direction: column and flex-wrap: wrap.
* {
box-sizing: border-box;
}
.content {
height: 100vh;
display: flex;
flex-direction: column;
flex-wrap: wrap;
}
div div:first-child {
flex: 0 0 100%;
width: 50%;
background: #880015;
}
div div:not(:first-child) {
width: 25%;
flex: 0 0 50%;
border: 1px solid black;
}
<div class="content">
<div class="box">1</div>
<div class="box">2</div>
<div class="box">3</div>
<div class="box">4</div>
<div class="box">5</div>
</div>

What is the equivalent to moz-box-orient?

I've been looking around the internet and through a number of different reference lists, but can't find the "universal" equivalent to -moz-box-orient.
The problem I am currently met with is in reversing my boxes without reversing the entire placement.
This is what I want to achieve, that is, keeping all of the boxes to the left hand side through orientation:
display: -moz-box;
-moz-box-orient: horizontal;
-moz-box-direction: reverse;
#parent {
border: 2px solid blue;
display: -moz-box;
-moz-box-orient: horizontal;
-moz-box-direction: reverse;
}
#parent > div {
border: 2px solid red;
padding: 20px;
margin: 10px;
}
#child1 { background: green; }
#child2 { background: yellow; }
#child3 { background: red; }
#child4 { background: orange; }
<section id="parent">
<div id="child1">Child #1</div>
<div id="child2">Child #2</div>
<div id="child3">Child #3</div>
<div id="child4">Child #4</div>
</section>
This is my attempt without -moz-box-orient:
display: flex;
flex-direction: row-reverse;
#parent {
border: 2px solid blue;
display: flex;
flex-direction: row-reverse;
}
#parent > div {
border: 2px solid red;
padding: 20px;
margin: 10px;
}
#child1 { background: green; }
#child2 { background: yellow; }
#child3 { background: red; }
#child4 { background: orange; }
<section id="parent">
<div id="child1">Child #1</div>
<div id="child2">Child #2</div>
<div id="child3">Child #3</div>
<div id="child4">Child #4</div>
</section>
To surmise my question once more:
Does anyone know the equivalent to -moz-box-orient: horizontal;?
box-orient is a property of the original CSS Flexible Box Layout Module draft, and has been replaced in newer drafts.
A more or less equivalent is flex-direction, which you already use.
If you want to align the elements to the left, you can try justify-content:
justify-content: flex-end;
#parent {
border: 2px solid blue;
display: flex;
flex-direction: row-reverse;
justify-content: flex-end;
}
#parent > div {
border: 2px solid red;
padding: 20px;
margin: 10px;
}
#child1 { background: green; }
#child2 { background: yellow; }
#child3 { background: red; }
#child4 { background: orange; }
<section id="parent">
<div id="child1">Child #1</div>
<div id="child2">Child #2</div>
<div id="child3">Child #3</div>
<div id="child4">Child #4</div>
</section>
However, note that with your old code, the reversed items are aligned to the right side too, but you don't see it because the container shrinks because of display: -moz-box.
If you want the same behavior, you can use display: inline-flex:
#parent {
border: 2px solid blue;
display: inline-flex;
flex-direction: row-reverse;
}
#parent > div {
border: 2px solid red;
padding: 20px;
margin: 10px;
}
#child1 { background: green; }
#child2 { background: yellow; }
#child3 { background: red; }
#child4 { background: orange; }
<section id="parent">
<div id="child1">Child #1</div>
<div id="child2">Child #2</div>
<div id="child3">Child #3</div>
<div id="child4">Child #4</div>
</section>