为什么添加或删除边框会改变边距的工作方式? [英] Why is adding or removing a border changing the way margins work?

查看:59
本文介绍了为什么添加或删除边框会改变边距的工作方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个容器,其中一个元素的内部有一个空白,则该元素空白不会将其向下推入该容器中,而是向下推入容器本身.仅此而已,虽然很烦人,但可能只是w3c决定它将如何工作的方式.但是很奇怪的是,如果我在容器上添加边框,则该元素现在将被推入容器中,然后容器将与上面的元素齐平(我想要的).到现在为止,我使用的大多数容器都带有边框,以便我可以判断所有内容如何排列,但是现在,我决定开始添加背景色并删除边距,以使所有内容看起来更加美观.容器之间没有应用背景色的容器之间存在间隙问题.

If I have a container with an element inside that has a margin, that elements margin will not push it down within that container but instead push down the container itself. That alone, while annoying, might just be how the w3c decided it would work. But the weird thing is, if I add a border to the container that element will now be pushed down within the container and the container will then be flush against the element above (what I want). Until now most of the containers I use have had borders so that I could tell how everything was lining up, but now that I have decided to start adding background-colors and removing margins to make everything look nicer I am running in to a ton of issues with gaps between containers that have neither of the containers background colors applied to them.

在不清楚的情况下,我创建了一个jsfiddle来演示该问题,如您所见,通过查看html/js,单击按钮只会切换边框的存在,而没有其他任何变化.但是,容器之间的所有边距都会根据边界是否存在而改变.

In case I was not clear I created a jsfiddle to demonstrate the issue, as you can see by looking at the html / js clicking the button only toggles the existence of a border and nothing else. Yet all the margins between the containers change based on whether or not the border is there.

http://jsfiddle.net/Tysonzero/p5fgjmrn/

HTML:

<div class="first-div">
</div>
<div class="second-div">
    <div class="inner-div">Test</div>
</div>
<button onclick="toggle()">Toggle Border</button>

CSS:

*
{
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}
.first-div
{
    background-color: red;
    min-height: 50px;
}
.second-div
{
    background-color: blue;
    min-height: 50px;
}
.inner-div
{
    margin: 10px;
    text-align: center;
}

JS:

toggle = function() {
    if (document.getElementsByClassName('second-div')[0].style.border) {
        document.getElementsByClassName('second-div')[0].style.border = "";
    }
    else {
        document.getElementsByClassName('second-div')[0].style.border = "1px solid";
    }
};

我希望它始终与IMO的边界一起工作,这使更多的事情变得有意义,而对于我的项目,这正是我所需要的.我真的希望我不必做任何麻烦的事情,例如为所有内容添加1px透明边框,并在必要时进行补偿.我试过使用溢出:自动;或溢出:隐藏.但是,两者都没有边界.溢出:自动;如果里面的元素大于父元素,或者如果您以某种方式使用负边距而没有添加边框,则有时会创建滚动条.完全可以防止滚动,而添加边框则不能.

I want it to always work the way it works WITH a border as IMO that makes a ton more sense, and for my project it is what I need. I really hope I don't have to do something hacky such as add a 1px transparent border to everything and compensate for it when necessary. I have tried using overflow: auto; or overflow: hidden. But neither are the same as having a border. overflow: auto; will sometimes create scrollbars if the element inside is bigger than the parent or if you are using negative margins in a certain way whereas adding a border does not, overflow: hidden; completely prevents scrolling whereas adding a border does not.

有人知道我可以如何强制浏览器将所有内容都视为带有边框(或至少不会出现此类问题),并且有人可以解释这种现象吗?似乎是无意的,但我可能是错的.

Does anyone know how I can force the browser to treat everything as though it has a border (or at least make issues like this not happen) and also can anyone explain this behavior? It seems unintentional but I could be wrong.

推荐答案

您问题的具体答案是

The specific answer to your question is collapsing margins and is part of the W3C's BOX MODEL specifications:

某些框之间的垂直边距可能会折叠:

Vertical margins may collapse between certain boxes:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆