Blake Rivell
Blake Rivell

Reputation: 13875

Parent container with flex-direction column and child container with flex-direction row

I have a header element that I want to display flex column so I can center the .container div vertically. This is working fine. I then need the items in .container to flex in a row with even spacing across the 1200px. Are you not able to do flex containers within flex containers going in different directions? Please see my css below:

header {
    height: 50px;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 15px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
}

Upvotes: 3

Views: 3502

Answers (2)

Nolan
Nolan

Reputation: 916

Yes, Flexbox is perfect for nested containers. To illustrate my point, here is an example of basic header using nested Flexbox.

header {
    width: 100vw;
    top: 0;
    left: 0;
    height: 50px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
}

header .image--container {
    position: relative;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    padding: 0.5rem;
    flex: 1 1 20%;
}

header .image--container img {
    width: 100%;
    display: block;
    height: auto;
}

header .menu--container  {
    flex-direction: row;
    justify-content: flex-end;
    align-items: center;
    flex: 1 1 70%;
}

header .menu--container .parent--items {
    display: flex;
    flex-direction: column;
    width: calc(100% / 4);
}

header .avatar--container {
    flex-direction: row;
    justify-content: center;
    align-items: center;
    padding: 0.5rem;
    flex: 1 1 10%;
}

Upvotes: 1

cd3k
cd3k

Reputation: 812

Yes that's definitely possible, I think the flex property will only affect the direct children of a parent element.

<div style="display: flex">
  Anything in here effected by flex
  <div>I'm effected by flex but my children aren't</div>
</div>

That doesn't mean you can't put flex on that child and set different properties (like flex-direction column etc).

It's difficult to work out without seeing more of your code but I'd try removing max-width as that can often effect things in weird ways.

The other thing I'd try is using more containers. You can often solve a problem in CSS by simply wrapping another div around it!

Upvotes: 1

Related Questions