Sunday 16 December 2012

What is CSS Float ?

With CSS float, an element can be pushed to the left or right, allowing other elements to wrap around it.

Float is very often used for images, but it is also useful when working with layouts.



How Elements Float

Elements are floated horizontally, this means that an element can only be floated left or right, not up or down.
A floated element will move as far to the left or right as it can. Usually this means all the way to the left or right of the containing element.
If an image is floated to the left, a following text flows around it, to the right:

Example

img
{
float:left;
}



Floating Elements Next to Each Other

If you place several floating elements after each other, they will float next to each other if there is room.
Here we have made an image gallery using the float property:

Example

.float
{
float:left;
width:110px;
height:90px;
margin:5px;
}

Elements after the floating element will flow around it. To avoid this, use the clear property.
The clear property specifies which sides of an element other floating elements are not allowed.
Add a text line into the image gallery, using the clear property:

Example

.clear
{
clear:both;
}

No comments:

Post a Comment