HTML+CSS(浮动与清除浮动)

HTML: 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <link rel="stylesheet" href="css/lian.css">
        <link rel="stylesheet" href="css/public.css">
    </head>
    <body>
        <!-- float -->
        <main class="pageMain">
            <div class="chengse">橙色</div>
            <div class="qingse">青色</div>
        </main>
        <!-- float end -->
    </body>
</html>

CSS:

.pageMain{
    width: 600px;
    height: 400px;
    border: 10px #000 solid;
    margin-left: auto;
    margin-right: auto;
}
.chengse{
    background: #f60;
    width: 400px;
    height: 300px;
}
.qingse{
    background: #0ff;
    width: 200px;
    height: 100px;
}

未浮动的效果图:

 加入浮动之后:

CSS:

.chengse{
    background: #f60;
    width: 400px;
    height: 300px;
    float: left;
}
.qingse{
    background: #0ff;
    width: 200px;
    height: 100px;
    float: right;
}

效果图:

加入清除浮动之后 :

HTML:

<main class="pageMain">
            <div class="chengse">橙色</div>
            <div class="qingse">青色</div>
            <div class="clear"></div><!-- 清除浮动 -->
        </main>

CSS:

.clear{
    clear: both;
}

效果图:

但如若单独清除浮动:

CSS:

.chengse{
    background: #f60;
    width: 400px;
    height: 300px;
    float: left;
    clear: right;
}
.qingse{
    background: #0ff;
    width: 200px;
    height: 100px;
    float: right;
    clear: left;
}

效果图:

若只浮动一个: 

CSS:

.chengse{
    background: #f60;
    width: 400px;
    height: 300px;
    float: left;
    clear:right;
}
.qingse{
    background: #0ff;
    width: 200px;
    height: 100px;
}

效果图:

 注意:浮动不占位,当橙色左浮动之后,橙色不占位,青色就会自动在左上角,以至于被橙色覆盖。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值