2
I have the following menu:
body {
padding: 1rem;
}
.menu {
display: flex;
padding: 0.5rem;
border: solid 1px #000;
}
.item {
border: solid 1px #444;
padding: 0.25rem;
}
.item:not(:last-child) {
margin-right: 0.25rem;
}
<div class="menu">
<span class="item">Esquerda 1</span>
<span class="item right">Direita 1</span>
<span class="item">Esquerda 2</span>
<span class="item">Esquerda 3</span>
<span class="item right">Direita 2</span>
</div>
How do I use flexbox
, all items in class "right
" line up on the right keeping the elements without that class on the left? I also need the remaining space to be between all right and left elements. So:
PS: I do not wish to create two Wrappers additional to separate the elements. I want to know if it is possible to separate them only with CSS and using the mentioned HTML structure.
Cool, Hugo! Thanks for the answer. But is there any way to get into this without adding class
primeiro
?– Luiz Felipe
@Luizfelipe has, but for me it is can be gambiarra rss... vc can create an empty span as last menu item, and in it vc puts flex-Grow: 1; If you care I edit the question with this template
– hugocsl
So you don’t have to... What I really needed was to be able to do without touching the HTML. I’m going to end up with the class approach
primeiro
same. But if you find out or know a way without messing with HTML, I’ll be happy to know!! D– Luiz Felipe
@Luizfelipe a hint, maybe with diplay Grid in place of the Flex display, it is possible to do it without needing the empty Span or the extra class First
– hugocsl
I’m still a little behind in CSS and I don’t know anything about CSS Grid. :v When I go to learn I’ll take a look at an alternative using this. :)
– Luiz Felipe
@Luizfelipe a detail, if you know that the second item will always have the class . right vc can use this CSS, ai do not need the class . first.
.item:nth-child(2) {
 margin-left: auto;
}
– hugocsl
@Luizfelipe sometimes this might interest you https://answall.com/questions/380734/selectr-a-primeira-ocorr%C3%Aancia-da-classe-usando-css/
– hugocsl