I use flexbox
for my layout and after completing Flexbox Froggy I tried to get the following alignment:
My thinking is the following:
<div>
) need to flow in a column: flex-direction: column;
align-content: flex-start;
align-self: flex-end;
This leads to the HTML code below (also available on JSFiddle)
<div class="container">
<div class="box1">
box 1
</div>
<div class="box2">
box 2
</div>
<div class="box3">
box 3
</div>
</div>
with the CSS
.container {
display: flex;
flex-direction: column;
align-content: flex-start;
}
.box3 {
align-self: flex-end;
}
But it does not work - it looks like to third box is pushed (flex-end
) to the right and not to the bottom.
My understanding was that align-self
handles an exception relative to the container ("the container aligns everything from the top, but I, myself, will align to the bottom"). Isn't that so?
The flex columns can be aligned left or right by using the align-content property in the flex container class. The align-content property changes the behavior of the flex-wrap property. It aligns flex lines. It is used to specify the alignment between the lines inside a flexible container.
In simple words, flex-items now expand from right to left as shown in the given figure. When justify-content is set to “flex-end”, it instantly shifts all the flex-items to the end of the flex-container along the main-axis, i.e flex items get right aligned.
To center a div vertically and horizontally using flexbox, you need to wrap the div or div's inside a container with properties ' display: flex; flex-direction: column; justify-content: center;align-items: center; ', then just make the div ' text-align: center; ' if it has text.
However, in Flexbox you can change the main axis by setting flex-direction to column. In this case, justify-content will align items in the block direction. Therefore it is easiest to think about the main and cross axis when working in Flexbox like so: The main axis = direction set by flex-direction = alignment via justify-content
baseline − The flex item will be aligned at the base line of the cross axis. On passing this value to the property align-self, a particular flex-item will be aligned vertically at the top of the container. The following example demonstrates the result of passing the value flex-start to the align-self property.
You can also use the align-self property on any individual flex item to align it inside the flex line and against the other flex items. In the following example, I have used align-items on the container to set the alignment for the group to center, but also used align-self on the first and last items to change their alignment value.
To avoid being confused by alignment properties, always keep in mind that when you use flexbox, you are working with a one-dimensional model. Even if your layout looks like it has two dimensions (i.e. rows and columns), flex items can only flow in one direction, along the main axis.
You don't need to use align-self
, you can do this with margin-auto
.
Flexbox's Best-kept secret
W3C Spec: Auto Margins
.container {
display: flex;
flex-direction: column;
align-content: flex-start;
height: 150px;
border:1px solid grey;
}
.box3 {
margin-top: auto;
background:lightgreen;
}
<div class="container">
<div class="box1">
box 1
</div>
<div class="box2">
box 2
</div>
<div class="box3">
box 3
</div>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With