1<!DOCTYPE html>
2<html>
3<body>
4
5<h2>Floating Images</h2>
6<p><strong>Float the image to the right:</strong></p>
7
8<p>
9<img src="smiley.gif" alt="Smiley face" style="float:right;width:42px;height:42px;">
10A paragraph with a floating image. A paragraph with a floating image. A paragraph with a floating image.
11</p>
12
13<p><strong>Float the image to the left:</strong></p>
14<p>
15<img src="smiley.gif" alt="Smiley face" style="float:left;width:42px;height:42px;">
16A paragraph with a floating image. A paragraph with a floating image. A paragraph with a floating image.
17</p>
18
19</body>
20</html>
21
1This is the code
2
3.float-wrapper::after {
4 content: "";
5 clear: both;
6 display: block;
7}
8---------------------------------------------------------------
9Explanation:
10
11.float-wrapper -> is some parent element that wraps the floating items
12
13example:
14<div class='float-wrapper'>
15 <div class='floating-item'> </div>
16 <div class='floating-item'> </div>
17 ....
18 </div>
19
20::after adds an element after the .float-wrapper, that
21has no content and clears floats from the both sides, making sure,
22other sections are not affected by floats
23
24
1/*
2 Nowadays with flex and grid, float has no chance to be putted inside your code
3 But just for the knowledge of using this property.
4 ===========================NOTICE===========================
5 you should use the 'clear' CSS property after the div that has the float prop
6 ===========================NOTICE===========================
7*/
8.div-1{
9 float: left;
10 width: 50%;
11 background-color: #f00;
12}
13/*
14 after applying float i used clear: both
15 try this code and remove the clear property and see what will be happened
16*/
17.clear-float{
18 clear: both;
19}
20.div-2{
21 float: left;
22 width: 50%;
23 background-color: #00f;
24}
25.clear-float{
26 clear: both;
27}
28/* div-1 and div-2 will now be at the side of each other*/
29/*
30 ==========HTML Layout==========
31 <div class="div-1">DIV 1</div>
32 <div class="clear-float"></div>
33 <div class="div-2">DIV 2</div>
34 <div class="clear-float"></div>
35*/