[css] How to remove wrapping with float “clear” and “clearfix”

[css] How to remove wrapping with float “clear” and “clearfix” css

Float” is often used to align elements horizontally.
Have you ever had a problem with other elements flowing in and out of your site?

For example, something like this.

width:100px;
height:100px;
margin:10px;
background:#f00;
float:left;
The element on the right has the css set as above.
The text is currently wrapped around the bottom of this red square.

When you want to express something like this, it’s no problem, but when it looks like the sample shown below, you’re in trouble.

The CSS for the red square is the same. This time, I put float:right; in the div that encloses this text.
(The width of the text is set by subtracting the width of the red square and one character from the entire width on the right.)

This part is written outside of the previous div.

In the above case, the yellow marker part is shown below the div with the red square and text, but due to the continuous wrapping, it is shown in an unintended position.

The source looks like this.

There are two ways to solve this kind of problem.
In this article, we will introduce these two methods.

Use [css] clear.

You can remove the wrapping by setting clear:both; on the element you want to remove.

The CSS for the red square is the same. The float:right; is placed in the div that encloses this text.
(The width of the text is set by subtracting the width of the red square and one character from the entire width on the right.)

Set clear:both; to this part.

It is useful to have this setting in your external css.

If this has been written, you can easily set it to clear using class=”clear”.

Use clearfix to remove wrapping.

The clearfix method is different from the clear method in that it confines the wrapped element to a single box so that the wrapping does not affect other elements outside the box.
Let’s take the sample I mentioned above as an example.

The CSS for the red square is the same. The float:right; is placed in the div that encloses this text.
(The width of the text is set by subtracting the width of the red square and one character from the entire width on the right.)

Elements outside the clearfix will not be affected by the wrapping.

For more information about the contents of clearfix (css), please refer to here.

[css] 2021, this is the latest clearfix setting!
clearfix is often used as a method to remove float wrapping, but at one time it ...