Home  >  Article  >  Web Front-end  >  Problem with floating elements

Problem with floating elements

一个新手
一个新手Original
2017-10-16 10:41:091705browse

The floating of the child element will cause the parent element box to be unable to be opened, causing the parent element's style to be unable to be displayed. Here are several methods to clear the floating.

Original code:

<!DOCTYPE html><html><head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        #content{
            border: 1px red solid;
        }
        .fl{
            border: 1px blueviolet solid;
            height: 100px;
            width: 100px;
            float: left;
        }
        .fr{
            border: 1px green solid;
            height: 200px;
            width: 200px;
            float: right;
        }
    </style></head><body><p id="content">
    <p class="fl">内容一</p>
    <p class="fr">内容二</p></p></body></html>

is displayed as follows :

1. Set the height of the parent element:

height: 500px; /*设置父元素高度*/


<!DOCTYPE html><html><head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        #content{
            border: 1px red solid;
            height: 500px; /*设置父元素高度*/
        }
        .fl{
            border: 1px blueviolet solid;
            height: 100px;
            width: 100px;
            float: left;
        }
        .fr{
            border: 1px green solid;
            height: 200px;
            width: 200px;
            float: right;
        }
    </style></head><body><p id="content">
    <p class="fl">内容一</p>
    <p class="fr">内容二</p></p></body></html>

2. Absolute positioning of the parent element: position:absolute ;


<!DOCTYPE html><html><head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        #content{
            border: 1px red solid;
            position: absolute; /*父元素绝对定位*/        }
        .fl{
            border: 1px blueviolet solid;
            height: 100px;
            width: 100px;
            float: left;
        }
        .fr{
            border: 1px green solid;
            height: 200px;
            width: 200px;
            float: right;
        }
    </style></head><body><p id="content">
    <p class="fl">内容一</p>
    <p class="fr">内容二</p></p></body></html>

3. The parent element sets overflow: hidden


<!DOCTYPE html><html><head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        #content{
            border: 1px red solid;
            overflow: hidden; 
        }
        .fl{
            border: 1px blueviolet solid;
            height: 100px;
            width: 100px;
            float: left;
        }
        .fr{
            border: 1px green solid;
            height: 200px;
            width: 200px;
            float: right;
        }
    </style></head><body><p id="content">
    <p class="fl">内容一</p>
    <p class="fr">内容二</p></p></body></html>

4. The parent element sets float: float: left/right


##

<!DOCTYPE html><html><head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        #content{
            border: 1px red solid;
            float: left; 
        }
        .fl{
            border: 1px blueviolet solid;
            height: 100px;
            width: 100px;
            float: left;
        }
        .fr{
            border: 1px green solid;
            height: 200px;
            width: 200px;
            float: right;
        }
    </style></head><body><p id="content">
    <p class="fl">内容一</p>
    <p class="fr">内容二</p></p></body></html>

5. Add an empty box at the end of the child element and set the style to clear:both;

##

<!DOCTYPE html><html><head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        #content{
            border: 1px red solid;
        }
        .fl{
            border: 1px blueviolet solid;
            height: 100px;
            width: 100px;
            float: left;
        }
        .fr{
            border: 1px green solid;
            height: 200px;
            width: 200px;
            float: right;
        }
       .clear{
            clear: both;
        } 
    </style></head><body><p id="content">
    <p class="fl">内容一</p>
    <p class="fr">内容二</p>
    <p class="clear"></p></p></body></html>

6. Adding a pseudo-class to the parent element style is equivalent to adding an empty box at the end of the child element. The principle is similar to 5

##
<!DOCTYPE html><html><head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style type="text/css">
        #content{
            border: 1px red solid;
        }
        .fl{
            border: 1px blueviolet solid;
            height: 100px;
            width: 100px;
            float: left;
        }
        .fr{
            border: 1px green solid;
            height: 200px;
            width: 200px;
            float: right;
        }
        #content:after{
            content: &#39;&#39;;
            display: block;
            /!*height: 0;*!/
            clear: both;
        }
    </style></head><body><p id="content">
    <p class="fl">内容一</p>
    <p class="fr">内容二</p></p></body></html>

The above is the detailed content of Problem with floating elements. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn