美文网首页
CSS clear清除浮动

CSS clear清除浮动

作者: 音乐与咖啡Bean | 来源:发表于2022-04-01 11:50 被阅读0次

<html>

<head>

    <style type="text/css">

        body {

            margin: 0;

            padding: 0;

            width: 700px;

            height: 700px;

            border: solid 2px;

        }

        #one {

            width: 100px;

            height: 100px;

            border: solid 2px;

            background-color: gold;

            float: left;

        }

        #two {

            width: 300px;

            height: 300px;

            border: solid 3px blue;

            background-color: #bbb;

            float: right;

        }

        #three {

            width: 200px;

            height: 200px;

            border: solid 2px;

            background-color: blue;

            clear: both;

        }

        #tow_1 {

            width: 50px;

            height: 50px;

            border: solid 2px;

            background-color: black;

            float: right;

        }

        #tow_2 {

            width: 80px;

            height: 80px;

            border: solid 2px;

            background-color: red;

            clear: both;

        }

        .text_view {

            font-family: 'Courier New', Courier, monospace;

            font-size: 16px;

            vertical-align: middle;

        }

    </style>

</head>

<body>

    <div id="one">1</div>

    <div id="two">2

        <div id="tow_1">2.1</div>

        <div id="tow_2">2.2</div>

    </div>

    <div id="three">3</div>

    <p class="text_view">clear 是为了当前元素不受之前兄弟元素本身(同一个父元素,不包含其子元素等)的浮动的影响</p>

</body>

</html>

image

父级边框塌陷的问题
clear:
right:右侧不允许有浮动元素;
left:左侧不允许有浮动元素;
both:两侧不允许有浮动元素;
none:
解决塌陷问题方案:

方案一:增加父级元素的高度;

father{

border:1px #000 solid;
height:800px;

}
方案二:增加一个空的 div 标签,清除浮动。
<div class = "clear"></div>
<style>
.clear{
clear:both;
margin:0;
padding:0;
}
</style>
方案三:在 父级元素中 增加一个 overflow 属性。
overflow:hidden; /* 隐藏超出部分 /
overflow:scroll; /
滚动 */
方案四(推荐):父类 添加一个伪类:after。

father:after{

content:'';
display:block;
clear:both;

}

相关文章

  • CSS clear both清除浮动

    原文地址:CSS clear both清除浮动 DIV+CSS clear both清除产生浮动我们知道有时使用了...

  • 清除浮动

    一、清除浮动 or 闭合浮动 ? 清除浮动:清除对应的单词是 clear,对应CSS中的属性是 clear:lef...

  • 一篇文章带你了解CSS clear both清除浮动

    一、前言 CSS clear both清除产生浮动 ,使用了css float浮动会产生css浮动,这个时候就需要...

  • 11.22 前端学习

    清除浮动 clear:left清除左浮动clear:right清除右浮动clear:both清除对它影响最大的浮动...

  • 前端06

    清除浮动 clear:left清除左浮动clear:right清除右浮动clear:both清除对它影响最大的浮动...

  • 06 前端学习

    清除浮动 clear:left清除左浮动clear:right清除右浮动clear:both清除对它影响最大的浮动...

  • Test10

    引用文章: 那些年我们一起清除过的浮动 CSS浮动float详解 Clear Float CSS float浮动的...

  • css3复习

    清除浮动: 方法:clear清除浮动(添加空div法)在浮动元素下方添加空div,并给该元素写css样式: ...

  • CSS clear清除浮动

    父级边框塌陷的问题clear:right:右侧不允许有浮动元素;left:左侧不允许有浮动元素;both:两侧不允...

  • 前端面试积累2-清除浮动

    1.清除浮动的方法 方法一:对父级设置适合的CSS高度(不推荐) 方法二:clear:both 清除浮动 (常用)...

网友评论

      本文标题:CSS clear清除浮动

      本文链接:https://www.haomeiwen.com/subject/ntkajrtx.html