[CSS3]Clearfix

本文详细介绍了如何使用CSS实现元素的浮动布局,并通过clearfix解决浮动带来的布局问题。提供了具体的HTML和CSS代码示例,展示了如何使导航栏项水平排列及在内容区域中正确布局侧边栏和主要内容。
<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="index.css" />
</head>
<div class="db group"> <!-- 在含有float的容器中家group class-->
    <div id="l" style="width:100px; height: 100px; background: red;"></div>
    <p>to left</p>
</div>
<div id="c">
    <div  style="width:100px; height: 100px; background: blue;"></div>
    <p>Not float</p>
</div>
</html>
#l{
    float:left;
}
#c{
    border: 1px solid yellow;
}
.db{
    border: 1px solid aqua;
}

.group:before,
.group:after{
    content: "";
    display: table;
}
.group:after{
    clear: both;
}
.group{
    zoom: 1;
}

 

-----------------------Ex-----------------------

<!doctype html>
<html lang="en">
  <head>
    <title>Sven's Snowshoe Emporium</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <nav>
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </nav>
    <section class="content home group">
      <aside>
        <p>Did you know that Sven's Snowshoe Emporium produces the highest quality snowshoes in North America? <a href="#">Find out more</a>.</p>
      </aside>
      <article>
        <h3>New Fall Styles</h3>
        <p>Be the first at your resort to sport the hot new tennis-themed snow kicks, now available in the <a href="#">store</a>.</p>
      </article>
    </section>
  </body>
</html>

 

nav li {
  float: left;
}
aside {
  float: right;
  width: 120px;
}
article {
  float: left;
  width: 120px;
}

/* clearfix */
  
.group:before,
.group:after {
  content: "";
  display: table;
}
.group:after {
  clear: both;
}
.group {
  zoom: 1;
}

 

转载于:https://www.cnblogs.com/Answer1215/p/3898503.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值