Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox float right

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.

like image 725
Martin Konicek Avatar asked Apr 13 '16 14:04

Martin Konicek


People also ask

How do you float a flex to the right?

So if you want to position child element to right of parent element you can use margin-left: auto but now child element will also push other div to the right as you can see here Fiddle .

How do you float left and right in flexbox?

The flex columns can be aligned left or right by using the align-content property in the flex container class. The align-content property changes the behavior of the flex-wrap property. It aligns flex lines. It is used to specify the alignment between the lines inside a flexible container.

Can you use float with flexbox?

Floats were used before Flexbox and Grid to create entire layouts. It isn't used as much anymore, but you may encounter it in legacy code. The float property essentially "floats" an element to the left or right of its container.

How do you make an element float right?

Syntax: clear: none | both | right | left; Let us how to clear the right floating elements of the layout using this clear property. In the above program, we can see that we have cleared the text element to the right side of the element layout.


2 Answers

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>
like image 98
pradeep1991singh Avatar answered Sep 24 '22 01:09

pradeep1991singh


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/

like image 28
BitOfUniverse Avatar answered Sep 23 '22 01:09

BitOfUniverse