美文网首页
5、浮动属性

5、浮动属性

作者: calvinbj | 来源:发表于2019-02-24 17:10 被阅读0次

正常div块级元素的排列是每个块级元素占一行。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    .box1{width: 100px;height: 100px;background-color: #f00;}
    .box2{width: 200px;height: 200px;background-color: #0f0;}
    .box3{width: 250px;height: 250px;background: #00f;}
    </style>
</head>
<body>
    <div class="box1">
    </div>
    <div class="box2">
    </div>
    <div class="box3">
    </div>
</body>
</html>

三个块级元素设置为浮动属性:float

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    .box1{width: 100px;height: 100px;background-color: #f00;float: left;}
    .box2{width: 300px;height: 300px;background-color: #0f0;float: left;}
    .box3{width: 500px;height: 500px;background: #00f;float: left;}
    </style>
</head>
<body>
    <div class="box1">
    </div>
    <div class="box2">
    </div>
    <div class="box3">
    </div>
</body>
</html>



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    .box{width: 960px;border: 5px #000 solid;}
        .box_l{width: 480px;height: 240px;background-color: #f00;float: left;}
        .box_c,.box_r{width: 240px;height: 240px;float: left;}
        .box_c{background: #f1f1f1;}
        .box_r{background: #fbfbfb url(images/1.jpg) no-repeat right bottom;}
    </style>
</head>
<body>
    <div class="box">
        <div class="box_l"></div>
        <div class="box_c"></div>
        <div class="box_r"></div>
    </div>
</body>
</html>

上面这种情况痴线了高度塌陷。
第一种方法:给父元素添加声明overflow:hidden;
<style type="text/css">
    .box{width: 960px;border: 5px #000 solid;overflow: hidden;}
        .box_l{width: 480px;height: 240px;background-color: #f00;float: left;}
        .box_c,.box_r{width: 240px;height: 240px;float: left;}
        .box_c{background: #f1f1f1;}
        .box_r{background: #fbfbfb url(images/1.jpg) no-repeat right bottom;}
    </style>

第二种方法:在浮动的元素下方添加空div,并给该元素添加声明:clear:both;height:0;overflow:hidden(或font-size:0;)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
    .box{width: 960px;border: 5px #000 solid;}
        .box_l{width: 480px;height: 240px;background-color: #f00;float: left;}
        .box_c,.box_r{width: 240px;height: 240px;float: left;}
        .box_c{background: #f1f1f1;}
        .box_r{background: #fbfbfb url(images/1.jpg) no-repeat right bottom;}
        .clean{clear: both;height: 0;font-size: 0;}
    </style>
</head>
<body>
    <div class="box">
        <div class="box_l"></div>
        <div class="box_c"></div>
        <div class="box_r"></div>
        <div class="clean"></div>
    </div>
</body>
</html>

上面两种方法最终解决“高度塌陷”后的效果图如下:

相关文章

  • 5、浮动属性

    三个块级元素设置为浮动属性:float 第二种方法:在浮动的元素下方添加空div,并给该元素添加声明:clear:...

  • CSS属性

    1、标准流和浮动 2、display属性 3、float属性 4、清楚浮动 5、文字环绕效果 6、定位 7、盒子模型

  • day23-markdown总结

    1.标准流和display属性 2.浮动 3.浮动(文字环绕) 4.清除浮动 5.定位 6.盒子模型 7.其他常用属性

  • 2018-10-31 二阶段day3

    1.标准流和浮动 2.display属性 3.float属性 4.消除浮动 5.文字效果 6.定位 盒子模型

  • 二阶段day3

    1标准流和浮动 2.display属性 3.float属性 4.清除浮动 5.文字环绕效果 6.定位 7.盒子模型

  • 03-CSS核心属性

    学习目标 1、css浮动属性详解2、css文本属性3、css列表属性4、css背景属性5、css边框属性 一、cs...

  • 前端浮动与定位知识点

    浮动与定位 元素的浮动 元素的浮动属性float 什么是浮动? 元素的浮动是指设置了浮动属性的元素会脱离标准文档流...

  • day4

    A.今天学了什么 1 轮廓属性 2 透明 3 css样式的继承 4 盒子浮动 5 清除浮动 6 定位:positi...

  • 浮动属性

    普通流(normal flow) 说明前面我们提过,网页布局的核心,就是用CSS来摆放盒子位置。如何把盒子摆放到合...

  • 浮动属性

    浮动定位 将元素排除在普通流之外,即元素将脱离标准文档流 元素将不在页面占用空间 将浮动元素放置在包含框的左边或者...

网友评论

      本文标题:5、浮动属性

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