前端3+1-8

html

同day4一样

css

题目:清除浮动的方式有哪些,原理是啥,有啥优缺点
答案:
ps:为啥要清除浮动:子元素浮动后,脱离文档流,不再被父元素包裹,故父元素高度坍塌,无法由子元素撑开
1、手动给父元素要给高度;兼容性好,但自适应差;
2、触发父元素BFC,让内层子元素不影响外层父元素样式
给父元素:overflow: auto;
可能会出现滚动条
2、新增一个空元素清除浮动
新增了冗余的元素

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      padding: 10px 10px;
      background-color: yellowgreen;
    }

    .item {
      float: left;
      width: 10px;
      height: 10px;
      background-color: blue;
      list-style: none;
    }
    // 重点
    .clear-item {
      clear: both;
    }
  </style>
</head>

<body>
  <ul class="box">
    <li class="item"></li>
    <li class="item"></li>
    <li class="item"></li>
    <!-- 重点 -->
    <div class="clear-item"></div>
  </ul>
</body>

</html>

3、使用after伪元素清除浮动
低版本IE不兼容

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .box {
      padding: 10px 10px;
      background-color: yellowgreen;
    }

    .item {
      float: left;
      width: 10px;
      height: 10px;
      background-color: blue;
      list-style: none;
    }
    // 重点
    .box::after {
      content: '';
      display: block;
      clear: both;
    }
  </style>
</head>

<body>
  <ul class="box">
    <li class="item"></li>
    <li class="item"></li>
    <li class="item"></li>
  </ul>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值