Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to align differently a specific <div> in flexbox?

Tags:

html

css

flexbox

I use flexbox for my layout and after completing Flexbox Froggy I tried to get the following alignment:

enter image description here

My thinking is the following:

  • the boxes (<div>) need to flow in a column: flex-direction: column;
  • they are aligned top-down: align-content: flex-start;
  • one specific box aligns itself to the bottom: 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.

enter image description here

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?

like image 970
WoJ Avatar asked Dec 07 '15 16:12

WoJ


People also ask

How do you align left and right items in Flex?

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.

How do I align a div to the right in Flex?

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.

How do I align divs horizontally Flex?

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.

How to change the axis of alignment in Flexbox?

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

How do you align a flex item vertically in CSS?

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.

How do I align flex items to the center of the container?

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.

How to avoid being confused by alignment properties in Flexbox?

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.


1 Answers

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>
like image 124
Paulie_D Avatar answered Oct 28 '22 09:10

Paulie_D