Detailed explanation of the problems and solutions caused by floating elements

Detailed explanation of the problems and solutions caused by floating elements

1. Problem

  • Multiple floating elements cannot expand the width of the parent element, and the height of the parent element may become 0.
  • If a floated element is followed by a non-floated element, the non-floated element will float immediately after it.
  • If there are elements of the same level in front of the floating element that are not floating, it will affect the page structure.

2. Solution

1. clear: both

Add an element with the attribute clear:both; after the last floated element.

<style>
div.parent>div.fl{
    float: left;
    width: 200px;
    height: 200px;
    margin-right: 20px;
    border: 1px solid red;
}
.clear{
    clear: both;
}
</style>
<div class="parent">
    <div class="fl">1</div>
    <div class="fl">2</div>
    <div class="fl">3</div>
    <div class="fl">4</div>
    <div class="clear">5</div>
</div>

Add the :after pseudo-element with the clear:both; attribute to the parent element.

<style>
div.parent>div.fl{
    float: left;
    width: 200px;
    height: 200px;
    margin-right: 20px;
    border: 1px solid red;
}
.clear:after{
    content: '';
    display: block;
    clear: both;
}
</style>
<div class="parent clear">
    <div class="fl">1</div>
    <div class="fl">2</div>
    <div class="fl">3</div>
    <div class="fl">4</div>
</div>

Note: Pseudo-elements are inline by default, so when using pseudo-elements, you must set the attribute display: block;.

2. overflow:auto / overflow:hidden

<style>
div.parent{
    overflow:auto;
    /*overflow: hidden; also works*/
}
div.parent>div.fl{
    float: left;
    width: 200px;
    height: 200px;
    margin-right: 20px;
    border: 1px solid red;
}
</style>
<div class="parent">
    <div class="fl">1</div>
    <div class="fl">2</div>
    <div class="fl">3</div>
    <div class="fl">4</div>
</div>

3. Floating parent elements

<style>
div.parent{
    float: left;
}
div.parent>div.fl{
    float: left;
    width: 200px;
    height: 200px;
    margin-right: 20px;
    border: 1px solid red;
}
</style>
<div class="parent">
    <div class="fl">1</div>
    <div class="fl">2</div>
    <div class="fl">3</div>
    <div class="fl">4</div>
</div>

Note: This method is generally not used, as it will cause layout problems for the parent element.

This concludes this article on the problems and solutions caused by floating elements. For more information about problems caused by floating elements, please search 123WORDPRESS.COM’s previous articles or continue to browse the related articles below. We hope that everyone will support 123WORDPRESS.COM in the future!

<<:  Three BOM objects in JavaScript

>>:  VMware Workstation virtual machine installation operation method

Recommend

Detailed steps to build the TypeScript environment and deploy it to VSCode

Table of contents TypeScript environment construc...

Solve the problem that the docker container cannot ping the external network

Today, when I was building a redis environment in...

Examples of using MySQL covering indexes

What is a covering index? Creating an index that ...

Installation tutorial of the latest stable version of MySQL 5.7.17 under Linux

Install the latest stable version of MySQL on Lin...

MySQL 5.7.18 Archive compressed version installation tutorial

This article shares the specific method of instal...

Tutorial on installing and using virtualenv in Deepin

virtualenv is a tool for creating isolated Python...

Mysql sorting to get ranking example code

The code looks like this: SELECT @i:=@i+1 rowNum,...

JS uses clip-path to implement dynamic area clipping function

background Today, I was browsing CodePen and saw ...

CentOS 7 cannot access the Internet after modifying the network card

Ping www.baidu.com unknown domain name Modify the...

Tomcat configuration and how to start it in Eclipse

Table of contents How to install and configure To...

mysql8.0 windows x64 zip package installation and configuration tutorial

MySQL 8 Windows version zip installation steps (d...

MySQL Failover Notes: Application-Aware Design Detailed Explanation

1. Introduction As we all know, in the applicatio...