div盒子或者图片并排居中

要使div总是找不到原因居中很简单,float和display都可以实现,float就不说了,这里说一下display:line-block,比如四个或者多个div盒子,明明设置好了宽度后,总有一个上不去,检查来检查去:比如

 这段代码应该并排排列的。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<style>
.main {
margin: 0 auto;
width: 1000px;
height: 800px;
border: 1px solid #F00;
text-align: center;
background: #933;

}
.divbox {
box-sizing: border-box;
width: 25%;
padding:0px;
margin:0px;
height: 100px;
background: #CCC;
display: inline-block;
}
</style>
<body>
<div class="main">
<div class="divbox"></div>
<div class="divbox"></div>
<div class="divbox"></div>
<div class="divbox"></div>
</div>
</div>
</body>
</html>

运行结果是这样的

没有边框没用margin,中间却有缝隙,原因是因为display后div变成了内联元素,  内联元素代码换行会产生空格。

<div class="divbox"></div><div class="divbox"></div><div class="divbox"></div><div class="divbox"></div>

这样写就不会有问题,但是阅读起来不方便,那该怎么办?可以设置父级元素的font-size设置为0;问题就没了。

.main {
    margin: 0 auto;
    width: 1000px;
    height: 800px;
    border: 1px solid #F00;
    text-align: center;
    background: #933;
    font-size:0px;
}

.............

猜你喜欢

转载自www.cnblogs.com/cgldl/p/10957277.html
今日推荐