我有下面的演示代码:
<html>
<head>
<style type="text/css">
a{
display:block;
float:left;
}
#linkDiv{
border-style:solid;
}
</style>
</head>
<body>
<div id="linkDiv">
<a href="">test</a>
<a href="">test</a>
<a href="">test</a>
</div>
</body>
</html>
我希望我的链接是 block 状的,而且每个链接都彼此相邻。没有 float:left
这就是我得到的:
然而,当我使用 float:left
时,结果是这样的:
提前致谢
您需要在父元素上使用“clearfix”:
overflow: hidden;
或者
/**
* For modern browsers
* 1. The space content is one way to avoid an Opera bug when the
* contenteditable attribute is included anywhere else in the document.
* Otherwise it causes space to appear at the top and bottom of elements
* that are clearfixed.
* 2. The use of `table` rather than `block` is only necessary if using
* `:before` to contain the top-margins of child elements.
*/
.cf:before,
.cf:after {
content: " "; /* 1 */
display: table; /* 2 */
}
.cf:after {
clear: both;
}
/**
* For IE 6/7 only
* Include this rule to trigger hasLayout and contain floats.
*/
.cf {
*zoom: 1;
}
http://nicolasgallagher.com/micro-clearfix-hack/
我是一名优秀的程序员,十分优秀!