简体   繁体   English

父 div 继承子 div 边距

[英]parent div inheriting child divs margin

It doesnt happen all the time but, every once in a while, The parent div inherits the child divs properties and throws my layouts positioning out of whack.它不会一直发生,但偶尔会发生,父 div 继承子 div 属性并使我的布局定位不正常。

for example, this is the html(pseudo):例如,这是 html(伪):

<div 1>
   <div 2>
      content here
   </div>

</div><!-- div one end -->

the Css (pseudo): Css(伪):

#1{ margin top:0; }
#2 {margin top:10 }

its supposed to look like this: div 1 the parent and div 2 the child它应该看起来像这样: div 1 父级和 div 2 子级

div 1parent div 1父级

-----------------------------------------
this is address bar (top of win)
-----------------------------------------
----------------
|  ----------  |
| | div2Child  |
| ------------ |
|--------------| 

assume that there isnt a gap from div 1 to addy window.假设从 div 1 到 addy window 没有间隙。

now, for some odd reason, using the same code above, when i preview, i get this:现在,出于某种奇怪的原因,使用上面相同的代码,当我预览时,我得到了这个:

-----------------------------------------
this is address bar (top of win)
-----------------------------------------

gap here.

----------------
|  ----------  |
| |   div2     |
| ------------ |
|--------------| 

no matter what i do i still get that gap.无论我做什么,我仍然有差距。 When i take it to firebug and test on the Divs, div2 clearly shows that the margin is affecting the parents positioning.当我将它带到 firebug 并在 Divs 上进行测试时,div2 清楚地表明边距正在影响父级定位。

..i was going to put up an example but...well heres the actual code so you can see...(simple as can be) ..我本来打算举个例子,但是...这是实际的代码,所以你可以看到...(尽可能简单)

HTML: HTML:

<div id="one">
    <div id="two">content here on 2</div>
</div>

the CSS: 

    #one{
        width:1000px;
        height:300px;
        background:#09F;
        margin-top:0px !important;
padding:0px;


    }

    #two{
        width:500px;
        height:100px;
        background:red;
        margin-top:20px;
padding:0px;
    }

and i still get the gap i showed above in my diagram.我仍然得到我在图表中显示的差距。 What drives me crazy is, like most of us, weve done this layout millions of times....i can pull up DOZENS of works that use this same method and no problems...i have my Cssreset that i always use.让我发疯的是,像我们大多数人一样,我们已经完成了数百万次这种布局……我可以调出 DOZENS 个使用相同方法的作品,没有问题……我有我一直使用的 Cssreset。 have the,important, no random <br/> or <p> , 0 padding... matter of fact to make SURE, i created a dummy page with nothing but the 2 divs and their css and still........重要的是,没有随机的<br/> or <p> ,0 填充......事实上,为了确定,我创建了一个虚拟页面,除了 2 个 div 和它们的 css 之外什么都没有...... ..

Any thoughts as to why this happens?关于为什么会发生这种情况的任何想法? cause right now im looking at my last site create and this dummy html i just made to test and on every browser, i get that stupid gap.因为现在我正在查看我创建的最后一个站点和我刚刚测试的这个虚拟 html,在每个浏览器上,我都得到了那个愚蠢的差距。

What you're observing is margin-collapse.您正在观察的是保证金崩溃。 It can happen in scenarios that aren't always straightforward to understand, so I suggest referring to documentation .它可能发生在并不总是易于理解的场景中,因此我建议参考文档

Why not use:为什么不使用:

#one{
        width:1000px;
        height:300px;
        background:#09F;
        margin-top:0px !important;
        padding-top:20px;
    }
    #two{
        width:500px;
        height:100px;
        background:red;
        margin-top:0px;
        padding:0px;
    }​

? just wondering就是想

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM