Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  143] [ 4]  / answers: 1 / hits: 34651  / 6 Years ago, wed, june 13, 2018, 12:00:00

I want to align an item in the primary axis. For example, I want to have a row with a few children all aligned left, and then one child aligned on the right side.



You could achieve that effect with something like position: absolute; right: 0, but I'm wondering if theres a better way. It seems like there ought to be a justifySelf property, that only affects one child and affects its alignment on the primary axis, in the same way the alignSelf affects one child's alignment on the secondary axis.



Yet no such justifySelf seems to exist. Why is this?



It's similar to this question but not quite: How can you float: right in React Native?


More From » reactjs

 Answers
4

I don't know React Native, but I do know flexbox!



Use the following code as a guide:



<div style=display: flex;>

<div>
I'll be on the left side
</div>
<div>
I'll be hugging the guy on the left side
</div>
<div>
I'll be hugging the guy hugging the guy on the left side
</div>
<div style=margin-left: auto;>
I'll be hugging the right side far away from those other guys
</div>

</div>


The margin set on the last child will push all other children to the left as far as their styles will allow, and push itself as far right as any other styles will allow.



You can test this out by also adding margin-right: auto; to the last child, and you will see the last child centered perfectly in the remaining space of the parent div, after the first three children take up their allotted space. This is because the competing margin autos will both share equally whatever space remains, since they can't cancel each other out and won't override each other.



Flex box was designed to handle margin spacing like this, so take advantage of it, as well as the other unique spacing options available under the justify-content property.



Helpful article: https://hackernoon.com/flexbox-s-best-kept-secret-bd3d892826b6


[#54203] Sunday, June 10, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eanskylerg

Total Points: 524
Total Questions: 107
Total Answers: 100

Location: Colombia
Member since Mon, May 2, 2022
2 Years ago
;