You get a bonus - 1 coin for daily activity. Now you have 1 coin

Alignment and Floating (Text Wrapping) in CSS

Lecture



CSS Versions

CSS 1CSS 2CSS 2.1CSS 3

Description

Defines which side an element is aligned to, with the other elements wrapping around it on the other sides. When the value of the float property is none, the element is displayed on the page normally, although one line of the wrapping text is allowed to be on the same line as the element itself.

Syntax

float: left | right | none | inherit

Values

left
Aligns the element to the left edge, with all other elements, such as text, wrapping around it on the right side.
right
Aligns the element to the right edge, with all other elements wrapping around it on the left side.
none
No wrapping is applied to the element.
inherit
Inherits the value of the parent.

Example

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8">
  <title>float</title>
  <style>
   .layer1 {
    float: left; /* Wrapping on the right side */
    background: #fd0; /* Background color */
    border: 1px solid black; /* Border parameters */
    padding: 10px; /* Padding around the text */
    margin-right: 20px; /* Right margin */
    width: 40%; /* Block width */
   }
  </style>
 </head>
 <body>
  <div class="layer1">
   Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diem nonummy nibh
   euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.
  </div>
  <div>
   Duis autem dolor in hendrerit in vulputate velit esse molestie consequat, vel
   illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio
   dignissim qui blandit praesent luptatum zzril delenit au gue duis dolore te
   feugat nulla facilisi.
  </div>
 </body>
</html>


The result of this example is shown in fig. 1.

Alignment and Floating (Text Wrapping) in CSS

Fig. 1. Applying the float property

Object model

[window.]document.getElementById("elementID").style.styleFloat

[window.]document.getElementById("elementID").style.cssFloat

Browsers

Internet Explorer 6 has a bug that doubles the value of the left or right margin for floated elements nested inside parent elements. The margin that is doubled is the one adjacent to the side of the parent. The problem is usually solved by adding display: inline for the floated element. This browser also adds a 3px offset (the so-called "three-pixel bug") in the direction specified by the float value.

Internet Explorer up to and including version 7.0 does not support the inherit value.

Category:
Formatting
created: 2014-08-16
updated: 2026-03-09
590



Was this answer useful?
Choose a quick rating so we can improve the next answer for you.
How satisfied are you?


Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Cascading CSS / CSS3 Style Sheets"

Terms: Cascading CSS / CSS3 Style Sheets