美文网首页
Css - float布局

Css - float布局

作者: ElricTang | 来源:发表于2019-10-20 17:26 被阅读0次

float CSS属性指定一个元素应沿其容器的左侧或右侧放置,允许文本和内联元素环绕它。该元素从网页的正常流动(文档流)中移除,尽管仍然保持部分的流动性(与绝对定位 相反)。

float取值 描述
left 往左浮动
right 往右浮动
none 不浮动
inline-start 表明元素必须浮动在其所在块容器的开始一侧
inline-end 表明元素必须浮动在其所在块容器的结束一侧
  1. 浮动布局下会把行内元素转化为块级元素
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .box{
                width:400px;
                height:200px;
                border:1px solid black;
            }
            span{
                width:50px;
                height:50px;
                background-color: green;
                float:right;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <span>hello world</span>
        </div>
    </body>
</html>
  • 本来不能设置宽高的行内元素span在用了浮动后可以设置宽高了。
  1. 浮动会将元素脱离文档流
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .box{
                border:1px solid black;
            }
            span{
                width:50px;
                height:50px;
                background-color: green;
                float:left;
            }
            p{
                background-color: red;
                line-height: 100px;
                margin:0;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <span>我向左浮动了</span>
            <p>我没有浮动</p>
        </div>
    </body>
</html>
  • span向左浮动,p没有浮动。
  • span脱离了文档流,所以后面的p摆放时忽略了span,但是文本还是会环绕在span外面。

如何清除浮动?

场景:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .box{
                border:1px solid yellow;
                width:300px;
            }
            .box div{
                width:50px;
                height:50px;
            }
            .item1{
                background-color: red;
                float: left;
            }
            .item2{
                background-color: green;
                float: right;
            }
            p{
                background-color: blue;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="item1"></div>
            <div class="item2"></div>
        </div>
        <p>hello world</p>
    </body>
</html>

因为浮动脱离文档流,父容器的高度塌陷。

  1. clear:both清除所有浮动(注意需要block元素clear:both才有效)
  • 伪元素+clear:both
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .box{
                border:1px solid yellow;
                width:300px;
            }
            .box div{
                width:50px;
                height:50px;
            }
            .box::after{
                content: '';
                display:block;
                clear: both;
                height: 0;
                width:0;
            }
            .item1{
                background-color: red;
                float: left;
            }
            .item2{
                background-color: green;
                float: right;
            }
            p{
                background-color: blue;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="item1"></div>
            <div class="item2"></div>
        </div>
        <p>hello world</p>
    </body>
</html>
  • 额外空标签+clear:both
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .box{
                border:1px solid yellow;
                width:300px;
            }
            .box div{
                width:50px;
                height:50px;
            }
            .item1{
                background-color: red;
                float: left;
            }
            .item2{
                background-color: green;
                float: right;
            }
            p{
                background-color: blue;
            }
            div .clear{
                clear:both;
                width:0;
                height:0;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="item1"></div>
            <div class="item2"></div>
            <div class="clear"></div>
        </div>
        <p>hello world</p>
    </body>
</html>
  1. 父容器overflow:hidden,触发BFC
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style>
            .box{
                border:1px solid yellow;
                width:300px;
                overflow: hidden;
            }
            .box div{
                width:50px;
                height:50px;
            }
            .item1{
                background-color: red;
                float: left;
            }
            .item2{
                background-color: green;
                float: right;
            }
            p{
                background-color: blue;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="item1"></div>
            <div class="item2"></div>
        </div>
        <p>hello world</p>
    </body>
</html>

清除浮动后效果:

相关文章

  • Css float属性的一些特点

    Css float属性的一些特点 css布局中float布局是常用的布局方式,用于实现横向多列布局。这个时候我们就...

  • CSS基础布局

    CSS布局 盒模型 Flexbox 使用float布局 inline-block 布局 响应式设计和布局 CSS面...

  • CSS布局属性

    CSS布局属性记录 display + float + position flex布局 总模块

  • CSS布局

    1.学习CSS布局2.Float

  • 浅析 CSS 布局与定位

    浅析 CSS 布局与定位 较多内容参考:CSS布局与定位入门 一、左右布局 float + clearfix块级元...

  • css布局

    CSS布局 作业1 作业1使用float布局作业1使用flex布局 作业2 作业2使用float布局作业2使用fl...

  • CSS布局

    CSS的常见布局 CSS常见布局使用display属性(文档流)+position属性(定位)+float属性(浮...

  • Css - float布局

    float CSS属性指定一个元素应沿其容器的左侧或右侧放置,允许文本和内联元素环绕它。该元素从网页的正常流动(文...

  • css布局和居中

    本文主要介绍了css常用的布局,居中等其他小技巧。 css布局 左右布局 利用float实现左右布局 左右模块设置...

  • CSS---浮动

    1. 浮动(float) 1.1 CSS 布局的三种机制 网页布局的核心——就是用 CSS 来摆放盒子。 CSS ...

网友评论

      本文标题:Css - float布局

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