DIV边框重叠变粗解决方案(子元素浮动导致父元素失去高度解决方法)
2016-03-08 17:27
639 查看
1、问题如图:
代码如下:
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta charset="utf-8"/>
<script>
</script>
<style>
.father{
width:400px;
}
.c{
width:95px;
height:100px;
float:left;
border:1px solid black;
}
</style>
</head>
<body>
<div class="father">
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
</div>
</body>
</html>
2、解决方法
最终解决效果图如下:
具体方法:
2-1:使用伪类选择器(nth-child(4n+1))
代码如下:
<style>
.father{width:400px;}
.c{width:95px;height:100px;float:left;border:1px solid black;border-left:none;border-top:none;}
/*第一行加border-top*/
.father div:nth-child(1),.father div:nth-child(2),.father div:nth-child(3),.father div:nth-child(4){border-top:1px solid black;}
/*第一列加border-left*/
.father div:nth-child(4n+1){border-left:1px solid black;}
</style>
注意:4n+1可不能写成1+4n
2-2:思维决定意思形态,技巧处理
代码如下:
<style>
.father{
width:400px;
}
.c{
width:99px;/*此处有改动:(99+1)*4 = 400*/
height:100px;
float:left;
border:1px solid black;
border-left:none;
border-top:none;
}
/*给父元素加border-top和border-left*/
.father{
overflow:hidden;
border-top:1px solid black;
border-left:1px solid black;
}
</style>
注意:1、父元素.width = 子元素.widht*每行个数(才能不漏马脚)
2、由于里面子元素都为浮动,须给父元素设置overflow:hidden重拾高度;
2-2-2:扩展子元素浮动父元素失去告诉解决方法(clear:both)
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta charset="utf-8"/>
<script>
</script>
<style>
.father{
width:400px;
}
.c{
width:99px;/*此处有改动:(99+1)*4 = 400*/
height:100px;
float:left;
border:1px solid black;
border-left:none;
border-top:none;
}
/*给父元素加border-top和border-left*/
.father{
border-top:1px solid black;
border-left:1px solid black;
}
.clear{
clear:both;
}
</style>
</head>
<body>
<div class="father">
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="clear"></div>
</div>
</body>
</html>
2-2-3:上面解决浮动影响感觉不是很好,个人见解:家里小孩砸坏学校玻璃要家长赔礼道歉?
小孩如果犯错自己去解决不是更好?
优雅处理方案: :after伪类
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta charset="utf-8"/>
<script>
</script>
<style>
.father{
width:400px;
}
.c{
width:99px;/*此处有改动:(99+1)*4 = 400*/
height:100px;
float:left;
border:1px solid black;
border-left:none;
border-top:none;
}
.father:after{
display:block;
content:"clear";
height:0;
clear:both;
overflow:hidden;
visibility:hidden;
}
/*给父元素加border-top和border-left*/
.father{
border-top:1px solid black;
border-left:1px solid black;
}
</style>
</head>
<body>
<div class="father">
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
</div>
</body>
</html>
代码如下:
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta charset="utf-8"/>
<script>
</script>
<style>
.father{
width:400px;
}
.c{
width:95px;
height:100px;
float:left;
border:1px solid black;
}
</style>
</head>
<body>
<div class="father">
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
</div>
</body>
</html>
2、解决方法
最终解决效果图如下:
具体方法:
2-1:使用伪类选择器(nth-child(4n+1))
代码如下:
<style>
.father{width:400px;}
.c{width:95px;height:100px;float:left;border:1px solid black;border-left:none;border-top:none;}
/*第一行加border-top*/
.father div:nth-child(1),.father div:nth-child(2),.father div:nth-child(3),.father div:nth-child(4){border-top:1px solid black;}
/*第一列加border-left*/
.father div:nth-child(4n+1){border-left:1px solid black;}
</style>
注意:4n+1可不能写成1+4n
2-2:思维决定意思形态,技巧处理
代码如下:
<style>
.father{
width:400px;
}
.c{
width:99px;/*此处有改动:(99+1)*4 = 400*/
height:100px;
float:left;
border:1px solid black;
border-left:none;
border-top:none;
}
/*给父元素加border-top和border-left*/
.father{
overflow:hidden;
border-top:1px solid black;
border-left:1px solid black;
}
</style>
注意:1、父元素.width = 子元素.widht*每行个数(才能不漏马脚)
2、由于里面子元素都为浮动,须给父元素设置overflow:hidden重拾高度;
2-2-2:扩展子元素浮动父元素失去告诉解决方法(clear:both)
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta charset="utf-8"/>
<script>
</script>
<style>
.father{
width:400px;
}
.c{
width:99px;/*此处有改动:(99+1)*4 = 400*/
height:100px;
float:left;
border:1px solid black;
border-left:none;
border-top:none;
}
/*给父元素加border-top和border-left*/
.father{
border-top:1px solid black;
border-left:1px solid black;
}
.clear{
clear:both;
}
</style>
</head>
<body>
<div class="father">
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="clear"></div>
</div>
</body>
</html>
2-2-3:上面解决浮动影响感觉不是很好,个人见解:家里小孩砸坏学校玻璃要家长赔礼道歉?
小孩如果犯错自己去解决不是更好?
优雅处理方案: :after伪类
<!DOCTYPE html>
<html>
<head>
<title> new document </title>
<meta charset="utf-8"/>
<script>
</script>
<style>
.father{
width:400px;
}
.c{
width:99px;/*此处有改动:(99+1)*4 = 400*/
height:100px;
float:left;
border:1px solid black;
border-left:none;
border-top:none;
}
.father:after{
display:block;
content:"clear";
height:0;
clear:both;
overflow:hidden;
visibility:hidden;
}
/*给父元素加border-top和border-left*/
.father{
border-top:1px solid black;
border-left:1px solid black;
}
</style>
</head>
<body>
<div class="father">
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
<div class="c"></div>
</div>
</body>
</html>
相关文章推荐
- Python实现动态规划切割钢条问题
- pyhton标准库 json
- 缓存与缓冲的区别与应用
- java.nio.Buffer flip()方法
- mongodb 聚合操作 操作符
- IOS 多个UIImageView 加载高清大图时内存管理
- 最小生成树算法--并查集实现
- linux下的信号
- cmake编写一段C++程序
- 将多个源文件编译成一个ko
- Python安装第三方库PIL时失败的解决办法
- 页游服务器压力测试方案
- JPA总结——实体关系映射(一对一@OneToOne)
- jquery slideToggle 动画问题
- PHP数组操作函数
- 搭建Java开发环境
- C#生成唯一机器标识码
- AT指令解惑
- sql优化
- swift的一些基本属性8 (字典)