• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
邹天得
博客园    首页    新随笔    联系   管理    订阅  订阅
css清除浮动的一些做法

1.代码如下:

<div class="container">
<div class="sidebar">this is the side bar.</div>
<div class="content">this is the main content.</div>
</div>

<!--css-->

.container{width:500px;background:grey;border:5px;padding:20px}
.sidebar{float:left;background:red;height:20px;width:200px;}
.content{float:right;background:blue;height:30px;width:200px;}

效果截图如下:

2.为了将盒子.container撑开,有下面三种方式:

2.1 overflow:hidden;

<div class="container">
<div class="sidebar">this is the side bar.</div>
<div class="content">this is the main content.</div>
</div>

<!--css-->

.container{width:500px;background:grey;border:5px;padding:20px;overflow:hidden;}
.sidebar{float:left;background:red;height:20px;width:200px;}
.content{float:right;background:blue;height:30px;width:200px;}

效果截图如下:

2.2 clear:both;

<div class="container">
<div class="sidebar">this is the side bar.</div>
<div class="content">this is the main content.</div>
<div class="clear"></div>

<!--css-->

.container{width:500px;background:grey;border:5px;padding:20px;}
.sidebar{float:left;background:red;height:20px;width:200px;}
.content{float:right;background:blue;height:30px;width:200px;}
.clear{clear:both;}

 2.2before after伪元素

<div class="container">
<div class="sidebar">this is the side bar.</div>
<div class="content">this is the main content.</div>

<!--css-->

.container{width:500px;background:grey;border:5px;padding:20px;}
.container{
*zoome:1
}
.container:before,
.container:after {
display:table;
content:"";
}
.container:after{
clear:both;
}

.sidebar{float:left;background:red;height:20px;width:200px;}
.content{float:right;background:blue;height:30px;width:200px;}

 

posted on 2017-07-01 19:00  邹天得  阅读(154)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3