CSS FLOAT PROPERTY

The CSS float property can make HTML elements float khổng lồ the left or right inside their parent element. Content inside the same parent element will move sầu up và wrap around the floating element. In this CSS float tutorial I will explain how the CSS float property works in more detail.

Bạn đang xem: Css float property

CSS Float Example

To illustrate how the CSS float property works, let us first look at an HTML example:


The HTML code contains a div element which has a text, two div elements & another text inside its body. When rendered, here is what that looks like:

*

In the following sections I will show you how lớn make the two nested div elements float left & right inside their parent element using the CSS float property.

float : left

Now, let us try to make the first nested div element float left using the float CSS property. We vì so by setting the CSS float property of the HTML element lớn the value left. Here an example showing a div element with the CSS float property set to left:


This is the first text
float: left; border: 2px solid red;">This is box 1
This is box 2
This is the last text
Here is what the example looks like with the left floating element when rendered in the browser:

*

Notice how the first div element (with the red border) now floats khổng lồ the left inside its parent element. The first text is now wrapping nicely around the first div element, to the right of it. The second div element is still positioned below the first div element, & the last text below that.

Now, let us try to make the second nested div float left too. Here is what the code looks like:


This is the first text
float: left; border: 2px solid red;">This is box 1
float: left; border: 2px solid green;">This is box 2
This is the last text
And here is what the code looks lượt thích when rendered in the browser:

*

Now both the first & second nested div element is floating to the left inside their parent element. The text wraps nicely around the two floating elements.

float : right

Now let us try lớn make the second nested div float lớn the right instead. We do so by setting its float CSS property to right instead of left. Here is the code:


This is the first text
float: left; border: 2px solid red;">This is box 1
float: right; border: 2px solid green;">This is box 2
This is the last text
Here is what it looks like when rendered in the browser:

*

See how the two elements are now floating in each their direction inside their parent element. They are even located at the same vertical position (towards the top of the parent element). The text now wraps around the two div elements by being rendered between the div elements.

Xem thêm: Set React Input Field Value From Javascript Or Jquery? Jquery Assign Value To Input

clear

Now, let us add a third div element which also floats left:


This is the first text
float: left; border: 2px solid red;">This is box 1
float: right; border: 2px solid green;">This is box 2
float: left; border: 2px solid red;">This is box 3
This is the last text
This is what the code looks lượt thích now, when rendered in a browser:

*

Notice how the two left floating elements are positioned at the same horizontal "line", after each other.

Imagine now that you want the two div elements with the red borders khổng lồ float left, but under each other instead of next lớn each other. To do that, you need to phối the clear CSS property.

The clear CSS property can take one of these values:

left right both none

The left value means that the element should stay clear of all left floating elements. The right value means that the element should stay clear of all right floating elements. The both value means that the element should stay clear of both left and right floating elements. The none value means no clearing, which is the same as leaving out the clear CSS property.

Let us mix the clear CSS property of the last div element to lớn left. Then the last div element should still float left, but stay clear of the first left floating div element. Here is how the code looks:


This is the first text
This is box 1
This is box 2
clear: left border: 2px solid red;">This is box 3
This is the last text
Here is what the code looks like when rendered in a browser:

*

Notice how the last div element still floats left, but stays under (clear of) the first left floating div element.


Next: CSS position
Tweet

Jakob obatambeienwasirherbal.com


Featured Videos
*
*
*
*
*
*
*
Sponsored Ads
*

Cthua thảm TOC
*
All Trails
*
Trail TOC
*
Page TOC
*
Previous
*
Next