2
我有一個h1
標記,當我更改它的margin-top
屬性時,它也會移動父元素和祖父元素。任何人都可以解釋爲什麼會發生這種情況,以及如何解決?爲什麼要更改標題標記的邊距會移動其父元素和祖父元素?
如果您注意到,當h1
保證金設置爲0 auto
時,兩個部分之間具有正確的排水溝。
<section></section>
<section>
<div class="notice">
<h1>Our website is currently undergoing construction, please feel free to contact us on social media.</h1>
</div>
</section>
section{
margin-bottom: 10px;
width: 100vw;
height: 100vh;
background-color: red;
}
section:nth-of-type(2){
background-color: blue;
}
.notice{
width: 100vw;
height: 10vh;
text-align: center;
}
.notice h1{
margin: 0 auto;
}
然而,一旦予設置h1
元件具有餘量,這樣做移動父DIV和祖父母節元件。
<section></section>
<section>
<div class="notice">
<h1>Our website is currently undergoing construction, please feel free to contact us on social media.</h1>
</div>
</section>
section{
margin-bottom: 10px;
width: 100vw;
height: 100vh;
background-color: red;
}
section:nth-of-type(2){
background-color: blue;
}
.notice{
width: 100vw;
height: 10vh;
text-align: center;
}
.notice h1{
margin: 50px auto;
}
所以基本上,我需要設置父母和祖父母元素的邊距? – Robert
@Robert有很多方法可以解決它。您可以在該部分或標題上使用「頂部」,而不是標題上的邊距。或者你可以在該部分添加「overflow:hidden」。如果這兩種方法都不適合你,還有其他解決方法。 –
好的,謝謝你的信息,鏈接和解決方案。 – Robert