Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't justify-content centering my divs?

Tags:

html

css

flexbox

I am trying to center two divs inside the daddy div horizontally.

Daddy div is set to flex-direction: column as I want the child divs one below another, but at the center of the page.

justify-content: center; should do it but not working.

I finally made it work with align-self, but any explanation why this much code is not enough to center the divs inside?

Here is my code:

<div class="main">
    <div class="child-1">HI</div>
    <div class="child-2">WE BUILD AWESOME STUFF</div>
</div>

.main {
    display: flex;
    flex-direction: column;
    justify-content: center;
}
like image 358
RP McMurphy Avatar asked Jan 30 '16 20:01

RP McMurphy


People also ask

Why are my DIVS not centering?

You can't center divs with margin: 0 auto; if you have not set width to element. Add width to . top-center to make it work.

How do I center justify a div?

You can do this by setting the display property to "flex." Then define the align-items and justify-content property to “center.” This will tell the browser to center the flex item (the div within the div) vertically and horizontally.

Why does justify-content not work?

justify-content only has an effect if there's space left over after your flex items have flexed to absorb the free space. In most/many cases, there won't be any free space left, and indeed justify-content will do nothing.

Can I use justify-content space evenly?

The "space-evenly" value for the justify-content property distributes the space between items evenly. It is similar to space-around but provides equal instead of half-sized space on the edges. Can be used in both CSS flexbox & grid.


1 Answers

Main vs Cross Axis

Consider the main axis and cross axis of a flex container:

enter image description here                                                                                                                        Source: W3C

In the image above, the main axis is horizontal and the cross axis is vertical. These are the default directions for a flex container.

However, these directions can be easily switched with the flex-direction property.

/* main axis is horizontal, cross axis is vertical */
flex-direction: row;
flex-direction: row-reverse;

/* main axis is vertical, cross axis is horizontal */    
flex-direction: column;
flex-direction: column-reverse;

(The cross axis is always perpendicular to the main axis.)


justify-content property (works on main axis only)

The justify-content property aligns flex items along the main axis of the flex container.

In other words, when your container is flex-direction: row, that makes the main axis horizontal. justify-content: center will work as you expect.

But you've set the container to flex-direction: column. This means that the main axis is now vertical, and justify-content will position flex items up/down, not left/right.

Since you have no extra height in your example, you won't notice anything different; justify-content has no space to work. (Unlike width, which block elements fill 100% by default, heights must be defined. Otherwise, elements default to auto – the height of the content.) But give the container some height and see what happens.


align-* properties (work on cross axis only)

The align-self, align-items and align-content properties operate on a flex container's cross axis (again, always perpendicular to the main axis).

Because your main axis is vertical, the align-* properties will align flex items left/right. That's why align-self worked to center your divs.


Quick Summary: Depending on the flex-direction, the main axis and cross axis switch, taking their assigned properties with them.


Solution

If your goal is minimal code, here's all you need:

.main {
    display: flex;
    flex-direction: column;
    align-items: center;
}

More details: In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?

like image 157
Michael Benjamin Avatar answered Sep 22 '22 11:09

Michael Benjamin