【发布时间】:2014-06-30 22:58:00
【问题描述】:
我正在尝试为我的页面制作一个分隔符,使用三个不同颜色的 div 来形成一条高度为 2px 的单行。我正在创建一个包含 3 个细分的 div,每个细分的高度为 2 px。父 div 莫名其妙地占用了 18px 的空间,我认为父 div 应该只占用他孩子的空间。
这是我的 HTML 代码:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body style="margin: 0; padding: 0">
<div class="separator">
<span class="third t1"></span><!--
--><div class="third t2"></div><!--
--><div class="third t3"></div>
</div>
</body>
</html>
还有我的 CSS:
.separator {
height: 2px;
width: 100%;
}
.third {
height: 2px;
margin-top: -10px;
width: 33.33%;
display: inline-block;
}
.t1 {
background-color: #ff7474;
}
.t2 {
background-color: #f1f58d;
}
.t3 {
background-color: #72baf1;
}
这是我的代码的 codepen.io 链接:
【问题讨论】:
-
示例小提琴?此外,根据
display和position,div 通常会占用 100% 的可用宽度。