2016-12-17 37 views
0

我想知道爲什麼在<div>中有內容(段落等)時出現margin爲什麼Div內容裏有自動保證金?

.container { 
 
    width: 300px; 
 
    height: 150px; 
 
    background-color: #ccc; 
 
} 
 
.container2 { 
 
    width: 300px; 
 
    height: 150px; 
 
    background-color: #000; 
 
}
<div class="container"> 
 
    <p>Text</p> 
 
</div> 
 

 
<div class="container2"> 
 
    <p>Text</p> 
 
</div>

在上面的示例中,存在兩個<div> s之間的裕度,但如果我取出段,將margin不見了。

這是怎麼發生的?

+2

段落內置保證金和保證金摺疊。 –

回答

1

由於某些HTML標記包含默認邊距<p>,因此會摺疊。通常的做法是使用CSS reset來設計網站,正如一樣,只要您希望它們看起來像。

-1

你可以通過你的CSS中做這樣的事情刪除所有元素全部利潤,以*瞄準的所有元素:

*{margin:0px;} 

這也許有點極端,你會是最好關閉找到經過驗證的CSS重置模板將遵循。我有幸運用一年前在網上找到的一款。他們做了這樣的基本復位(選擇性地去除他們想要的任何元素:

html, body, div, span, applet, object, iframe, 
h1, h2, h3, h4, h5, h6, p, blockquote, pre, 
a, abbr, acronym, address, big, cite, code, 
del, dfn, em, img, ins, kbd, q, s, samp, 
small, strike, strong, sub, sup, tt, var, 
b, u, i, center, 
dl, dt, dd, ol, ul, li, 
fieldset, form, label, legend, 
table, caption, tbody, tfoot, thead, tr, th, td, 
article, aside, canvas, details, embed, 
figure, figcaption, footer, header, hgroup, 
menu, nav, output, ruby, section, summary, 
time, mark, audio, video { 
    margin: 0; 
    padding: 0; 
    border: 0; 
    font-size: 100%; 
    font-family:Verdana, Arial, Helvetica, sans-serif; 
    vertical-align: baseline; 
} 

記住你最終不得不放回了很多你想要的利潤,如果你有以下兩種方法去,但它會刪除您期望定位的保證金。

希望它有幫助!

相關問題