margin
2016-08-24 02:16
148 查看
margin呈现于边框外侧,padding呈现于边框内侧。
margin:0 auto即可实现内容的居中,为了在各大浏览器的支持,最好在父级写上text-align:center;
(1)margin-top 无效
两个div,其中一个浮动,另一个没有浮动
<div class="box1">float:left</div>
<div class="box2">clear:both; margin-top:20px;</div>
此时的margin-top没有起任何作用;
网上能找到的两种比较靠谱的解释:
1:“在CSS2.1中,水平的margin不会被折叠;垂直margin可能在一些盒模型中被折叠…”
2:当第一个层浮动,而第二个没浮动层的margin会被压缩。
解决方案:
一起浮动,或者一起不浮动;
1.给box2添加float;
2.给box1和box2之间添加一层”<divstyle=“clear:both;"></div>",即给它加一个正常文档流的盒子;
(2)子元素设置margin-top作用于父容器
<div class="box">
<div class="box1">
</div>
</div>
.box{width: 300px;height: 100px;background: #fb3;}
.box1{width: 250px;height: 50px;background: red;margin-top: 30px;}
父 元素随子元素的margin一起往下移,
解决方案:
1.给父容器加overflow-hidden;2.父容器box加border除none以外的属性 3.用父容器box的padding-top代替margin-top。
margin:0 auto即可实现内容的居中,为了在各大浏览器的支持,最好在父级写上text-align:center;
(1)margin-top 无效
两个div,其中一个浮动,另一个没有浮动
<div class="box1">float:left</div>
<div class="box2">clear:both; margin-top:20px;</div>
此时的margin-top没有起任何作用;
网上能找到的两种比较靠谱的解释:
1:“在CSS2.1中,水平的margin不会被折叠;垂直margin可能在一些盒模型中被折叠…”
2:当第一个层浮动,而第二个没浮动层的margin会被压缩。
解决方案:
一起浮动,或者一起不浮动;
1.给box2添加float;
2.给box1和box2之间添加一层”<divstyle=“clear:both;"></div>",即给它加一个正常文档流的盒子;
(2)子元素设置margin-top作用于父容器
<div class="box">
<div class="box1">
</div>
</div>
.box{width: 300px;height: 100px;background: #fb3;}
.box1{width: 250px;height: 50px;background: red;margin-top: 30px;}
父 元素随子元素的margin一起往下移,
解决方案:
1.给父容器加overflow-hidden;2.父容器box加border除none以外的属性 3.用父容器box的padding-top代替margin-top。
相关文章推荐
- Web布局连载——两栏固定布局(五)
- [div+css]晒晒最新制作专题推广页模板
- 盒子模型
- CSS选择器
- CSS样式优先权
- [css] line-height 百分比单位和数值单位的区别
- css如何搞正方形
- Apple官网研究之使用Justify布局导航
- 关于前端的思考与感悟
- rem : web app适配的秘密武器
- jquery高级应用之Deferred对象
- 移动端点击事件全攻略,这里的坑你知多少?
- $q 实例分析 Angular 中的 Promise
- 浏览器中唤起native app || 跳转到应用商城下载
- Angular directive 实例详解
- 关于浮动与清除浮动,你应该知道的
- 数组reduce方法的高级技巧
- 设计更快的网页(三):字体和 CSS 调整