I advise you to simply put a margin-left
and translateX()
into filho1
so it stays aligned in the middle of the page!
See what's in this example
.pai{
display: flex;
justify-content: space-between;
}
.filho1 {
width: 100px;
height: 100px;
background-color: #f00;
margin-left: 50%;
transform: translateX(-50%);
}
.filho2 {
width: 100px;
height: 100px;
background-color: #0f0;
}
<div class="pai">
<div class="filho1"></div>
<div class="filho2"></div>
</div>
To align the filho1
on the space left in the parent by discounting the width of the filho2
is only of a margin: auto
(so it aligns in the empty space that filho2
leaves inside pai
)
.pai{
display: flex;
justify-content: space-between;
}
.filho1 {
width: 100px;
height: 100px;
background-color: #f00;
margin: 0 auto;
}
.filho2 {
width: 100px;
height: 100px;
background-color: #0f0;
}
<div class="pai">
<div class="filho1"></div>
<div class="filho2"></div>
</div>
EDIT
Dude I was kind of puzzled because at first I wanted to use the justify-self
property, but it did not work! I went searching and found this excellent answer that may open your mind to some things of FlexBox
link
Itseemsthatjustify-self
doesnotapplyatthistimeandalignmentintheMain-axis(X-axis)mustbedoneusingmargins
itself!
Seesomeexamplesofalignmentwithmarginsina.container
withdisplay:flex
Ifyoureversetheaxisbychangingthe
flex-direction
defaultfrom
row
to
column
youcanuse
align-self
toalignasyouwant,howeveritisasifeachitem(%)lineisdifferent.
Butifyouwantyoucancorrectthisbyputtingrow
inthechildren.InthisexampleIdidnotdothisbecauseit'sjustforthedidacticpurposesoftheexample.
Seetheexample:
.pai{
display: flex;
flex-direction: column;
}
.filho1 {
width: 100px;
height: 100px;
background-color: #f00;
align-self: center;
/* position: absolute; se quiser deixa-los na mesma linha de forma mais fácil */
}
.filho2 {
width: 100px;
height: 100px;
background-color: #0f0;
align-self: flex-end;
/* position: absolute; se quiser deixa-los na mesma linha de forma mais fácil */
}
<div class="pai"> pai com: display:flex e flex-direction:column
<div class="filho1">align-self: center;</div>
<div class="filho2"> align-self: flex-end;</div>
</div>