如何解决子div设置margin-top时影响父div的margin-top
来源:互联网 发布:淘宝旗舰店的手机 编辑:程序博客网 时间:2024/05/29 16:46
测试代码如下:
<!DOCTYPE html>
<html><head>
<title></title>
<style type="text/css">
.container{width: 50%;margin: 0px auto;background-color: #919191;overflow:hidden;}
/*.container{width: 50%;margin: 0px auto;background-color: #919191;position: absolute;}*/
/*.container{width: 50%;margin: 0px auto;background-color: #919191;float: left;}*/
.sub-container{width: 100px;background-color: #ddd;margin: 0px auto;margin-top: 30px;}</style>
</head>
<body>
<div class="container">
<div class="sub-container">
sub-container
</div>
<div class="sub-container">
sub-container
sdkfj<br><br>
</div>
</div>
</body>
</html>
说明:有三种方式解决子div设置margin-top时影响父div的margin-top
1、使用overflow:hidden 此时不要指定父div的height, 不然会将子div超出父div的内容隐藏。这个用法非常好!
.container{width: 50%;margin: 0px auto;background-color: #919191;overflow:hidden;}
2、使用float
.container{width: 50%;margin: 0px auto;background-color: #919191;float: left;}
3、使用position:absolute
.container{width: 50%;margin: 0px auto;background-color: #919191;position: absolute;}
0 0
- 如何解决子div设置margin-top时影响父div的margin-top
- 子DIV设置margin-top影响父DIV位置
- 子DIV块中设置margin-top时影响父DIV块位置的解决办法
- 子DIV块中设置margin-top时影响父DIV块位置的解决办法
- 子DIV块中设置margin-top时影响父DIV块位置的解决办法
- 子DIV块中设置margin-top时影响父DIV块位置的解决办法
- CSS常见问题解决001——子div的margin-top影响父div的位置
- 子DIV设置margin-top影响父DIV位置的解决办法
- 子div块中设置margin-top时影响父div块位置的解决办法及其原因
- 子div块中设置margin-top时影响父div块位置的解决办法及其原因
- 解决:子div设置margin-top使得父div也跟着向下移动
- 子div 的margin-top不生效
- css-子div设置margin-top后,父div与子div一起下移
- 子div的margin影响父div
- div margin-top不起作用
- margin-top在什么情况下会影响父div
- 如何消除子容器的margin-top给父容器造成的合并margin影响。
- 关于div嵌套margin-top影响父div的解决办法及各种情况测试
- c++ 名字查找
- C# LINQ去重复杂类型对象集合
- 关于Blocking IO, Non-Blocking IO 和 Asynchronous I/O的理解
- chrome调试工具常用功能整理
- sqlalchemy enum AttributeError: 'list' object has no attribute 'replace'
- 如何解决子div设置margin-top时影响父div的margin-top
- 怎么用java实现md5算法(转自csdn…
- css 网页背景 不滚动 效果
- iOS-界面跟随键盘移动
- 数据库建模 相关,不要一味的追求…
- hibernate处理数据的时候:Session …
- android 调试
- java数据类型转换, 基础不扎实 不…
- jquery-autocomplete 使用手册