css:神秘的“边距”
每当我有两个水平并排的元素并指定了右和/或左填充和/或边距时,元素之间通常会在我指定的内容之上和之上存在空间。我希望有人能告诉我如何消除这个空间(没有像负边距这样的粗俗东西)。
请注意:我并不是在寻找替代的多列 CSS 布局技术。我知道那里有很多这样的问题,这个问题不仅仅是列布局问题。
以下是工作示例页面的标记和样式。 这是该页面的部分屏幕截图,其中显示了使用 Firebug 选择的左侧元素。所讨论的神秘空间位于右侧,并标有红色星号。没有包含重置样式,但我插入了 Eric Meyers 的重置,但它没有解决问题。
<div id="side-a">
<p>
Lorem ipsum ....
</p>
</div>
<div id="side-b">
<p>
Nunc dapibus....
</p>
</div>
<div id="website-footer">
<ul id="legal-information">
<li>Copyright 2011</li>
<li><a href="#">Privacy Policy</a></li>
</ul>
</div>
div#side-a,
div#side-b {
display: inline-block;
width: 200px;
padding: 17px 17px 0;
}
div#side-a {
vertical-align: top;
}
div#side-b {
background: #999;
}
ul {
padding-bottom: 17px;
list-style: none outside none;
}
ul li {
line-height: 17px;
margin-left: 17px;
}
div#website-footer ul#legal-information {
float: left;
}
div#website-footer ul#legal-information li {
border-left: 1px solid #29443C;
display: inline;
margin: 17px 0;
padding-left: 8px;
}
div#website-footer ul#legal-information li:first-child {
border-left: medium none;
padding: 0 8px 0 0;
}
Whenever I have two elements side by side horizontally with right and/or left padding and/or margin specified, there is often space between the elements over and above what I've specified. I'm hoping someone can tell me how to eliminate that space (without something crufty like a negative margin).
Please note: I am not looking for alternative multi-column CSS layout techniques. I know there are loads of them out there and this issue is bigger than just a column layout issue.
Below is the markup and styles for a working example page. Here's a partial screenshot of that page that shows left elements selected with Firebug. The mysterious space in question is to the right and is marked with a red asterisk. There are no reset styles included but I've plugged in Eric Meyers' reset and it didn't solve the problem.
<div id="side-a">
<p>
Lorem ipsum ....
</p>
</div>
<div id="side-b">
<p>
Nunc dapibus....
</p>
</div>
<div id="website-footer">
<ul id="legal-information">
<li>Copyright 2011</li>
<li><a href="#">Privacy Policy</a></li>
</ul>
</div>
div#side-a,
div#side-b {
display: inline-block;
width: 200px;
padding: 17px 17px 0;
}
div#side-a {
vertical-align: top;
}
div#side-b {
background: #999;
}
ul {
padding-bottom: 17px;
list-style: none outside none;
}
ul li {
line-height: 17px;
margin-left: 17px;
}
div#website-footer ul#legal-information {
float: left;
}
div#website-footer ul#legal-information li {
border-left: 1px solid #29443C;
display: inline;
margin: 17px 0;
padding-left: 8px;
}
div#website-footer ul#legal-information li:first-child {
border-left: medium none;
padding: 0 8px 0 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
inline-block
,这很自然。简单的解决方案是删除空格。http://work.arounds.org/ issues/6/unwanted-white-space- Between-inline-block-elements/
还有其他基于 css 的解决方法,例如在正文上设置字体大小为 0,但据我所知,它们并不一致/可靠。但我可能是错的。
It's natural because of
inline-block
. Simple solution is to kill whitespace.http://work.arounds.org/issue/6/unwanted-white-space-between-inline-block-elements/
There are other css based workarounds such as setting a font size of 0 on the body, but AFAIK they aren't as consistent/reliable. I could be wrong though.
我更改了此 Css:
并在页脚中添加了 Css:
这解决了问题。
当我希望元素彼此相邻时,我通常会浮动它们。
I changed this Css:
And added in Css for the footer:
And this fixed the issue.
I usually float the elements when I want them next to each other.