Skip to content Skip to sidebar Skip to footer

Do You Really Need A Width On Floated Element?

Say you have a Div (id='toolbar'), and inside that toolbar you have a Div (id='ButtonHolder') that contains 2 buttons. If you float the #ButtonHolder to the right and don't set an

Solution 1:

A change from "save" to "apply" isn't going to take up much more room, to be honest.

In principle, yes you should always set a width - if you don't, then say you have the button float:left; and another <div> float:right;. If you don't set widths, they're not going to take up the full screen width, so any elements you put in below are going to try to position themselves in the gap between the two.

It is also a good idea to have a 100% width container element for this particular scenario to prevent the described effects.


Solution 2:

float and position usually come with a cost. You should try to first find other ways to position elements within your layout. You can should consider other options such as margin, padding, display: inline-block;, text-align ... etc.

I would recommend reading this.

To answer your question directly. Setting width for floats is not written on stone but not doing so, usually means trouble later. At least in my personal point of view.


Post a Comment for "Do You Really Need A Width On Floated Element?"