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

Several solutions for forgetting the MySQL password

Solution 1 Completely uninstall and delete all da...

Solution to MySQL error code 1862 your password has expired

The blogger hasn't used MySQL for a month or ...

Solution to the Docker container cannot be stopped and deleted

Find the running container id docker ps Find the ...

The implementation process of long pressing to identify QR code in WeChat applet

Preface We all know that the QR codes in official...

Thoughts on copy_{to, from}_user() in the Linux kernel

Table of contents 1. What is copy_{to,from}_user(...

A very detailed explanation of Linux C++ multi-thread synchronization

Table of contents 1. Mutex 1. Initialization of m...

Several ways to implement inheritance in JavaScript

Table of contents Structural inheritance (impleme...

In-depth explanation of various binary object relationships in JavaScript

Table of contents Preface Relationships between v...

The use of mysql unique key in query and related issues

1. Create table statement: CREATE TABLE `employee...

How to understand semantic HTML structure

I believe everyone knows HTML and CSS, knows the ...

HTML form tag tutorial (5): text field tag

<br />This tag is used to create a multi-lin...

Linux Operation and Maintenance Basic System Disk Management Tutorial

1. Disk partition: 2. fdisk partition If the disk...

A brief discussion on Linux signal mechanism

Table of contents 1. Signal List 1.1. Real-time s...