Questions tagged [css-float]

Positioning elements to the left or right of their containers using the CSS float attribute.

Adding float: left or float: right to an element pushes it as far as possible to the chosen side of the document flow. Multiple elements floated in the same direction will stack on top of each other. Floating is extremely useful for arranging elements on the page, but it is also liable to create several interesting layout problems. The clear property is a related attribute sometimes used to solve floating issues.

Floating to the Right - The Backwards Effect

When floating multiple elements to the right side of the document, you will have to list them in reverse order that you want them to appear. This occurs because the first element found in the document gets floated to the right first. The next one which gets floated to the right gets stacked on top of the previous element, stacking to the left. Each element after that stacks to the left on top of the others.

The backwards effect also partially extends to elements which are floated to the right on the same line with elements floated to the left. Any elements you want to float directly to the right of left-floated elements should be placed before those floated to the left. Otherwise, the right-floated elements may end up on the line below.

Removal from the Flow of the Document

One of the major issues developers tend to have is that floated elements get removed from the flow of the document. This means they do not take up any space at all, both vertically, and horizontally. Other elements will expand behind them and only text will wrap around them. Most notably, you will see floated content expanding down past the edge of its parent container. Take for example, a green container with a padding of 20px, which contains a red container that is floated to the left:

Floating example

Since the floated element does not actually take up any space, the parent container has a calculated height of 0, plus the 40px of padding (top and bottom), which creates this adverse effect. Fixing this is simple, though. You can either add overflow: hidden to the parent container (which will work in most cases) or use a clear fix on the parent element (for CSS3-compatible browsers only).

.parent::after {
    content: "";
    clear: both;
    display: block;
    font-size: 0;
}

See the pseudo-element tag wiki for more information after the ::after selector.

Clearing Floated Elements

You can clear elements which are floated to the left, right, or both sides. Keep in mind that clearing floated elements will clear all elements to the specified sides (s). You cannot only clear one or two elements at a time with this property. Also, you should keep in mind that it will only affect those elements which are actually floated in that direction. In the case where an element floated to the left is stacked between two other elements floated to the left, you cannot apply a clear: right to that element to clear the left-floated element which is stacked to its right.

References

Additional Information

5944 questions
1066
votes
15 answers

How do you keep parents of floated elements from collapsing?

Although elements like
s normally grow to fit their contents, using the float property can cause a startling problem for CSS newbies: If floated elements have non-floated parent elements, the parent will collapse. For example:
Nathan Long
  • 122,748
  • 97
  • 336
  • 451
452
votes
21 answers

How to align 3 divs (left/center/right) inside another div?

I want to have 3 divs aligned inside a container div, something like this: [[LEFT] [CENTER] [RIGHT]] Container div is 100% wide (no set width), and center div should remain in center after resizing the container. So I…
serg
  • 109,619
  • 77
  • 317
  • 330
395
votes
12 answers

How do I center floated elements?

I'm implementing pagination, and it needs to be centered. The problem is that the links need to be displayed as block, so they need to be floated. But then, text-align: center; doesn't work on them. I could achieve it by giving the wrapper div…
Mike
  • 6,854
  • 17
  • 53
  • 68
324
votes
11 answers

Floating elements within a div, floats outside of div. Why?

Say you have a div, give it a definite width and put elements in it, in my case an img and another div. The idea is that the content of the container div will cause the container div to stretch out, and be a background for the content. But when I…
DavidR
  • 5,350
  • 6
  • 30
  • 36
317
votes
6 answers

What does the CSS rule "clear: both" do?

What does the following CSS rule do: .clear { clear: both; } And why do we need to use it?
user1708560
313
votes
37 answers

How do I get a div to float to the bottom of its container?

I have floated images and inset boxes at the top of a container using float:right (or left) many times. Now, I need to float a div to the bottom right corner of another div with the normal text wrap that you get with float (text wrapped above and to…
Stephen Martin
  • 9,495
  • 3
  • 26
  • 36
289
votes
16 answers

How to float 3 divs side by side using CSS?

I know how to make 2 divs float side by side, simply float one to the left and the other to the right. But how to do this with 3 divs or should I just use tables for this purpose?
Dameon
  • 2,899
  • 2
  • 16
  • 3
283
votes
6 answers

float:left; vs display:inline; vs display:inline-block; vs display:table-cell;

My Question(s) Are any of these methods preferred by a professional web designer? Are any of these methods prefereed by a web browser when drawing the website? Is this all just personal preference? Are there other techniques I'm missing? Note:…
EGHDK
  • 17,818
  • 45
  • 129
  • 204
236
votes
7 answers

Why doesn't the height of a container element increase if it contains floated elements?

I would like to ask how height and float work. I have an outer div and an inner div that has content in it. Its height may vary depending on the content of the inner div but it seems that my inner div will overflow its outside div. What would be the…
Boy Pasmo
  • 8,021
  • 13
  • 42
  • 67
217
votes
10 answers

How do I keep CSS floats in one line?

I want to have two items on the same line using float: left for the item on the left. I have no problems achieving this alone. The problem is, I want the two items to stay on the same line even when you resize the browser very small. You know...…
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
163
votes
11 answers

Stacking DIVs on top of each other?

Is it possible to stack up multiple DIVs like:
So that all those inner DIVs have the same X and Y position? By default they all go below each other increasing the Y position by the height…
Tower
  • 98,741
  • 129
  • 357
  • 507
154
votes
5 answers

Float right and position absolute doesn't work together

I want a div to be always at the right of its parent div, so I use float:right. It works. But I also want it to not affect other content when inserted, so I use position:absolute. Now float:right doesn't work, my div is always at the left of its…
trbaphong
  • 1,583
  • 2
  • 11
  • 9
140
votes
8 answers

How to Create Grid/Tile View?

For example, I have some class .article, and I want to view this class as grid view. So I applied this style: .article{ width:100px; height:100px; background:#333; float:left; margin:5px; } That style will make the .article look…
Ariona Rian
  • 9,153
  • 3
  • 24
  • 36
136
votes
15 answers

How to make a div with no content have a width?

I am trying to add a width to a div, but I seem to be running into a problem because it has no content. Here is the CSS and HTML I have so far, but it is not working: CSS body{ margin:0 auto; width:1000px } ul{ width:800px; } ul…
runeveryday
  • 2,751
  • 4
  • 30
  • 44
132
votes
5 answers

Advantages of using display:inline-block vs float:left in CSS

Normally, when we want to have multiple DIVs in a row we would use float: left, but now I discovered the trick of display:inline-block Example link here. It seems to me that display:inline-block is a better way to align DIVs in a row, but are there…
Ryan
  • 10,041
  • 27
  • 91
  • 156
1
2 3
99 100