43

I am looking for solutions on Flexbox layout best practice for a common use case.

example

In the example, I want to use Flexbox to make the score number to be float to the right. I thought of using:

position: absolute;
right: 0;

But then the problem is that we can't use the center align with the outer box.

Another way I can think of is to make another flex box to wrap the image and name part, then create an outer flex box to use

justifyContent: space-between;

to make the expected layout.

icyerasor
  • 4,613
  • 1
  • 40
  • 47
Martin Konicek
  • 36,412
  • 20
  • 88
  • 95
  • 3
    Consider flex `auto` margins: http://stackoverflow.com/a/33856609/3597276 – Michael Benjamin Apr 13 '16 at 14:24
  • I found `position: absolute; right 0;` still very useful when "floating" something to the right that should span over multiple other flex-item "rows" that should not be affected by the item pinned to the right. Using 'float: right;' in chrome somehow affects other flex-contents that follow afterwards (they won't take up the space occupied by the right-floated content). They need to be converted to `display: block` to take up the space correctly, but I'm more happy using absolute positioning in that particular case. – icyerasor Jul 13 '21 at 16:27

3 Answers3

93

This will help you

.parent {
  display: flex;
}

.child2 {
  margin-left: auto;
}
<div class="parent">
  <div class="child1">left</div>
  <div class="child2">right</div>  
</div>
Paweł Gościcki
  • 8,327
  • 5
  • 65
  • 76
pradeep1991singh
  • 7,640
  • 3
  • 20
  • 31
17

Use justify-content: space-between it will push elements to the sides:

.flex {
 display: flex;
 justify-content: space-between;
}

.flex-grow {
  flex-grow: 1;
}

.one {
  border: 1px solid black;
  width: 20px; height: 20px;
}

.two {
  border: 1px solid black;
  width: 20px; height: 20px;
}

.three {
  border: 1px solid black;
  width: 20px; height: 20px;
}
Growing middle element
<div class="flex">
  <div class="one"></div><div class="two flex-grow"></div><div class="three"></div>
</div>

Without growing element
<div class="flex">
  <div class="one"></div><div class="two"></div><div class="three"></div>
</div>

Without middle element
<div class="flex">
  <div class="one"></div><div class="three"></div>
</div>

Refer to this amazing tutorial to easily understand how flex box behaves: https://css-tricks.com/snippets/css/a-guide-to-flexbox/

icyerasor
  • 4,613
  • 1
  • 40
  • 47
BitOfUniverse
  • 5,663
  • 1
  • 33
  • 38
  • This answer works a lot better than @pradeep1991singh answer. `margin-left:auto` will cause unexpected results in a lot of places. – Seth B Mar 02 '21 at 00:22
9

Use this awesome guide to answer common Flexbox questions: https://css-tricks.com/snippets/css/a-guide-to-flexbox

Pseudocode:

<View style={{flexDirection: 'row', alignItems: 'center'}}>
  <ProfilePicture />
  <Text style={{flex: 1}}>{username}</Text>
  <ScoreNumber />
</View>

This renders the 3 elements next to each other, with the Text occupying all the available space, therefore pushing the ScoreNumber to the right.

Martin Konicek
  • 36,412
  • 20
  • 88
  • 95