2017-06-12 58 views
0

我一直在試圖在我的網站上使用靜態圖像,但標題和圖像之間存在很大差距,我嘗試刪除填充並在Photoshop中更改圖像高度,在HTML中。 這裏是我的問題的小提琴https://jsfiddle.net/o27w80ve/Html標題和靜態圖像之間的空白

section.module:last-child { 
 
    margin-bottom: 0; 
 
} 
 

 
section.module h2 { 
 
    font-family: "Roboto Slab", serif; 
 
    font-size: 30px; 
 
} 
 

 
section.module p { 
 
    margin-bottom: 40px; 
 
    font-size: 16px; 
 
    font-weight: 300; 
 
} 
 

 
section.module p:last-child { 
 
    margin-bottom: 0; 
 
} 
 

 
section.module.content { 
 
    padding: 40px 0; 
 
    color: #090C02; 
 
} 
 

 
section.module.content#section1 { 
 
    background-color: #b23783; 
 
} 
 

 
section.module.content#section2 { 
 
    background-color: #3783b2; 
 
} 
 

 
section.module.parallax { 
 
    height: 600px; 
 
    background-position: 50% 50%; 
 
    background-repeat: no-repeat; 
 
    background-attachment: fixed; 
 
    background-size: cover; 
 
} 
 

 
section.module.parallax h1 { 
 
    color: #8FD5A6; 
 
    font-size: 48px; 
 
    line-height: 600px; 
 
    font-weight: 700; 
 
    text-align: center; 
 
    text-transform: uppercase; 
 
    text-shadow: 0 0 10px rgba(0, 0, 0, 0.2); 
 
    font-family: 'Julius Sans One', sans-serif; 
 
} 
 

 
section.module.parallax-1 { 
 
    background-image: url("https://i.imgur.com/YpRAOt3.jpg"); 
 
} 
 

 
@media all and (min-width: 600px) { 
 
    section.module h2 { 
 
    font-size: 42px; 
 
    } 
 
    section.module p { 
 
    font-size: 20px; 
 
    } 
 
    section.module.parallax h1 { 
 
    font-size: 96px; 
 
    } 
 
} 
 

 
@media all and (min-width: 960px) { 
 
    section.module.parallax h1 { 
 
    font-size: 160px; 
 
    } 
 
} 
 

 
.header ul { 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    overflow: hidden; 
 
    background-color: #1e1e20; 
 
} 
 

 
.header li { 
 
    float: right; 
 
    text-align: middle; 
 
} 
 

 
.header li a { 
 
    display: block; 
 
    color: white; 
 
    text-align: center; 
 
    padding: 14px 16px; 
 
    text-decoration: none; 
 
    font-family: 'Bitter', serif; 
 
} 
 

 
.header li:hover { 
 
    background-color: #fec10e; 
 
    color: black; 
 
}
<header class="header"> 
 
    <div> 
 
    <ul> 
 
     <li><a href="/">Testr Co.</a></li> 
 
     <li><a href="#">Home</a></li> 
 
     <li><a href="#">About Us</a></li> 
 
     <li><a href="#">Blog</a></li> 
 
    </ul> 
 
    </div> 
 
</header> 
 
<section class="module parallax parallax-1"> 
 
    <div class="container"> 
 
    <h1>Example</h1> 
 
    </div> 
 
</section>

回答

1

這就是所謂的 「保證金崩潰。」 h1上的默認margin-top正在摺疊在父級之外。

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Box_Model/Mastering_margin_collapsing

家長和第一/最後一個子 - 如果沒有邊框,填充,內嵌的內容,block_formatting_context創建或間隙塊的上邊距從的上邊距分開它的第一個子塊,或沒有邊框,填充,內聯內容,高度,最小高度或最大高度以將塊的邊緣底部與其最後一個子塊的邊緣底部分開,那麼這些邊界將摺疊。摺疊後的保證金最終在家長之外。

section.module:last-child { 
 
    margin-bottom: 0; 
 
} 
 

 
section.module h2 { 
 
    font-family: "Roboto Slab", serif; 
 
    font-size: 30px; 
 
} 
 

 
section.module p { 
 
    margin-bottom: 40px; 
 
    font-size: 16px; 
 
    font-weight: 300; 
 
} 
 

 
section.module p:last-child { 
 
    margin-bottom: 0; 
 
} 
 

 
section.module.content { 
 
    padding: 40px 0; 
 
    color: #090C02; 
 
} 
 

 
section.module.content#section1 { 
 
    background-color: #b23783; 
 
} 
 

 
section.module.content#section2 { 
 
    background-color: #3783b2; 
 
} 
 

 
section.module.parallax { 
 
    height: 600px; 
 
    background-position: 50% 50%; 
 
    background-repeat: no-repeat; 
 
    background-attachment: fixed; 
 
    background-size: cover; 
 
} 
 

 
section.module.parallax h1 { 
 
    color: #8FD5A6; 
 
    font-size: 48px; 
 
    line-height: 600px; 
 
    font-weight: 700; 
 
    text-align: center; 
 
    text-transform: uppercase; 
 
    text-shadow: 0 0 10px rgba(0, 0, 0, 0.2); 
 
    font-family: 'Julius Sans One', sans-serif; 
 
    margin-top: 0; 
 
} 
 

 
section.module.parallax-1 { 
 
    background-image: url("https://i.imgur.com/YpRAOt3.jpg"); 
 
} 
 

 
@media all and (min-width: 600px) { 
 
    section.module h2 { 
 
    font-size: 42px; 
 
    } 
 
    section.module p { 
 
    font-size: 20px; 
 
    } 
 
    section.module.parallax h1 { 
 
    font-size: 96px; 
 
    } 
 
} 
 

 
@media all and (min-width: 960px) { 
 
    section.module.parallax h1 { 
 
    font-size: 160px; 
 
    } 
 
} 
 

 
.header ul { 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    overflow: hidden; 
 
    background-color: #1e1e20; 
 
} 
 

 
.header li { 
 
    float: right; 
 
    text-align: middle; 
 
} 
 

 
.header li a { 
 
    display: block; 
 
    color: white; 
 
    text-align: center; 
 
    padding: 14px 16px; 
 
    text-decoration: none; 
 
    font-family: 'Bitter', serif; 
 
} 
 

 
.header li:hover { 
 
    background-color: #fec10e; 
 
    color: black; 
 
}
<header class="header"> 
 
    <div> 
 

 
    <ul> 
 
     <li><a href="/">Testr Co.</a></li> 
 
     <li><a href="#">Home</a></li> 
 
     <li><a href="#">About Us</a></li> 
 
     <li><a href="#">Blog</a></li> 
 
    </ul> 
 
    </div> 
 
</header> 
 
<section class="module parallax parallax-1"> 
 
    <div class="container"> 
 
    <h1>Example</h1> 
 
    </div> 
 
</section>

0

檢查你的代碼後,你可以看到,在 'section.module.parallax H1' 具有40像素的邊距。 你需要使用這個(margin-top:0)來消除空隙。

相關問題