前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >2种清除浮动方式(超详解)

2种清除浮动方式(超详解)

作者头像
贵哥的编程之路
发布2020-11-03 14:25:27
2310
发布2020-11-03 14:25:27
举报

1.清除浮动方式一

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        *{padding: 0px;margin: 0px;}
        .box1
        {
            background-color: red;
        }
        .box2
        {
            background-color: blue;
        }
        .box1 p
        {
            width: 100px;
            background-color: yellow;
        }
        .box2 p
        {
            width: 100px;
            background-color: green;
        }
        p
        {
            float: left;
        }
    </style>
</head>
<body>
<div class="box1">
    <p>我是文字1</p>
    <p>我是文字1</p>
    <p>我是文字1</p>
</div>

<div class="box2">
    <p>我是文字2</p>
    <p>我是文字2</p>
    <p>我是文字2</p>
</div>
</body>
</html>

效果:

在这里插入图片描述
在这里插入图片描述

怎么解决这里的浮动呢? 给第一个div设置高度.

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>65-清除浮动方式一</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .box1{
            height: 20px;
            background-color: red;
        }
        .box2{
            background-color: green;
        }
        .box1 p{
            width: 100px;
            background-color: blue;
        }
        .box2 p{
            width: 100px;
            background-color: yellow;
        }
        p{
            float: left;
        }
    </style>
</head>
<body>
<div class="box1">
    <p>我是文字1</p>
    <p>我是文字1</p>
    <p>我是文字1</p>
</div>

<div class="box2">
    <p>我是文字2</p>
    <p>我是文字2</p>
    <p>我是文字2</p>
</div>
</body>
</html>

注意一下,是给第一个.div设置高度. 效果:

在这里插入图片描述
在这里插入图片描述

2.清除浮动的第二种方式

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        *{padding: 0px;margin: 0px;}
        .box1
        {
            background-color: red;
        }
        .box2
        {
            background-color: blue;
        }
        .box1 p
        {
            width: 100px;
            background-color: yellow;
        }
        .box2 p
        {
            width: 100px;
            background-color: green;
        }
        p
        {
            float: left;
        }
    </style>
</head>
<body>
<div class="box1">
    <p>我是文字1</p>
    <p>我是文字1</p>
    <p>我是文字1</p>
</div>

<div class="box2">
    <p>我是文字2</p>
    <p>我是文字2</p>
    <p>我是文字2</p>
</div>
</body>
</html>

效果:

在这里插入图片描述
在这里插入图片描述

怎么清楚浮动? 在第二个div中添加clear:both;属性

代码语言:javascript
复制
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style type="text/css">
        *{padding: 0px;margin: 0px;}
        .box1
        {
            background-color: red;
        }
        .box2
        {
            clear: both;
            background-color: blue;
        }
        .box1 p
        {
            width: 100px;
            background-color: yellow;
        }
        .box2 p
        {
            width: 100px;
            background-color: green;
        }
        p
        {
            float: left;
        }
    </style>
</head>
<body>
<div class="box1">
    <p>我是文字1</p>
    <p>我是文字1</p>
    <p>我是文字1</p>
</div>

<div class="box2">
    <p>我是文字2</p>
    <p>我是文字2</p>
    <p>我是文字2</p>
</div>
</body>
</html>
在这里插入图片描述
在这里插入图片描述
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-10-31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.清除浮动方式一
  • 2.清除浮动的第二种方式
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档