有人可以解释一下利润率下降吗?我觉得它们非常烦人
我有一个包含链接的 div:
<div id="like_bar"><a href="#" onclick="return false;" id="like"></a></div>
使用一些 CSS:
#like_bar{
width:140px;
height:26px;
background:url('bar.jpg');
}
#like{
display:block;
width:20px;
height:20px;
margin:3px 36px;
background:url('mini_img.png');
}
链接放置在栏的顶部,链接上的边距应用于栏。
这很烦人。
Could someone explain these collapsing margins, how to avoid them and what they're used for.
I have a div that contains a link:
<div id="like_bar"><a href="#" onclick="return false;" id="like"></a></div>
With some CSS:
#like_bar{
width:140px;
height:26px;
background:url('bar.jpg');
}
#like{
display:block;
width:20px;
height:20px;
margin:3px 36px;
background:url('mini_img.png');
}
The link is placed at the top of the bar and the margins on the link are applied to the bar.
This is annoying.
Could someone explain these collapsing margins, how to avoid them and what they're used for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有很多方法可以“解决这个问题”。
也许对你来说最简单的是:
其他方法包括:
填充
边框
(甚至边框:1px固体透明
就足够了)浮动
元素position:absolute
overflow
设置为默认值visible<之外的值/代码>。
您还问:
一个常见的用例是
标记。
请参阅: http://jsfiddle.net/thirtydot/tPaTY/
如果没有边距折叠,某些事情会变得恼人的。
There are many ways to "fix this".
Perhaps the easiest for you would be this:
Other ways include:
padding
border
(evenborder: 1px solid transparent
is enough)float
the elementposition: absolute
overflow
to a value other than the default ofvisible
.You also asked:
A common use case is the
<p>
tag.See: http://jsfiddle.net/thirtydot/tPaTY/
Without margin collapsing, certain things would become annoying.
因为我很懒,所以我只想链接到一些资源:
我在这里的回答解释了为什么盒子模型是这样的,这与包含边距折叠有关。
w3c css 规范 定义了边距折叠的行为。考虑到盒模型,这是为了方便起见的预期行为。您无需担心内容块之间的双边距。听起来您实际上想要的是一些
#like
周围的填充,而不是边距。将 CSS 视为一种以内容为中心的由内→外的样式方法,而不是一种编程的由外→内的方法。
Because I'm lazy I'm just going to link to a few resources:
My answer here explains why the box model is the way it is, which is related to margin-collapsing being included.
The w3c css spec defines the behavior of margin collapsing. It's an expected behavior for convenience given the box model. You don't need to worry about double margins between blocks of content with it. What it sounds like you actually want is some padding around
#like
, rather than margins.Think of CSS as a content-centric inside→out approach to styling, rather than a programmed outside→in approach.