Divs with FLEXBOX do not align correctly

Asked

Viewed 186 times

0

I am new with FLEXBOX and I am developing a layout that would follow this structure:

imagem1 But I was only able to achieve that result:

imagem2

.container {
  width: 100%;
  background-color: #CCC;
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  justify-content: flex-start;
}

.div1 {
  width: 50%;
  height: 200px;
  background-color: #FF0;
  flex-grow: 0;
  order: 1;
}

@media screen and (max-width: 700px) {
  .div1 {
    width: 100%;
    height: 40px;
    background-color: #FF0;
    flex-grow: 0;
    order: 1;
  }
}

.div2 {
  width: 50%;
  height: 40px;
  background-color: #FC0;
  align-self: flex-end;
  flex-grow: 0;
  order: 2;
}

@media screen and (max-width: 700px) {
  .div2 {
    width: 100%;
    height: 40px;
    background-color: #FC0;
    flex-grow: 0;
    order: 2;
  }
}

.div3 {
  width: 50%;
  height: 40px;
  background-color: #F90;
  align-self: flex-end;
  flex-grow: 0;
  order: 3;
}

@media screen and (max-width: 700px) {
  .div3 {
    width: 100%;
    height: 40px;
    background-color: #F90;
    flex-grow: 0;
    order: 3;
  }
}

.div4 {
  width: 50%;
  height: 40px;
  background-color: #F60;
  align-self: flex-end;
  flex-grow: 0;
  order: 4;
}

@media screen and (max-width: 700px) {
  .div4 {
    width: 100%;
    height: 40px;
    background-color: #F60;
    flex-grow: 0;
    order: 4;
  }
}

.div5 {
  width: 50%;
  height: 100px;
  background-color: #0C6;
  flex-grow: 0;
  order: 5;
}

@media screen and (max-width: 700px) {
  .div5 {
    width: 100%;
    height: 40px;
    background-color: #0C6;
    flex-grow: 0;
    order: 5;
  }
}

.div6 {
  width: 50%;
  height: 40px;
  background-color: #396;
  align-self: flex-end;
  flex-grow: 0;
  order: 6;
}

@media screen and (max-width: 700px) {
  .div6 {
    width: 100%;
    height: 40px;
    background-color: #396;
    flex-grow: 0;
    order: 6;
  }
}
<div class="container">
  <div class="div1">1</div>
  <div class="div2">2</div>
  <div class="div3">3</div>
  <div class="div4">4</div>
  <div class="div5">5</div>
  <div class="div6">6</div>
</div>

EDIT

Sorry, I wanted to simplify the situation and so I posted only the layout block that theoretically would matter to the solution of the problem. But the solution (which works perfectly in the presented scenario) presented by hugocsl does not work in the "complete" scenario. Follows the new image:

inserir a descrição da imagem aqui

  • This kind of layout gets better and easier to do using grid.

  • I was doing before with "float" and "clear:Both" and I was referred to "flexbox". I had even opened a topic before but as the subject was walking to the flexbox I had to open this topic...

  • I left an answer there with flex, I do not know if it is to say that one technique is better than the other, but that it is possible to do both ways you can check in the answers :)

1 answer

3


Follow an option with flex, which has the option to use the attribute order in the children and can be distributed in columns using the flex-direction:column.

The detail is that the div 1 occupies 70% of the height, and the div 5 occupies 30% of the height, so the first column is completely filled by these two elements, and the rest of the child elements will be "aligned" in the second column.

inserir a descrição da imagem aqui


EDIT

As the div 5 will have a fixed height, and the div 1 no, just make a calc at the time of div 1, would look like this for example height: calc(100% - var(--altura));, a na div 5 the height is height: var(--altura);

See that var(--altura) is the CSS variable that you will define in :root, so you don’t have to keep changing the value in various places, you change in the :root the value of the variable and this value changes everywhere, and so the height of div 1 will always be 100% minus the antura of div 5

:root {
    --altura: 100px; 
}

Follow the code referring to the image above:

:root {
    --altura: 100px;
}
html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
.container {
    display: flex;
    flex-wrap: wrap;
    flex-direction: column;
    height: 100%;
}
[class^="div"] {
    width: 50%;
}
.div1 {
    height: calc(100% - var(--altura));
    order: 1;
    background-color:#FF0;
}
.div5 {
    height: var(--altura);
    order: 1;
    background-color:#0C6;
}
.div2 {
   background-color:#FC0;
}
.div3 {
   background-color:#F90;
}
.div4 {
   background-color:#F60;
}
.div6 {
   background-color:#396;
}
.div2, .div3, .div4, .div6 {
    order: 2;
}

@media screen and (max-width: 700px) {
    :root {
        --altura: initial;
    }
    .container {
        height: auto;
    }
    [class^="div"] {
        width: 100%;
        order: initial;
    }
}
<div class="container">
    <div class="div1">1</div>
    <div class="div2">2</div>
    <div class="div3">3</div>
    <div class="div4">4</div>
    <div class="div5">5</div>
    <div class="div6">6</div>
</div>

  • hugocsl there is a way to do this without heights (70% and 30%) being set? because Divs have variable height in px...

  • To clarify, all Divs will have fixed height in px less the div1.... this will be variable.

  • @Johnsnowdad has as yes, since you know the height of the div5 it is enough that the height of the div1 is 100% - Height Div5. Read the EDIT that I did in the above answer that I teach to do this in a practical way. The code has already been updated tb to be used with the Variable

  • hugocsl... guy did work! I really appreciate the help and your time. Let me take one last question with you: there is the possibility of getting the same result without having to set the container height to 100%?

  • I say this because this whole block will be inside a larger block (general containerer, header, content, footer)... when the . container gets 100% distorts the entire layout.

  • For that code to work someone has to have a set height. Either div 1 or container... But what I don’t understand is because div 5 has to come after div 1, there it should be div 2 or div 6, the model you are trying to build is not the appropriate one in HTML, to speak of HTML not CSS. In HTML you have to come up with a way that is more coherent. Staying "nice" in CSS is not ideal... Think about this layout, and how you’re going to distribute things, but div 5 "marrying" div 1 gets in the way of things and it doesn’t make any sense...

  • Div5 is a kind of footer of div1 (content)... Divs 2,3 and 4 are complementary contents of div1.... so div5 is always below div1 (desktop/tablet) or div1 + complementary (mobile). div6 is content not related to div1.

  • 1

    Anyway, I’d like to thank you for your help. I will evaluate this scenario and see if I can remodel it because it is making things difficult instead of helping. Great hug to all and may God bless!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.