浮动display和float

7 篇文章 0 订阅

1、标准文档流

!

块级元素:独占一行 h1-h6 p div 列表…

行内元素:不独占一行 span a img strong

注:行内元素可以被包含在 块级元素中,反之,则不可以

2、display(重要)

  1. block:块元素
  2. inline:行内元素
  3. inline-block:是块元素,但是可以内联,在一行(更多时候用float)
  4. none:消失
<!DOCTYPE html>
<html lang=""en"">
<head>
    <meta charset=""UTF-8"">
    <title>Title</title>
    <style>
        /*
        block           块元素
        inline          行内元素
        inline-block    是块元素,但是可以内联,在一行
        */
        div{
            width: 100px;
            height: 100px;
            border: 1px solid green;
            display: inline;
        }
        span{
            width: 100px;
            height: 100px;
            border: 1px solid green;
            display: inline-block;
        }
    </style>
</head>
<body>

<div>div块元素</div>
<span>span行内元素</span>

</body>
</html>

这个也是一种实现行内元素排列的方式,当时我们很多情况都是用float

3、float

1、左右浮动 float

<!DOCTYPE html>
<html lang=""en"">
<head>
    <meta charset=""UTF-8"">
    <title>Title</title>
    <link rel=""stylesheet"" href=""css/style.css"" type=""text/css"">
</head>
<body>
<div id=""father"">
    <div class=""layer01""><img src=""images/1.jpg"" alt=""""></div>
    <div class=""layer02""><img src=""images/2.jpg"" alt=""""></div>
    <div class=""layer03""><img src=""images/3.jpg"" alt=""""></div>
    <div class=""layer04"">
        浮动的盒子可以向左浮动,也可以向右浮动,知道它的外边缘碰到包含或另一个浮动盒子为止
    </div>
    <div class=""clear""></div>
</div>
</body>
</html>

div{
    margin: 10px;
    padding: 5px;
}
#father{
    border: 1px #000 solid;
    height: 800px;
}

#father:after{
    content: '';
    display: block;
    clear: both;
}

.layer01{
    border: 1px #F00 dashed;
    display: inline-block;
    float: left;/*向左浮动*/
}
.layer02{
    border: 1px #00F dashed;
    display: inline-block;
    float: left;
}
.layer03{
    border: 1px #060 dashed;
    display: inline-block;
    float: right;
}
/*
clear:right;右侧不允许有浮动元素
clear:left; 左侧不允许有浮动元素
clear:both; 两侧不允许有浮动元素
clear:none;
 */
.layer04{
    border: 1px #666 dashed;
    font-size: 12px;
    line-height: 23px;
    display: inline-block;
    float: right;
    clear: left;
}
.clear{
    clear: both;
    margin: 0;
    padding: 0;
}

4、父级边框塌陷问题(以float为例)

  1. 增加父级元素的高度
  #father{
        border: 1px #000 solid;
        height: 500px;
    }

  1. 增加一个空的div标签,设置属性
<div class=""clear""></div>

.clear{
    clear: both;
    margin: 0;
    padding: 0;
}

  1. 父级元素增加属性
overflow: hidden; //溢出时,隐藏;边框大小根据内容适应
    overflow: none;

  1. 父类增加一个伪类
#father:after{
    content: '';
    display: block;
    clear: both;
}

小结:**

  1. 浮动元后面增加空div

    简单,代码中尽量避免空div

  2. 设置父元素的高度

    简单,元素假设有了固定的高度,就会被限制

  3. overflow

    简单,下拉的一些场景避免使用

  4. 父类添加一个伪类:after

    写法稍微复杂一点,但是没有副作用,**推荐使用!

5、overflow

<!DOCTYPE html>
<html lang=""en"">
<head>
    <meta charset=""UTF-8"">
    <title>Title</title>

    <style>
        #content{
            width: 200px;
            height: 150px;
            overflow: scroll;
        }
    </style>
</head>
<body>

<div id=""content"">
    <img src=""images/1.png"" alt="""">
    <p>
        干嘛听苦情歌以为多浪漫
        再浪漫都被拆散
        说要平平淡淡 长路漫漫 一起再共患难
        高脚杯 在陪伴以为决定 多果断
        在青春 快过半的阶段
        失去了对爱的基本判断
    </p>
</div>
</body>
</html>



6、display与float对比

  • display:方向不可以控制
  • float:浮动起来会脱离标准文档流,所以要解决父级边框塌陷的问题
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值