This exercise that demonstrates the use of the clear property to prevent a containing HTML element from collapsing due to nested floated elements.
Go to codepen.io and log into your account.
Next, go to the button icon at the far right top corner and select the "New pen" option.
In the HTML pane, add a main tag and give it class attribute with value "main".
Within the main tag, add two divs. Give each class attributes, one with value "col1" and the other "col2".
Within each div, type "lorem" and then hit the tab key to produce some placeholder text.
Between where the second div closes and the main tag closes add a break tag with a class attirbute and value "clearfloat". e.g
In the CSS pane create a "main" class selector. Set the background color to red and the min-height property to 50px. e.g. min-height: 50px;
Below the .main class selector, create a "col1" class selector. Set the background color to green, the width property to 500px, and the float property to left.
Below the .col1 class selector, create a "col2" class selector. Set the background color to blue, the width property to 500px, and the float property to left.
Note that, at this point, the floated divs are sticking out beyond the containing main tag. This is where the clear property comes in.
- Below the .col2 class selector, create a "clearfloat" class selector. Set the clear property to both. e.g. clear: both;
The red background of the main tag should now "clear" the floated elements.
When done, click the "Save" button.
Click on the "Settings" button. Add the following title "Clearing floats" in the title field. Also, add "html" and "css" tags in the tags field.
Send me your CodePen URL for this exercise via Insight.