Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

float does not work in a flex container

Tags:

html

css

flexbox

I want to position text (foo link) in right of the footer element.

I need footer display to remain flex.

But when I set it to flex, float:right for span doesn't work anymore.

<footer style="display: flex;">       <span style="text-align: right;float: right;">          <a>foo link</a>       </span>  </footer>

https://jsfiddle.net/dhsgvxdx/

like image 873
mlibre Avatar asked Aug 28 '16 18:08

mlibre


People also ask

Does float work with Flex?

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 a div float right in Flex?

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 .

Is float better than Flex?

These are the following reasons to use flexbox over floats.Flexbox is responsive and mobile-friendly. Flex container's margins do not collapse with the margins of its content. We can easily change the order of elements on our webpage without even making changes in HTML.

How can we fix the floating problem in CSS?

To fix this problem, the footer can be cleared to ensure it stays beneath both floated columns. Clear has four valid values as well. Both is most commonly used, which clears floats coming from either direction. Left and Right can be used to only clear the float from one direction respectively.


1 Answers

The float property is ignored in a flex container.

From the flexbox specification:

3. Flex Containers: the flex and inline-flex display values

A flex container establishes a new flex formatting context for its contents. This is the same as establishing a block formatting context, except that flex layout is used instead of block layout.

For example, floats do not intrude into the flex container, and the flex container’s margins do not collapse with the margins of its contents.

float and clear do not create floating or clearance of flex item, and do not take it out-of-flow.

Instead, just use flex properties:

footer {      display: flex;      justify-content: flex-end;  }
<footer>      <span>         <a>foo link</a>      </span>  </footer>

If you have more items in the footer, and need other alignment options, then here are two guides:

  • In CSS Flexbox, why are there no "justify-items" and "justify-self" properties?
  • How does flex-wrap work with align-self, align-items and align-content?
like image 70
Michael Benjamin Avatar answered Oct 02 '22 17:10

Michael Benjamin