[英]child div changing parent div margin?
basically I have a container div and a slogan div. 基本上我有一个容器div和一个口号div。 when i apply a margin to the slogan div it applies it to the container div as well and i do not know why.can anyone push me in the right direction as to why it is moving the parent div?
当我在标语div上应用边距时,它也将其应用到容器div上,我不知道为什么。有人可以向我正确的方向推动它为什么移动父div吗?
HTML: HTML:
<div id="container">
<ul>
<a href=""><li>Home</li></a>
<a href=""><li>About</li></a>
<a href=""><li>School Sessions</li></a>
<a href=""><li>Summer</li></a>
<a href=""><li>Online Classes</li></a>
<a href=""><li>Register</li></a>
<a href=""><li>Contact</li></a>
</ul>
<div id="slogan">
</div>
</div>
CSS: CSS:
*{
margin:0;
padding:0;
}
body{
background: #7559a6;
font-family: 'Open Sans', sans-serif;
}
#container{
position:relative;
width:1000px;
height:500px;
margin:auto;
background:white;
}
ul{
position:absolute;
right:0px;
background:rgba(77, 77, 77, 0.75);
border-radius:12px;
margin:25px 0px;
padding:5px;
}
ul li{
display:inline-block;
list-style:none;
color:white;
padding:5px 10px;
}
ul li a{
text-decoration:none;
color:inherit;
}
#slogan{
width:1000px;
height:300px;
background:blue;
margin-top:50px;
}
在这种情况下,最简单的方法是老式的overflow: auto
放入容器div中。
Wrap your menu with a div and give it a min-height of at least 1px, causing it to take some space back in the flow of the document and essentially cause the slogan div to clear onto a new line. 用div包装菜单,并使其最小高度至少为1px,从而使其在文档流中占用一些空间,并实质上使标语div换行。 The slogan div will now "push" down from the menu div.
现在,标语div将从菜单div中“下推”。
HTML: HTML:
<div id="container">
<div id="menu">
<ul>
<a href=""><li>Home</li></a>
<a href=""><li>About</li></a>
<a href=""><li>School Sessions</li></a>
<a href=""><li>Summer</li></a>
<a href=""><li>Online Classes</li></a>
<a href=""><li>Register</li></a>
<a href=""><li>Contact</li></a>
</ul>
</div>
<div id="slogan">
text
</div>
</div>
CSS: CSS:
*{
margin:0;
padding:0;
}
body{
background: #7559a6;
font-family: 'Open Sans', sans-serif;
}
#container{
position:relative;
width:1000px;
height:500px;
margin:auto;
background:white;
}
ul{
position:absolute;
right:0px;
background:rgba(77, 77, 77, 0.75);
border-radius:12px;
margin:25px 0px;
padding:5px;
}
ul li{
display:inline-block;
list-style:none;
color:white;
padding:5px 10px;
}
ul li a{
text-decoration:none;
color:inherit;
}
#slogan{
width:1000px;
height:300px;
background:blue;
margin-top:50px;
}
#menu{
min-height: 1px;
}
First of all, it's alarming that you're wrapping the li with anchor tags. 首先,这令人震惊,您正在用锚标记包装li。 The semantic way of creating a list with links is this:
创建带有链接的列表的语义方式是这样的:
<ul>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
I think what happens in your code is it computes the margin of the elements in a different way than what is usually expected. 我认为您的代码中发生的事情是它以不同于通常预期的方式计算元素的边距。 As what Passerby suggests, search for "margin collapse".
正如Passerby所建议的那样,搜索“边际崩溃”。
What I usually do is to float it: 我通常要做的是浮动它:
float: left;
So just modify your bit of code to this (added more margin): 因此,只需将您的代码修改为此(增加更多的余量):
#slogan{
width:1000px;
height:300px;
background:blue;
margin-top:100px;
float: left;
}
The main reason for this is because of the way margin collapsing works. 这样做的主要原因是由于保证金崩溃的方式。 You can read more about that here: http://reference.sitepoint.com/css/collapsingmargins
您可以在这里阅读有关此内容的更多信息: http : //reference.sitepoint.com/css/collapsingmargins
The extra space your seeing is actually margin sticking out of your slogan. 您看到的多余空间实际上是您的标语中留出的空白。 While margin collapsing is very useful for paragraphs, list-items, etc. it doesn't really make much sense here.
尽管边距折叠对于段落,列表项等非常有用,但在这里并没有多大意义。
Basically, what you want to do is add either a 1px padding to the top of your container. 基本上,您要执行的操作是在容器顶部添加一个1px的填充。
padding-top: 1px
Additionally, you can add a 1px border-top rule that will have the same effect. 此外,您可以添加一个1px的边框顶部规则,该规则将具有相同的效果。
border-top: 1px solid white
Good luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.