How do I align Divs from left to right from top to bottom with flexbox?

Asked

Viewed 1,067 times

2

I just met this property and I need to create a social-network with the possibility of including videos... in case I would like the last one added to be at the top left and so on...

that’s my code

.videos{
border: #D42574 1px solid;
margin: 0px 7% auto;
display: flex;
flex-direction: row-reverse;
flex-wrap: wrap-reverse; 
justify-content: space-between;

}

but the last one added is aligned to the right... if I just put "wrap" the sequence changes, taking the videos from the bottom up and from the top down, aligning to the right... What to do?

1 answer

0


Yes it is possible to get what you want, but you will need a trick for some situations...

First to align everything left you need to use justify-content: flex-end; this way if your grid is "complete" everything will be perfect. Below I will explain what I mean by "complete".

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
.container {
    width: 80%;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap-reverse;
    flex-direction: row-reverse;
/* alinha o último item a esquerda */
    justify-content: flex-end;
}
.box {
    border: 1px solid #000;
    height: 100px;
    width:calc(100% / 3);
    box-sizing: border-box;
}
<div class="container">
    <div class="box">1</div>
    <div class="box">2</div>
    <div class="box">3</div>
    <div class="box">4</div>
    <div class="box">5</div>
    <div class="box">6</div>
</div>


But what if it is only 5 elements and not 6. Because with 5 it is not "complete" and the top line will have a "hole" due to the lack of an element to completely fill the grid... For this case you need to create a "spacer", an empty element that you will use to fill this "hole" and keep everything aligned. In case I created a class called .vazio that I use in this element. Tb aria-hidden="true" in the element for it is not visible to screen readers.

See the result, I left commented in the code. (if it is only 4 videos you will need 2 empty elementod at the end)

html, body {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
}
.container {
    width: 80%;
    margin: 0 auto;
    display: flex;
    flex-wrap: wrap-reverse;
    flex-direction: row-reverse;
    justify-content: flex-end;
}
.box {
    border: 1px solid #000;
    height: 100px;
    width:calc(100% / 3);
    box-sizing: border-box;
}
/* classe do elemento vazio */
.box.vazio {
    border: 1px solid transparent;
}
<div class="container">
<!-- elemento vazio para ajustar o grid -->
    <div class="box vazio" aria-hidden="true"></div>
    <div class="box">1</div>
    <div class="box">2</div>
    <div class="box">3</div>
    <div class="box">4</div>
    <div class="box">5</div>
</div>

  • Hi Hi, thank you so much for the demonstration, but how would I "automate" this process? Because, the user would put the videos/ photos and etc and could be only 4 or 5 or 6 and such... how do I make this empty box to be used when necessary, in an automatic way? (there will be a maximum of 3 videos per line, if a room is added, it will be placed underneath.) Thanks for your time!!!

  • @Filipeoliveira the easiest way to solve this is to always leave 2 empty boxes there even having only 1 or 4 or 5 will always line up. Otherwise you will have to do a js to count how many items are in the container and add 1 or 2 empty boxes. So I told you to leave 2 at once rss. I don’t know about js so I don’t know how to solve this way, but you can mark this answer and open another asking for help with js to do this, there is a beast people here from js who can help you

  • So, I did here and did not miss... the box 1 went up alone... while the others grouped below :/

  • Now it’s bad to test here, but then I’ll test it right and tell you

  • @Filipeoliveira is expensive unfortunately tested here and there is no way to leave "automatic", if the last row has only one item you have to for 2 empty box, and if you have 2 vc you have to for 1 empty box, not to leave the 2 empty at once.... You can think of a JS to count how many box has and put 1 or 2 empty, but JS do not understand anything I can not help you in this

Browser other questions tagged

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