<kbd id="qceq4"><acronym id="qceq4"></acronym></kbd>
<kbd id="qceq4"><em id="qceq4"></em></kbd>
<pre id="qceq4"><samp id="qceq4"></samp></pre>
<code id="qceq4"></code>
\r\n????寬為100px,右邊距為50px<\/p>\r\n????寬為100px,左邊距為50px<\/p>\r\n<\/body>\r\n<\/html><\/pre>\n

效果如下:<\/p>\n

\"\"<\/p>\n

這時兩者之間的距離剛好為100px。<\/p>\n

補(bǔ)充說明:<\/span><\/strong>大家可以看到,為了使得兩個p(塊狀元素)脫離正常的文檔流我使用了display:inline-block;屬性,另外,我還把body的font-size設(shè)置為0,這樣可以解決inline-block自身的問題,否則兩個p的舉例會大于100px。當(dāng)然使用float也可以使得兩個p出現(xiàn)在同一行中。<\/p>\n

(2)豎直方向的外邊距合并<\/strong><\/p>\n

兩個豎直方向的盒子相遇時,其豎直方向的距離等于上方盒子的下外邊距和下方盒子的上外邊距中較大的一個。<\/p>\n

例2:<\/p>\n

\r\n\r\n\r\n????\r\n????水平方向的兩個盒子<\/title>\r\n????<style>\r\n????????*{\r\n????????????margin:0;\r\n????????????padding:0;\r\n????????????border:0;\r\n????????}\r\n????????.top{\r\n????????????width:?100px;\r\n????????????height:?100px;\r\n????????????margin-bottom:?100px;\r\n????????????background:?red;\r\n????????}\r\n????????.bottom{\r\n????????????width:?100px;\r\n????????????height:?100px;\r\n????????????margin-top:?50px;\r\n????????????background:?green;\r\n????????}\r\n????<\/style>\r\n<\/head>\r\n<body>
<h1><a href="http://ipnx.cn/">亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱</a></h1>\r\n????<p class=\"top\">高為100px,下邊距為100px<\/p>\r\n????<p class=\"bottom\">高為100px,上邊距為50px<\/p>\r\n<\/body>\r\n<\/html><\/pre>\n<p style=\"text-align: left;\">效果如下:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/317e3e4cdee475e69aec60cf01f8b4b2-2.png\"><\/p>\n<p style=\"text-align: left;\">這時我們?nèi)庋鄱伎梢杂^察出來,兩者豎直方向的舉例大約為100px(實際就是100px)而非100+50=150px;這正是因為兩個豎直方向的盒子相遇時,其豎直方向的距離等于上方盒子的下外邊距和下方盒子的上外邊距中較大的一個。<\/p>\n<p style=\"text-align: left;\">另外一個有趣的例子就是:假設(shè)有一個元素同時設(shè)置了margin-top和margin-bottom,但是內(nèi)容為空,那么這兩個margin值也會疊加,值為兩者最大的一個,它類似與豎直方向上兩個盒子margin值的疊加。代碼如下:<\/p>\n<pre><!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n????<meta charset=\"UTF-8\">\r\n????<title>水平方向的兩個盒子<\/title>\r\n????<style>\r\n????????*{\r\n????????????margin:0;\r\n????????????padding:0;\r\n????????}\r\n?\r\n????????.top{\r\n????????????width:?500px;\r\n????????????height:?100px;\r\n????????????background:?red;\r\n????????}\r\n????????.middle{\r\n????????????margin-top:?100px;\r\n????????????margin-bottom:50px;\r\n????????}\r\n????????.footer{\r\n????????????width:?500px;\r\n????????????height:?100px;\r\n????????????background:?green;\r\n????????}\r\n?\r\n????<\/style>\r\n<\/head>\r\n<body>\r\n????<p class=\"top\">上面的p,高100px<\/p>\r\n????<p class=\"middle\"><\/p>\r\n????<p class=\"footer\">下面的p,高100px<\/p>\r\n<\/body>\r\n<\/html><\/pre>\n<p style=\"text-align: left;\">最終的效果如下:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/317e3e4cdee475e69aec60cf01f8b4b2-3.png\"><\/p>\n<p style=\"text-align: left;\">我們發(fā)現(xiàn)這時在上面的p和在下面的p之間的舉例并不是100+50=150px,而是兩者中的最大者,即100px。<\/p>\n<p style=\"text-align: left;\">那么W3C為什么會設(shè)定這樣的標(biāo)準(zhǔn)而不設(shè)定和水平方向一樣的標(biāo)準(zhǔn)呢?即margin值的疊加,實際上這也是有一定的道理的。比如我們需要設(shè)計一個由若干個段落構(gòu)成的一個頁面。我們需要設(shè)置margin-top和margin-bottom使得第一段和頁面的最上方有一段距離,使得最后一段和最下方有一段距離。下面是不疊加和疊加的效果圖:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/317e3e4cdee475e69aec60cf01f8b4b2-4.png\"><\/p>\n<p style=\"text-align: left;\">我們可以看到左邊的頁面沒有重疊,那么兩個段落之間的舉例就是最上方的兩倍間距了,而右邊的頁面發(fā)生了重疊,則所有的間距都是相等的?;蛟S這就是這樣設(shè)定標(biāo)準(zhǔn)的目的吧,誰知道呢?<\/p>\n<p style=\"text-align: left;\"><strong>第三部分:margin--在父元素和子元素之間應(yīng)用(重點)<\/strong><\/p>\n<p style=\"text-align: left;\">第二部分介紹了同級元素之間使用margin,而這一部分將要介紹最有意思的父元素和子元素之間margin的應(yīng)用。這一部分,我們同樣從兩個方面來討論。一方面是子元素設(shè)置水平方向上的margin值,另一方面是子元素設(shè)置豎直方向的margin值。<\/p>\n<p style=\"text-align: left;\">(1)在子元素中設(shè)置水平方向的margin值<\/p>\n<p style=\"text-align: left;\">我們可以設(shè)置margin-left來控制子元素的左邊框和父元素的左邊框之間的舉例。<\/p>\n<p style=\"text-align: left;\">例3:<\/p>\n<pre><!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n????<meta charset=\"UTF-8\">\r\n????<title>margin<\/title>\r\n????<style>\r\n????????*{padding:0;?margin:0;?border:0;}\r\n????????.father{\r\n????????????width:?500px;\r\n????????????height:?500px;\r\n????????????background:?red;\r\n????????}\r\n????????.son{\r\n????????????width:?100px;\r\n????????????height:?100px;\r\n????????????background:?green;\r\n????????????margin-left:?100px;\r\n????????}\r\n????<\/style>\r\n<\/head>\r\n<body>\r\n????<p class=\"father\">\r\n????????<p class=\"son\">寬度為100px,margin-left為100px。<\/p>\r\n????<\/p>\r\n<\/body>\r\n<\/html><\/pre>\n<p style=\"text-align: left;\">我將子元素的margin-left設(shè)置為了100px;效果如下:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\"    style=\"max-width:90%\"  style=\"max-width:90%\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/317e3e4cdee475e69aec60cf01f8b4b2-5.png\"><\/p>\n<p style=\"text-align: left;\">即子元素的左邊框和父元素的左邊框之間的距離為100px。與在同級元素之間設(shè)置margin不同,因為同級元素之間的margin不會考慮到padding,但是在父元素和子元素就不同了,那么如果父元素中如果有padding,效果會是什么樣的呢?請看下面一個例子:<\/p>\n<p style=\"text-align: left;\">例4:<\/p>\n<p style=\"text-align: left;\">下面我們在上面例子的基礎(chǔ)上給父元素添加padding值。<\/p>\n<pre><!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n????<meta charset=\"UTF-8\">\r\n????<title>margin<\/title>\r\n????<style>\r\n????????*{padding:0;?margin:0;?border:0;}\r\n????????.father{\r\n????????????width:?500px;\r\n????????????height:?500px;\r\n????????????padding:100px;\r\n????????????background:?red;\r\n????????}\r\n????????.son{\r\n????????????width:?100px;\r\n????????????height:?100px;\r\n????????????background:?green;\r\n????????????margin-left:?100px;\r\n????????}\r\n????<\/style>\r\n<\/head>\r\n<body>\r\n????<p class=\"father\">\r\n????????<p class=\"son\">寬度為100px,margin-left為100px。<\/p>\r\n????<\/p>\r\n<\/body>\r\n<\/html><\/pre>\n<p style=\"text-align: left;\">上面的代碼給父元素添加了100px的padding值,效果如下:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/a7832a4c683024af2d836c39cf9bd1a7-6.png\"><\/p>\n<p style=\"text-align: left;\">我們可以看到子元素舉例上方的距離為100px,因為子元素一定是在父元素的content的部分的,這點毫無疑問。<\/p>\n<p style=\"text-align: left;\">但是經(jīng)過測量可以發(fā)現(xiàn)子元素的左邊框距離父元素的左邊框之間的距離為200px,因為其中還有100px的左padding值,前面的例子因為我沒有設(shè)置padding值,所以沒有觀察出來,因此這就說明了在子元素中設(shè)置margin-left,其值實際上是子元素的左邊框距離父元素左padding內(nèi)側(cè)的距離。<\/p>\n<p style=\"text-align: left;\">例5:margin-right的使用和margin-left的使用是相似的,我在這里只舉一個例子。<\/p>\n<p style=\"text-align: left;\">這個例子在子元素中設(shè)置了margin-right值,如下所示:<\/p>\n<pre><!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n????<meta charset=\"UTF-8\">\r\n????<title>margin<\/title>\r\n????<style>\r\n????????*{padding:0;?margin:0;?border:0;}\r\n????????.father{\r\n????????????width:?500px;\r\n????????????height:?500px;\r\n????????????padding:100px;\r\n????????????background:?red;\r\n????????}\r\n????????.son{\r\n????????????float:?right;\r\n????????????width:?100px;\r\n????????????height:?100px;\r\n????????????background:?green;\r\n????????????margin-right:?100px;\r\n????????}\r\n????<\/style>\r\n<\/head>\r\n<body>\r\n????<p class=\"father\">\r\n????????<p class=\"son\">寬度為100px,margin-right為100px。<\/p>\r\n????<\/p>\r\n<\/body>\r\n<\/html><\/pre>\n<p style=\"text-align: left;\">這個例子與例4的區(qū)別僅在與子元素的位置不同。效果如下:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/a7832a4c683024af2d836c39cf9bd1a7-7.png\"><\/p>\n<p style=\"text-align: left;\">通過這個例子可以說明margin-right的值是子元素的右邊框和父元素的右padding內(nèi)側(cè)的距離。只是前面的幾個例子我沒有使用padding,所以無法觀察出來。<\/p>\n<p style=\"text-align: left;\">(2)在子元素中設(shè)置豎直方向的margin值<\/p>\n<p style=\"text-align: left;\">按照前面的經(jīng)驗,理論上來說,我們同樣可以通過設(shè)置margin-top的值使得子元素的上邊框和父元素的上padding的內(nèi)側(cè)留有一定的距離。那么我們就試試吧!<\/p>\n<p style=\"text-align: left;\">例6:<\/p>\n<pre><!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n????<meta charset=\"UTF-8\">\r\n????<title>margin<\/title>\r\n????<style>\r\n????????*{padding:0;?margin:0;?border:0;}\r\n????????.father{\r\n????????????width:?500px;\r\n????????????height:?500px;\r\n????????????background:?red;\r\n????????}\r\n????????.son{\r\n????????????width:?100px;\r\n????????????height:?100px;\r\n????????????background:?green;\r\n????????????margin-top:?100px;\r\n????????}\r\n????<\/style>\r\n<\/head>\r\n<body>\r\n????<p class=\"father\">\r\n????????<p class=\"son\">高度為100px,margin-top為100px。<\/p>\r\n????<\/p>\r\n<\/body>\r\n<\/html><\/pre>\n<p style=\"text-align: left;\">這個例子我設(shè)置了margin-top為100px,效果如下:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/a7832a4c683024af2d836c39cf9bd1a7-8.png\"><\/p>\n<p style=\"text-align: left;\">這并不是我們想要的效果啊,我們希望子元素的上部距離父元素的上部為100px,可是我們看到的卻是父元素的上部距離瀏覽器頁面的上部有100px的距離,這是為什么呢?哪里出現(xiàn)問題了呢?<\/p>\n<p style=\"text-align: left;\">實際上這是因為當(dāng)父元素沒有設(shè)置padding值以及border值時,出現(xiàn)了一個bug--父元素的上方與子元素的上方完全重合在了一起,無法分開。所以才會導(dǎo)致上述這種父元素和子元素同時向下的情況。<\/p>\n<p style=\"text-align: left;\">對于這種問題解決方法有下面幾種:<\/p>\n<ul class=\" list-paddingleft-2\">\n<li><p style=\"text-align: left;\">方法一:給父元素添加padding-top值<\/p><\/li>\n<li><p style=\"text-align: left;\">方法二:給父元素添加border值<\/p><\/li>\n<li><p style=\"text-align: left;\">方法三:給父元素添加屬性overflow:hidden;<\/p><\/li>\n<li><p style=\"text-align: left;\">方法四:給父元素或者子元素聲明浮動float<\/p><\/li>\n<li><p style=\"text-align: left;\">方法五:使父元素或子元素聲明為<a href=\"http:\/\/ipnx.cn\/code\/6074.html\" target=\"_blank\">絕對定位<\/a>:position:absolute;<\/p><\/li>\n<li><p style=\"text-align: left;\">方法六:給父元素添加屬性 overflow:auto; positon:relative;(注:此方法為后續(xù)添加,感謝博友@小精靈Pawn提供此方法)<\/p><\/li>\n<\/ul>\n<p style=\"text-align: left;\">方法一:基于例6,在父元素的css代碼中添加padding-top:1px;效果如下:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/0ccfb338b8fe6d678c4b327eee5d5aa0-9.png\"><\/p>\n<p style=\"text-align: left;\">?方法的唯一缺點就是增加了1px的誤差。<\/p>\n<p style=\"text-align: left;\">方法二:基于例6,在父元素的css代碼中添加border-top:1px solid transparent;效果如下:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/0ccfb338b8fe6d678c4b327eee5d5aa0-10.png\"><\/p>\n<p style=\"text-align:left;\">同樣達(dá)到了效果, 缺點同方法一。<\/p>\n<p style=\"text-align: left;\">方法三:基于例6,在父元素的css代碼中添加overflow:hidden;效果如下:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/0ccfb338b8fe6d678c4b327eee5d5aa0-11.png\"><\/p>\n<p style=\"text-align: left;\">同樣達(dá)到了效果,并且沒有任何誤差的存在??胺Qperfect!!!!<\/p>\n<p style=\"text-align: left;\">方法四:給父元素或者子元素聲明float;基于例6,在子元素css代碼添加float:left;或者在父元素css代碼添加float:left;均達(dá)到效果,這里不再展示相同的圖片。<\/p>\n<p style=\"text-align: left;\">優(yōu)點:沒有像素的誤差。?? 缺點:float有時是不必要的。<\/p>\n<p style=\"text-align: left;\">方法五:給父元素或者子元素添加position:absolute;屬性。 同樣達(dá)到效果。<\/p>\n<p style=\"text-align: left;\">優(yōu)點:同方法四。? 且只要我們不使用top和left也不會有任何影響,所以這也是一種不錯的方法。<\/p>\n<p style=\"text-align: left;\">方法六:給父元素添加overflow:auto;和position:relative;同樣達(dá)到效果。<\/p>\n<p style=\"text-align: left;\"><strong>第四部分:margin值的單位為%時的幾種情況<\/strong><\/p>\n<p style=\"text-align: left;\">之前我舉例子時使用margin,它的值都是以px為單位的,這個理解起來沒有問題。但是如果margin值是以%為單位呢?實際上這時候百分比(%)是相對于該元素的父元素(容器),對于同級元素和父子元素都是如此。(再次感謝 博友@小精靈Pawn 提供的建議??!基于此建議補(bǔ)充這部分內(nèi)容) 但是在同級元素中使用豎直方向的margin時會出現(xiàn)意想不到的結(jié)果,下面舉例說明。<\/p>\n<p style=\"text-align: left;\">(1)同級元素在水平方向使用值為%的margin<\/p>\n<p style=\"text-align: left;\">例7:<\/p>\n<pre><head>\r\n????<meta charset=\"UTF-8\">\r\n????<title>margin<\/title>\r\n????<style>\r\n????????*{\r\n????????????margin:0;\r\n????????????padding:0;\r\n????????}\r\n????????.first{\r\n????????????float:?left;\r\n????????????width:?200px;\r\n????????????height:?200px;\r\n????????????background:?green;\r\n????????}\r\n????????.second{\r\n????????????float:?left;\r\n????????????width:?200px;\r\n????????????height:?200px;\r\n????????????background:?red;\r\n????????????margin-left:?20%;\r\n????????}\r\n????<\/style>\r\n<\/head>\r\n<body>\r\n????<p class=\"first\">寬為200,無margin<\/p>\r\n????<p class=\"second\">寬為200,margin-left為20%;<\/p>\r\n<\/body>\r\n<\/html><\/pre>\n<p style=\"text-align: left;\">這個例子中,設(shè)置兩個元素向左浮動,以便于觀察兩者水平方向的margin。其中左邊p無margin,右邊p的margin-left為20%,效果如下:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/4afda3ae098dfd1684f0b60d5616ac9d-12.gif\"><\/p>\n<p style=\"text-align: left;\">從效果圖可以看出兩個p之間的間距始終為父元素(這里右邊p的父元素即為body,其寬度為瀏覽器寬度)的20%。<\/p>\n<p style=\"text-align: left;\">(2)同級元素在豎直方向使用值為%的margin<\/p>\n<p style=\"text-align: left;\">根據(jù)例7的啟發(fā),我們可以猜想,如果在豎直方向上使用margin,且值的單位為%,那么最終兩者之間的距離將是父元素(上例中為body)的百分?jǐn)?shù)。那么究竟是不是這樣呢?看下面的例子。<\/p>\n<p style=\"text-align: left;\">例8<\/p>\n<pre><head>\r\n????<meta charset=\"UTF-8\">\r\n????<title>margin<\/title>\r\n????<style>\r\n????????*{\r\n????????????margin:0;\r\n????????????padding:0;\r\n????????}\r\n????????.first{\r\n????????????width:?200px;\r\n????????????height:?200px;\r\n????????????background:?green;\r\n????????}\r\n????????.second{\r\n????????????width:?200px;\r\n????????????height:?200px;\r\n????????????background:?red;\r\n????????????margin-top:?10%;\r\n????????}\r\n????<\/style>\r\n<\/head>\r\n<body>\r\n????<p class=\"first\">高為200,無margin<\/p>\r\n????<p class=\"second\">高為200,margin-top為20%;<\/p>\r\n<\/body>\r\n<\/html><\/pre>\n<p style=\"text-align: left;\">這里設(shè)置上面的p無margin,下面的p的margin-top為10。效果如下:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/0ee5c0d9b67d186cbf21ca067a244297-13.gif\"><\/p>\n<p style=\"text-align: left;\">我們發(fā)現(xiàn),當(dāng)我在縮小瀏覽器的高度時,豎直方向上的間距并沒有縮?。。?! 而當(dāng)我縮小瀏覽器的寬度時,豎直方向上的距離縮小了?。。?\/p>\n<p style=\"text-align: left;\">這就說明:統(tǒng)計元素之間在豎直方向上使用margin,當(dāng)值的單位為%時,它是相對于父元素的寬度。<\/p>\n<p style=\"text-align: left;\">那么這里為什么不是如我們所希望的那樣相對于瀏覽器的高度呢?知乎上有大神是這樣解釋的:<\/p>\n<p style=\"text-align: left;\"><img alt=\"\"    style=\"max-width:90%\"  style=\"max-width:90%\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/5907097efe49c76cc8fc447782c27c1c-14.png\"><\/p>\n<p style=\"text-align: left;\">(3)父子元素使用值為%的margin<\/p>\n<p style=\"text-align: left;\">對于父子元素,如果在子元素中使用單位為%margin,那么這個margin值是相對于父元素的寬度和高度(注意:這時的確是相對于父元素的高度?。┑?。<\/p>\n<p style=\"text-align: left;\">例9?<\/p>\n<p style=\"text-align: left;\">代碼如下:<\/p>\n<pre><!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n????<meta charset=\"UTF-8\">\r\n????<title>Document<\/title>\r\n????<style>\r\n????????*{\r\n????????????margin:0;\r\n????????????padding:0;\r\n????????}\r\n????????.father{\r\n????????????width:?500px;\r\n????????????height:?300px;\r\n????????????background:?red;\r\n????????????overflow:?hidden;\r\n????????}\r\n????????.son{\r\n????????????width:?100px;\r\n????????????height:?100px;\r\n????????????background:?green;\r\n????????????margin-top:?20%;\r\n????????????margin-left:?20%;\r\n????????}\r\n????<\/style>\r\n<\/head>\r\n<body>\r\n????<p class=\"father\">\r\n????????<p class=\"son\"><\/p>\r\n????<\/p>\r\n<\/body>\r\n<\/html><\/pre>\n<p style=\"text-align: left;\">在這個例子中,我設(shè)置了margin-left的值為20%,margin-top的值為20%,父元素的width為500px,父元素的height為300px。下面看看效果吧。<\/p>\n<p style=\"text-align: left;\"><img alt=\"\"    style=\"max-width:90%\"  style=\"max-width:90%\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/0d09889803d6fef7ca5c780770b40238-15.png\"><\/p>\n<p style=\"text-align: left;\">從上圖可以看出子元素的margin-top值最終同樣是相對與父元素的寬度而非高度。<\/p>\n<p style=\"text-align: left;\"><strong>總結(jié):<\/strong><\/p>\n<p style=\"text-align: left;\">這篇博文只介紹了margin的其中一部分知識點,但如果大家讀后能有些許收獲是再好不過的了。由于本人總結(jié)倉促,知識不足,錯誤在所難免,希望大家如果發(fā)現(xiàn)不妥之初能提出意見,我將非常感激。<\/p>\n<p style=\"text-align: left;\">相信看了本文案例你已經(jīng)掌握了方法,更多精彩請關(guān)注php中文網(wǎng)其它相關(guān)文章!<\/p>\n<p style=\"text-align: left;\">推薦閱讀:<\/p>\n<p style=\"text-align: left;\"><a href=\"http:\/\/ipnx.cn\/css-tutorial-389742.html\" target=\"_blank\">CSS3的box-sizing屬性圖文教程<\/a><\/p>\n<p style=\"text-align: left;\"><a href=\"http:\/\/ipnx.cn\/css-tutorial-389740.html\" target=\"_blank\">在CSS邊界線消失如何處理<\/a><\/p>\n<p style=\"text-align: left;\"><a href=\"http:\/\/ipnx.cn\/css-tutorial-389730.html\" target=\"_blank\">三種絕對定位元素的水平垂直居中的辦法<\/a><\/p>"}	</script>
	
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<script>var V_PATH="/";window.onerror=function(){ return true; };</script>
</head>

<body data-commit-time="2023-12-28T14:50:12+08:00" class="editor_body body2_2">
	<link rel="stylesheet" type="text/css" href="/static/csshw/stylehw.css">
<header>
    <div   id="wjcelcm34c"   class="head">
        <div   id="wjcelcm34c"   class="haed_left">
            <div   id="wjcelcm34c"   class="haed_logo">
                <a href="http://ipnx.cn/zh/" title="" class="haed_logo_a">
                    <img src="/static/imghw/logo.png" alt="" class="haed_logoimg">
                </a>
            </div>
            <div   id="wjcelcm34c"   class="head_nav">
                <div   id="wjcelcm34c"   class="head_navs">
                    <a href="javascript:;" title="社區(qū)" class="head_nava head_nava-template1">社區(qū)</a>
                    <div   class="wjcelcm34c"   id="dropdown-template1" style="display: none;">
                        <div   id="wjcelcm34c"   class="languagechoose">
                            <a href="http://ipnx.cn/zh/article.html" title="文章" class="languagechoosea on">文章</a>
                            <a href="http://ipnx.cn/zh/faq/zt" title="合集" class="languagechoosea">合集</a>
                            <a href="http://ipnx.cn/zh/wenda.html" title="問答" class="languagechoosea">問答</a>
                        </div>
                    </div>
                </div>

                <div   id="wjcelcm34c"   class="head_navs">
                    <a href="javascript:;" title="學(xué)習(xí)" class="head_nava head_nava-template1_1">學(xué)習(xí)</a>
                    <div   class="wjcelcm34c"   id="dropdown-template1_1" style="display: none;">
                        <div   id="wjcelcm34c"   class="languagechoose">
                            <a href="http://ipnx.cn/zh/course.html" title="課程" class="languagechoosea on">課程</a>
                            <a href="http://ipnx.cn/zh/dic/" title="編程詞典" class="languagechoosea">編程詞典</a>
                        </div>
                    </div>
                </div>

                <div   id="wjcelcm34c"   class="head_navs">
                    <a href="javascript:;" title="工具庫" class="head_nava head_nava-template1_2">工具庫</a>
                    <div   class="wjcelcm34c"   id="dropdown-template1_2" style="display: none;">
                        <div   id="wjcelcm34c"   class="languagechoose">
                            <a href="http://ipnx.cn/zh/toolset/development-tools" title="開發(fā)工具" class="languagechoosea on">開發(fā)工具</a>
                            <a href="http://ipnx.cn/zh/toolset/website-source-code" title="網(wǎng)站源碼" class="languagechoosea">網(wǎng)站源碼</a>
                            <a href="http://ipnx.cn/zh/toolset/php-libraries" title="PHP 庫" class="languagechoosea">PHP 庫</a>
                            <a href="http://ipnx.cn/zh/toolset/js-special-effects" title="JS特效" class="languagechoosea on">JS特效</a>
                            <a href="http://ipnx.cn/zh/toolset/website-materials" title="網(wǎng)站素材" class="languagechoosea on">網(wǎng)站素材</a>
                            <a href="http://ipnx.cn/zh/toolset/extension-plug-ins" title="擴(kuò)展插件" class="languagechoosea on">擴(kuò)展插件</a>
                        </div>
                    </div>
                </div>

                <div   id="wjcelcm34c"   class="head_navs">
                    <a href="http://ipnx.cn/zh/ai" title="AI工具" class="head_nava head_nava-template1_3">AI工具</a>
                </div>

                <div   id="wjcelcm34c"   class="head_navs">
                    <a href="javascript:;" title="休閑" class="head_nava head_nava-template1_3">休閑</a>
                    <div   class="wjcelcm34c"   id="dropdown-template1_3" style="display: none;">
                        <div   id="wjcelcm34c"   class="languagechoose">
                            <a href="http://ipnx.cn/zh/game" title="游戲下載" class="languagechoosea on">游戲下載</a>
                            <a href="http://ipnx.cn/zh/mobile-game-tutorial/" title="游戲教程" class="languagechoosea">游戲教程</a>

                        </div>
                    </div>
                </div>
            </div>
        </div>
                    <div   id="wjcelcm34c"   class="head_search">
                <input id="key_words"  onkeydown="if (event.keyCode == 13) searchs('zh')" class="search-input" type="text" autocomplete="off" name="keywords" required="required" placeholder="Block,address,transaction,news" value="">
                <a href="javascript:;" title="搜索"  onclick="searchs('zh')"><img src="/static/imghw/find.png" alt="搜索"></a>
            </div>
                <div   id="wjcelcm34c"   class="head_right">
            <div   id="wjcelcm34c"   class="haed_language">
                <a href="javascript:;" class="layui-btn haed_language_btn">簡體中文<i class="layui-icon layui-icon-triangle-d"></i></a>
                <div   class="wjcelcm34c"   id="dropdown-template" style="display: none;">
                    <div   id="wjcelcm34c"   class="languagechoose">
                                                <a href="javascript:;" title="簡體中文" class="languagechoosea">簡體中文</a>
                                                <a href="javascript:setlang('en');" title="English" class="languagechoosea">English</a>
                                                <a href="javascript:setlang('zh-tw');" title="繁體中文" class="languagechoosea">繁體中文</a>
                                                <a href="javascript:setlang('ja');" title="日本語" class="languagechoosea">日本語</a>
                                                <a href="javascript:setlang('ko');" title="???" class="languagechoosea">???</a>
                                                <a href="javascript:setlang('ms');" title="Melayu" class="languagechoosea">Melayu</a>
                                                <a href="javascript:setlang('fr');" title="Fran?ais" class="languagechoosea">Fran?ais</a>
                                                <a href="javascript:setlang('de');" title="Deutsch" class="languagechoosea">Deutsch</a>
                                            </div>
                </div>
            </div>
            <span id="wjcelcm34c"    class="head_right_line"></span>
                            <div style="display: block;" id="login" class="haed_login ">
                    <a href="javascript:;"  title="Login" class="haed_logina ">Login</a>
                </div>
                <div style="display: block;" id="reg" class="head_signup login">
                    <a href="javascript:;"  title="singup" class="head_signupa">singup</a>
                </div>
            
        </div>
    </div>
</header>

	
	<main>
		<div   id="wjcelcm34c"   class="Article_Details_main">
			<div   id="wjcelcm34c"   class="Article_Details_main1">
							<div   id="wjcelcm34c"   class="Article_Details_main1M">
					<div   id="wjcelcm34c"   class="phpgenera_Details_mainL1">
						<a href="http://ipnx.cn/zh/" title="首頁"
							class="phpgenera_Details_mainL1a">首頁</a>
						<img src="/static/imghw/top_right.png" alt="" />
												<a href="http://ipnx.cn/zh/web-designer.html"
							class="phpgenera_Details_mainL1a">web前端</a>
						<img src="/static/imghw/top_right.png" alt="" />
												<a href="http://ipnx.cn/zh/css-tutorial.html"
							class="phpgenera_Details_mainL1a">css教程</a>
						<img src="/static/imghw/top_right.png" alt="" />
						<span>css中的margin屬性詳解</span>
					</div>
					
					<div   id="wjcelcm34c"   class="Articlelist_txts">
						<div   id="wjcelcm34c"   class="Articlelist_txts_info">
							<h1 class="Articlelist_txts_title">css中的margin屬性詳解</h1>
							<div   id="wjcelcm34c"   class="Articlelist_txts_info_head">
								<div   id="wjcelcm34c"   class="author_info">
									<a href="http://ipnx.cn/zh/member/61021.html"  class="author_avatar">
									<img class="lazy"  data-src="/static/images/user_avatar.jpg" src="/static/imghw/default1.png" alt="php中世界最好的語言">
									</a>
									<div   id="wjcelcm34c"   class="author_detail">
																			<a href="http://ipnx.cn/zh/member/61021.html" class="author_name">php中世界最好的語言</a>
                                										</div>
								</div>
                			</div>
							<span id="wjcelcm34c"    class="Articlelist_txts_time">Mar 22, 2018 pm	 04:55 PM</span>
															<div   id="wjcelcm34c"   class="Articlelist_txts_infos">
																			<span id="wjcelcm34c"    class="Articlelist_txts_infoss on">css</span>
																			<span id="wjcelcm34c"    class="Articlelist_txts_infoss ">margin</span>
																	</div>
														
						</div>
					</div>
					<hr />
					<div   id="wjcelcm34c"   class="article_main php-article">
						<div   id="wjcelcm34c"   class="article-list-left detail-content-wrap content">
						<ins class="adsbygoogle"
							style="display:block; text-align:center;"
							data-ad-layout="in-article"
							data-ad-format="fluid"
							data-ad-client="ca-pub-5902227090019525"
							data-ad-slot="3461856641">
						</ins>
						

					<p style="text-align: left;">這次給大家?guī)韈ss中的margin屬性詳解,使用css中margin的<a href="http://ipnx.cn/code/10182.html" target="_blank">注意事項</a>有哪些,下面就是實戰(zhàn)案例,一起來看一下。</p>
<p style="text-align: left;">之前我一直認(rèn)為margin屬性是一個非常簡單的屬性,但是最近做項目時遇到了一些問題,才發(fā)現(xiàn)margin屬性還是有一些“坑”的,下面我會介紹margin的基本知識以及那些“坑”。這篇博文主要分為以下幾個部分:</p>
<ul class=" list-paddingleft-2">
<li><p style="text-align: left;">margin--基礎(chǔ)知識</p></li>
<li><p style="text-align: left;">margin--在同級元素(非父子關(guān)系)之間應(yīng)用</p></li>
<li><p style="text-align: left;">margin--在父元素和子元素之間應(yīng)用(重點)</p></li>
<li><p style="text-align: left;">margin--margin值的單位為%時的幾種情況??</p></li>
</ul>
<p style="text-align: left;"><strong>第一部分:margin--基礎(chǔ)知識</strong></p>
<p style="text-align: left;">要介紹margin的基礎(chǔ)知識,我們不可回避地要談到<a href="http://ipnx.cn/code/790.html" target="_blank">css盒子模型</a>(Box Model),一般而言,css盒子模型是用來設(shè)計和布局的。它本質(zhì)上是一個盒子,包括:外邊距(margin)、邊框(border)、內(nèi)邊距(padding)以及最中間的內(nèi)容(content)。下圖即為盒子模型(這里只談W3C規(guī)范的標(biāo)準(zhǔn)盒模型,而不談IE5和IE6在怪異模式中使用的非標(biāo)準(zhǔn)的盒子模型):</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/b5678c0ec7d8826678f9fbf62431a624-0.png" class="lazy" alt=""></p>
<p style="text-align: left;">?我們要介紹的margin在最外層,因為margin(外邊距)一定是透明的,所以它可以用來使得不同的盒子之間留有一定的間隙從而達(dá)到布局美觀等效果。從上面的盒子模型中我們可以看到,margin在四周均存在,我們可以使用margin-top、<a href="http://ipnx.cn/wiki/934.html" target="_blank">margin-right</a>、<a href="http://ipnx.cn/wiki/935.html" target="_blank">margin-bottom</a>、margin-left分別設(shè)置這四個方向的margin值。(注:由于這部分知識較為基礎(chǔ),所以我不再在這部分不做更多介紹)</p>
<p style="text-align: left;"><strong>?第二部分:margin--在同級元素(非父子關(guān)系)之間應(yīng)用</strong></p>
<p style="text-align: left;">這一部分主要介紹水平方向和豎直方向的外邊距的合并問題。</p>
<p style="text-align: left;"><strong>(1)水平方向的外邊距合并</strong></p>
<p style="text-align: left;">兩個水平方向的盒子相遇,那么最終兩者之間的距離為左邊盒子的右外邊距和右邊盒子的做外邊距之和。</p>
<p style="text-align: left;">?例1:</p>
<p style="text-align: left;">代碼如下:</p>
<pre class="brush:php;toolbar:false"><!DOCTYPE html>
<html lang="en">
<head>
????<meta charset="UTF-8">
????<title>水平方向的兩個盒子</title>
????<style>
????????*{
????????????margin:0;
????????????padding:0;
????????????border:0;
????????}
????????body{
????????????font-size:?0;
????????}
????????.left{
????????????width:?100px;
????????????height:?100px;
????????????background:?red;
????????????display:?inline-block;
????????????margin-right:?50px;
????????????font-size:?20px;
????????}
????????.right{
????????????width:?100px;
????????????height:?100px;
????????????background:?yellow;
????????????display:?inline-block;
????????????margin-left:?50px;
????????????font-size:?20px;
????????}
????</style>
</head>
<body>
????<p class="left">寬為100px,右邊距為50px</p>
????<p class="right">寬為100px,左邊距為50px</p>
</body>
</html></pre>
<p style="text-align: left;">效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/b5678c0ec7d8826678f9fbf62431a624-1.png" class="lazy" alt=""></p>
<p style="text-align: left;">這時兩者之間的距離剛好為100px。</p>
<p style="text-align: left;"><strong><span style="color:#ff0000">補(bǔ)充說明:</span></strong>大家可以看到,為了使得兩個p(塊狀元素)脫離正常的文檔流我使用了display:inline-block;屬性,另外,我還把body的font-size設(shè)置為0,這樣可以解決inline-block自身的問題,否則兩個p的舉例會大于100px。當(dāng)然使用float也可以使得兩個p出現(xiàn)在同一行中。</p>
<p style="text-align: left;"><strong>(2)豎直方向的外邊距合并</strong></p>
<p style="text-align: left;">兩個豎直方向的盒子相遇時,其豎直方向的距離等于上方盒子的下外邊距和下方盒子的上外邊距中較大的一個。</p>
<p style="text-align: left;">例2:</p>
<pre class="brush:php;toolbar:false"><!DOCTYPE html>
<html lang="en">
<head>
????<meta charset="UTF-8">
????<title>水平方向的兩個盒子</title>
????<style>
????????*{
????????????margin:0;
????????????padding:0;
????????????border:0;
????????}
????????.top{
????????????width:?100px;
????????????height:?100px;
????????????margin-bottom:?100px;
????????????background:?red;
????????}
????????.bottom{
????????????width:?100px;
????????????height:?100px;
????????????margin-top:?50px;
????????????background:?green;
????????}
????</style>
</head>
<body>
????<p class="top">高為100px,下邊距為100px</p>
????<p class="bottom">高為100px,上邊距為50px</p>
</body>
</html></pre>
<p style="text-align: left;">效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/317e3e4cdee475e69aec60cf01f8b4b2-2.png" class="lazy" alt=""></p>
<p style="text-align: left;">這時我們?nèi)庋鄱伎梢杂^察出來,兩者豎直方向的舉例大約為100px(實際就是100px)而非100+50=150px;這正是因為兩個豎直方向的盒子相遇時,其豎直方向的距離等于上方盒子的下外邊距和下方盒子的上外邊距中較大的一個。</p>
<p style="text-align: left;">另外一個有趣的例子就是:假設(shè)有一個元素同時設(shè)置了margin-top和margin-bottom,但是內(nèi)容為空,那么這兩個margin值也會疊加,值為兩者最大的一個,它類似與豎直方向上兩個盒子margin值的疊加。代碼如下:</p>
<pre class="brush:php;toolbar:false"><!DOCTYPE html>
<html lang="en">
<head>
????<meta charset="UTF-8">
????<title>水平方向的兩個盒子</title>
????<style>
????????*{
????????????margin:0;
????????????padding:0;
????????}
?
????????.top{
????????????width:?500px;
????????????height:?100px;
????????????background:?red;
????????}
????????.middle{
????????????margin-top:?100px;
????????????margin-bottom:50px;
????????}
????????.footer{
????????????width:?500px;
????????????height:?100px;
????????????background:?green;
????????}
?
????</style>
</head>
<body>
????<p class="top">上面的p,高100px</p>
????<p class="middle"></p>
????<p class="footer">下面的p,高100px</p>
</body>
</html></pre>
<p style="text-align: left;">最終的效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/317e3e4cdee475e69aec60cf01f8b4b2-3.png" class="lazy" alt=""></p>
<p style="text-align: left;">我們發(fā)現(xiàn)這時在上面的p和在下面的p之間的舉例并不是100+50=150px,而是兩者中的最大者,即100px。</p>
<p style="text-align: left;">那么W3C為什么會設(shè)定這樣的標(biāo)準(zhǔn)而不設(shè)定和水平方向一樣的標(biāo)準(zhǔn)呢?即margin值的疊加,實際上這也是有一定的道理的。比如我們需要設(shè)計一個由若干個段落構(gòu)成的一個頁面。我們需要設(shè)置margin-top和margin-bottom使得第一段和頁面的最上方有一段距離,使得最后一段和最下方有一段距離。下面是不疊加和疊加的效果圖:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/317e3e4cdee475e69aec60cf01f8b4b2-4.png" class="lazy" alt=""></p>
<p style="text-align: left;">我們可以看到左邊的頁面沒有重疊,那么兩個段落之間的舉例就是最上方的兩倍間距了,而右邊的頁面發(fā)生了重疊,則所有的間距都是相等的。或許這就是這樣設(shè)定標(biāo)準(zhǔn)的目的吧,誰知道呢?</p>
<p style="text-align: left;"><strong>第三部分:margin--在父元素和子元素之間應(yīng)用(重點)</strong></p>
<p style="text-align: left;">第二部分介紹了同級元素之間使用margin,而這一部分將要介紹最有意思的父元素和子元素之間margin的應(yīng)用。這一部分,我們同樣從兩個方面來討論。一方面是子元素設(shè)置水平方向上的margin值,另一方面是子元素設(shè)置豎直方向的margin值。</p>
<p style="text-align: left;">(1)在子元素中設(shè)置水平方向的margin值</p>
<p style="text-align: left;">我們可以設(shè)置margin-left來控制子元素的左邊框和父元素的左邊框之間的舉例。</p>
<p style="text-align: left;">例3:</p>
<pre class="brush:php;toolbar:false"><!DOCTYPE html>
<html lang="en">
<head>
????<meta charset="UTF-8">
????<title>margin</title>
????<style>
????????*{padding:0;?margin:0;?border:0;}
????????.father{
????????????width:?500px;
????????????height:?500px;
????????????background:?red;
????????}
????????.son{
????????????width:?100px;
????????????height:?100px;
????????????background:?green;
????????????margin-left:?100px;
????????}
????</style>
</head>
<body>
????<p class="father">
????????<p class="son">寬度為100px,margin-left為100px。</p>
????</p>
</body>
</html></pre>
<p style="text-align: left;">我將子元素的margin-left設(shè)置為了100px;效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/317e3e4cdee475e69aec60cf01f8b4b2-5.png" class="lazy" alt=""    style="max-width:90%"  style="max-width:90%"></p>
<p style="text-align: left;">即子元素的左邊框和父元素的左邊框之間的距離為100px。與在同級元素之間設(shè)置margin不同,因為同級元素之間的margin不會考慮到padding,但是在父元素和子元素就不同了,那么如果父元素中如果有padding,效果會是什么樣的呢?請看下面一個例子:</p>
<p style="text-align: left;">例4:</p>
<p style="text-align: left;">下面我們在上面例子的基礎(chǔ)上給父元素添加padding值。</p>
<pre class="brush:php;toolbar:false"><!DOCTYPE html>
<html lang="en">
<head>
????<meta charset="UTF-8">
????<title>margin</title>
????<style>
????????*{padding:0;?margin:0;?border:0;}
????????.father{
????????????width:?500px;
????????????height:?500px;
????????????padding:100px;
????????????background:?red;
????????}
????????.son{
????????????width:?100px;
????????????height:?100px;
????????????background:?green;
????????????margin-left:?100px;
????????}
????</style>
</head>
<body>
????<p class="father">
????????<p class="son">寬度為100px,margin-left為100px。</p>
????</p>
</body>
</html></pre>
<p style="text-align: left;">上面的代碼給父元素添加了100px的padding值,效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/a7832a4c683024af2d836c39cf9bd1a7-6.png" class="lazy" alt=""></p>
<p style="text-align: left;">我們可以看到子元素舉例上方的距離為100px,因為子元素一定是在父元素的content的部分的,這點毫無疑問。</p>
<p style="text-align: left;">但是經(jīng)過測量可以發(fā)現(xiàn)子元素的左邊框距離父元素的左邊框之間的距離為200px,因為其中還有100px的左padding值,前面的例子因為我沒有設(shè)置padding值,所以沒有觀察出來,因此這就說明了在子元素中設(shè)置margin-left,其值實際上是子元素的左邊框距離父元素左padding內(nèi)側(cè)的距離。</p>
<p style="text-align: left;">例5:margin-right的使用和margin-left的使用是相似的,我在這里只舉一個例子。</p>
<p style="text-align: left;">這個例子在子元素中設(shè)置了margin-right值,如下所示:</p>
<pre class="brush:php;toolbar:false"><!DOCTYPE html>
<html lang="en">
<head>
????<meta charset="UTF-8">
????<title>margin</title>
????<style>
????????*{padding:0;?margin:0;?border:0;}
????????.father{
????????????width:?500px;
????????????height:?500px;
????????????padding:100px;
????????????background:?red;
????????}
????????.son{
????????????float:?right;
????????????width:?100px;
????????????height:?100px;
????????????background:?green;
????????????margin-right:?100px;
????????}
????</style>
</head>
<body>
????<p class="father">
????????<p class="son">寬度為100px,margin-right為100px。</p>
????</p>
</body>
</html></pre>
<p style="text-align: left;">這個例子與例4的區(qū)別僅在與子元素的位置不同。效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/a7832a4c683024af2d836c39cf9bd1a7-7.png" class="lazy" alt=""></p>
<p style="text-align: left;">通過這個例子可以說明margin-right的值是子元素的右邊框和父元素的右padding內(nèi)側(cè)的距離。只是前面的幾個例子我沒有使用padding,所以無法觀察出來。</p>
<p style="text-align: left;">(2)在子元素中設(shè)置豎直方向的margin值</p>
<p style="text-align: left;">按照前面的經(jīng)驗,理論上來說,我們同樣可以通過設(shè)置margin-top的值使得子元素的上邊框和父元素的上padding的內(nèi)側(cè)留有一定的距離。那么我們就試試吧!</p>
<p style="text-align: left;">例6:</p>
<pre class="brush:php;toolbar:false"><!DOCTYPE html>
<html lang="en">
<head>
????<meta charset="UTF-8">
????<title>margin</title>
????<style>
????????*{padding:0;?margin:0;?border:0;}
????????.father{
????????????width:?500px;
????????????height:?500px;
????????????background:?red;
????????}
????????.son{
????????????width:?100px;
????????????height:?100px;
????????????background:?green;
????????????margin-top:?100px;
????????}
????</style>
</head>
<body>
????<p class="father">
????????<p class="son">高度為100px,margin-top為100px。</p>
????</p>
</body>
</html></pre>
<p style="text-align: left;">這個例子我設(shè)置了margin-top為100px,效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/a7832a4c683024af2d836c39cf9bd1a7-8.png" class="lazy" alt=""></p>
<p style="text-align: left;">這并不是我們想要的效果啊,我們希望子元素的上部距離父元素的上部為100px,可是我們看到的卻是父元素的上部距離瀏覽器頁面的上部有100px的距離,這是為什么呢?哪里出現(xiàn)問題了呢?</p>
<p style="text-align: left;">實際上這是因為當(dāng)父元素沒有設(shè)置padding值以及border值時,出現(xiàn)了一個bug--父元素的上方與子元素的上方完全重合在了一起,無法分開。所以才會導(dǎo)致上述這種父元素和子元素同時向下的情況。</p>
<p style="text-align: left;">對于這種問題解決方法有下面幾種:</p>
<ul class=" list-paddingleft-2">
<li><p style="text-align: left;">方法一:給父元素添加padding-top值</p></li>
<li><p style="text-align: left;">方法二:給父元素添加border值</p></li>
<li><p style="text-align: left;">方法三:給父元素添加屬性overflow:hidden;</p></li>
<li><p style="text-align: left;">方法四:給父元素或者子元素聲明浮動float</p></li>
<li><p style="text-align: left;">方法五:使父元素或子元素聲明為<a href="http://ipnx.cn/code/6074.html" target="_blank">絕對定位</a>:position:absolute;</p></li>
<li><p style="text-align: left;">方法六:給父元素添加屬性 overflow:auto; positon:relative;(注:此方法為后續(xù)添加,感謝博友@小精靈Pawn提供此方法)</p></li>
</ul>
<p style="text-align: left;">方法一:基于例6,在父元素的css代碼中添加padding-top:1px;效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/0ccfb338b8fe6d678c4b327eee5d5aa0-9.png" class="lazy" alt=""></p>
<p style="text-align: left;">?方法的唯一缺點就是增加了1px的誤差。</p>
<p style="text-align: left;">方法二:基于例6,在父元素的css代碼中添加border-top:1px solid transparent;效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/0ccfb338b8fe6d678c4b327eee5d5aa0-10.png" class="lazy" alt=""></p>
<p style="text-align:left;">同樣達(dá)到了效果, 缺點同方法一。</p>
<p style="text-align: left;">方法三:基于例6,在父元素的css代碼中添加overflow:hidden;效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/0ccfb338b8fe6d678c4b327eee5d5aa0-11.png" class="lazy" alt=""></p>
<p style="text-align: left;">同樣達(dá)到了效果,并且沒有任何誤差的存在??胺Qperfect!!!!</p>
<p style="text-align: left;">方法四:給父元素或者子元素聲明float;基于例6,在子元素css代碼添加float:left;或者在父元素css代碼添加float:left;均達(dá)到效果,這里不再展示相同的圖片。</p>
<p style="text-align: left;">優(yōu)點:沒有像素的誤差。?? 缺點:float有時是不必要的。</p>
<p style="text-align: left;">方法五:給父元素或者子元素添加position:absolute;屬性。 同樣達(dá)到效果。</p>
<p style="text-align: left;">優(yōu)點:同方法四。? 且只要我們不使用top和left也不會有任何影響,所以這也是一種不錯的方法。</p>
<p style="text-align: left;">方法六:給父元素添加overflow:auto;和position:relative;同樣達(dá)到效果。</p>
<p style="text-align: left;"><strong>第四部分:margin值的單位為%時的幾種情況</strong></p>
<p style="text-align: left;">之前我舉例子時使用margin,它的值都是以px為單位的,這個理解起來沒有問題。但是如果margin值是以%為單位呢?實際上這時候百分比(%)是相對于該元素的父元素(容器),對于同級元素和父子元素都是如此。(再次感謝 博友@小精靈Pawn 提供的建議?。』诖私ㄗh補(bǔ)充這部分內(nèi)容) 但是在同級元素中使用豎直方向的margin時會出現(xiàn)意想不到的結(jié)果,下面舉例說明。</p>
<p style="text-align: left;">(1)同級元素在水平方向使用值為%的margin</p>
<p style="text-align: left;">例7:</p>
<pre class="brush:php;toolbar:false"><head>
????<meta charset="UTF-8">
????<title>margin</title>
????<style>
????????*{
????????????margin:0;
????????????padding:0;
????????}
????????.first{
????????????float:?left;
????????????width:?200px;
????????????height:?200px;
????????????background:?green;
????????}
????????.second{
????????????float:?left;
????????????width:?200px;
????????????height:?200px;
????????????background:?red;
????????????margin-left:?20%;
????????}
????</style>
</head>
<body>
????<p class="first">寬為200,無margin</p>
????<p class="second">寬為200,margin-left為20%;</p>
</body>
</html></pre>
<p style="text-align: left;">這個例子中,設(shè)置兩個元素向左浮動,以便于觀察兩者水平方向的margin。其中左邊p無margin,右邊p的margin-left為20%,效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/4afda3ae098dfd1684f0b60d5616ac9d-12.gif" class="lazy" alt=""></p>
<p style="text-align: left;">從效果圖可以看出兩個p之間的間距始終為父元素(這里右邊p的父元素即為body,其寬度為瀏覽器寬度)的20%。</p>
<p style="text-align: left;">(2)同級元素在豎直方向使用值為%的margin</p>
<p style="text-align: left;">根據(jù)例7的啟發(fā),我們可以猜想,如果在豎直方向上使用margin,且值的單位為%,那么最終兩者之間的距離將是父元素(上例中為body)的百分?jǐn)?shù)。那么究竟是不是這樣呢?看下面的例子。</p>
<p style="text-align: left;">例8</p>
<pre class="brush:php;toolbar:false"><head>
????<meta charset="UTF-8">
????<title>margin</title>
????<style>
????????*{
????????????margin:0;
????????????padding:0;
????????}
????????.first{
????????????width:?200px;
????????????height:?200px;
????????????background:?green;
????????}
????????.second{
????????????width:?200px;
????????????height:?200px;
????????????background:?red;
????????????margin-top:?10%;
????????}
????</style>
</head>
<body>
????<p class="first">高為200,無margin</p>
????<p class="second">高為200,margin-top為20%;</p>
</body>
</html></pre>
<p style="text-align: left;">這里設(shè)置上面的p無margin,下面的p的margin-top為10。效果如下:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/0ee5c0d9b67d186cbf21ca067a244297-13.gif" class="lazy" alt=""></p>
<p style="text-align: left;">我們發(fā)現(xiàn),當(dāng)我在縮小瀏覽器的高度時,豎直方向上的間距并沒有縮?。。。?而當(dāng)我縮小瀏覽器的寬度時,豎直方向上的距離縮小了?。?!</p>
<p style="text-align: left;">這就說明:統(tǒng)計元素之間在豎直方向上使用margin,當(dāng)值的單位為%時,它是相對于父元素的寬度。</p>
<p style="text-align: left;">那么這里為什么不是如我們所希望的那樣相對于瀏覽器的高度呢?知乎上有大神是這樣解釋的:</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/5907097efe49c76cc8fc447782c27c1c-14.png" class="lazy" alt=""    style="max-width:90%"  style="max-width:90%"></p>
<p style="text-align: left;">(3)父子元素使用值為%的margin</p>
<p style="text-align: left;">對于父子元素,如果在子元素中使用單位為%margin,那么這個margin值是相對于父元素的寬度和高度(注意:這時的確是相對于父元素的高度?。┑?。</p>
<p style="text-align: left;">例9?</p>
<p style="text-align: left;">代碼如下:</p>
<pre class="brush:php;toolbar:false"><!DOCTYPE html>
<html lang="en">
<head>
????<meta charset="UTF-8">
????<title>Document</title>
????<style>
????????*{
????????????margin:0;
????????????padding:0;
????????}
????????.father{
????????????width:?500px;
????????????height:?300px;
????????????background:?red;
????????????overflow:?hidden;
????????}
????????.son{
????????????width:?100px;
????????????height:?100px;
????????????background:?green;
????????????margin-top:?20%;
????????????margin-left:?20%;
????????}
????</style>
</head>
<body>
????<p class="father">
????????<p class="son"></p>
????</p>
</body>
</html></pre>
<p style="text-align: left;">在這個例子中,我設(shè)置了margin-left的值為20%,margin-top的值為20%,父元素的width為500px,父元素的height為300px。下面看看效果吧。</p>
<p style="text-align: left;"><img src="/static/imghw/default1.png" data-src="https://img.php.cn/upload/article/000/061/021/0d09889803d6fef7ca5c780770b40238-15.png" class="lazy" alt=""    style="max-width:90%"  style="max-width:90%"></p>
<p style="text-align: left;">從上圖可以看出子元素的margin-top值最終同樣是相對與父元素的寬度而非高度。</p>
<p style="text-align: left;"><strong>總結(jié):</strong></p>
<p style="text-align: left;">這篇博文只介紹了margin的其中一部分知識點,但如果大家讀后能有些許收獲是再好不過的了。由于本人總結(jié)倉促,知識不足,錯誤在所難免,希望大家如果發(fā)現(xiàn)不妥之初能提出意見,我將非常感激。</p>
<p style="text-align: left;">相信看了本文案例你已經(jīng)掌握了方法,更多精彩請關(guān)注php中文網(wǎng)其它相關(guān)文章!</p>
<p style="text-align: left;">推薦閱讀:</p>
<p style="text-align: left;"><a href="http://ipnx.cn/css-tutorial-389742.html" target="_blank">CSS3的box-sizing屬性圖文教程</a></p>
<p style="text-align: left;"><a href="http://ipnx.cn/css-tutorial-389740.html" target="_blank">在CSS邊界線消失如何處理</a></p>
<p style="text-align: left;"><a href="http://ipnx.cn/css-tutorial-389730.html" target="_blank">三種絕對定位元素的水平垂直居中的辦法</a></p><p>以上是css中的margin屬性詳解的詳細(xì)內(nèi)容。更多信息請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!</p>


						</div>
					</div>
					<div   id="wjcelcm34c"   class="wzconShengming_sp">
						<div   id="wjcelcm34c"   class="bzsmdiv_sp">本站聲明</div>
						<div>本文內(nèi)容由網(wǎng)友自發(fā)貢獻(xiàn),版權(quán)歸原作者所有,本站不承擔(dān)相應(yīng)法律責(zé)任。如您發(fā)現(xiàn)有涉嫌抄襲侵權(quán)的內(nèi)容,請聯(lián)系admin@php.cn</div>
					</div>
				</div>

				<ins class="adsbygoogle"
     style="display:block"
     data-ad-format="autorelaxed"
     data-ad-client="ca-pub-5902227090019525"
     data-ad-slot="2507867629"></ins>



				<div   id="wjcelcm34c"   class="AI_ToolDetails_main4sR">


				<ins class="adsbygoogle"
        style="display:block"
        data-ad-client="ca-pub-5902227090019525"
        data-ad-slot="3653428331"
        data-ad-format="auto"
        data-full-width-responsive="true"></ins>
    


					<!-- <div   id="wjcelcm34c"   class="phpgenera_Details_mainR4">
						<div   id="wjcelcm34c"   class="phpmain1_4R_readrank">
							<div   id="wjcelcm34c"   class="phpmain1_4R_readrank_top">
								<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									src="/static/imghw/hotarticle2.png" alt="" />
								<h2>熱門文章</h2>
							</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottom">
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/1796832397.html" title="Grass Wonder Build Guide |烏瑪媽媽漂亮的德比" class="phpgenera_Details_mainR4_bottom_title">Grass Wonder Build Guide |烏瑪媽媽漂亮的德比</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>4 周前</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/1796833110.html" title="<??>:在森林里99夜 - 所有徽章以及如何解鎖" class="phpgenera_Details_mainR4_bottom_title"><??>:在森林里99夜 - 所有徽章以及如何解鎖</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>3 周前</span>
										<span>By DDD</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/1796831605.html" title="烏瑪?shù)姆劢z漂亮的德比橫幅日程(2025年7月)" class="phpgenera_Details_mainR4_bottom_title">烏瑪?shù)姆劢z漂亮的德比橫幅日程(2025年7月)</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>4 周前</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/1796836699.html" title="Rimworld Odyssey溫度指南和Gravtech" class="phpgenera_Details_mainR4_bottom_title">Rimworld Odyssey溫度指南和Gravtech</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>3 周前</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/1796831905.html" title="Windows安全是空白或不顯示選項" class="phpgenera_Details_mainR4_bottom_title">Windows安全是空白或不顯示選項</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>4 周前</span>
										<span>By 下次還敢</span>
									</div>
								</div>
														</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_more">
								<a href="http://ipnx.cn/zh/article.html">顯示更多</a>
							</div>
						</div>
					</div> -->


											<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3">
							<div   id="wjcelcm34c"   class="phpmain1_4R_readrank">
								<div   id="wjcelcm34c"   class="phpmain1_4R_readrank_top">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/hottools2.png" alt="" />
									<h2>熱AI工具</h2>
								</div>
								<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_bottom">
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/zh/ai/undress-ai-tool" title="Undress AI Tool" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/ai_manual/001/246/273/173410641626608.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Undress AI Tool" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/zh/ai/undress-ai-tool" title="Undress AI Tool" class="phpmain_tab2_mids_title">
													<h3>Undress AI Tool</h3>
												</a>
												<p>免費(fèi)脫衣服圖片</p>
											</div>
										</div>
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/zh/ai/undresserai-undress" title="Undresser.AI Undress" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411540686492.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Undresser.AI Undress" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/zh/ai/undresserai-undress" title="Undresser.AI Undress" class="phpmain_tab2_mids_title">
													<h3>Undresser.AI Undress</h3>
												</a>
												<p>人工智能驅(qū)動的應(yīng)用程序,用于創(chuàng)建逼真的裸體照片</p>
											</div>
										</div>
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/zh/ai/ai-clothes-remover" title="AI Clothes Remover" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411552797167.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="AI Clothes Remover" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/zh/ai/ai-clothes-remover" title="AI Clothes Remover" class="phpmain_tab2_mids_title">
													<h3>AI Clothes Remover</h3>
												</a>
												<p>用于從照片中去除衣服的在線人工智能工具。</p>
											</div>
										</div>
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/zh/ai/clothoffio" title="Clothoff.io" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/ai_manual/001/246/273/173411529149311.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Clothoff.io" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/zh/ai/clothoffio" title="Clothoff.io" class="phpmain_tab2_mids_title">
													<h3>Clothoff.io</h3>
												</a>
												<p>AI脫衣機(jī)</p>
											</div>
										</div>
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/zh/ai/video-swap" title="Video Face Swap" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/ai_manual/001/246/273/173414504068133.jpg?x-oss-process=image/resize,m_fill,h_50,w_50" src="/static/imghw/default1.png" alt="Video Face Swap" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/zh/ai/video-swap" title="Video Face Swap" class="phpmain_tab2_mids_title">
													<h3>Video Face Swap</h3>
												</a>
												<p>使用我們完全免費(fèi)的人工智能換臉工具輕松在任何視頻中換臉!</p>
											</div>
										</div>
																</div>
								<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_more">
									<a href="http://ipnx.cn/zh/ai">顯示更多</a>
								</div>
							</div>
						</div>
					


					<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4">
						<div   id="wjcelcm34c"   class="phpmain1_4R_readrank">
							<div   id="wjcelcm34c"   class="phpmain1_4R_readrank_top">
								<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									src="/static/imghw/hotarticle2.png" alt="" />
								<h2>熱門文章</h2>
							</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottom">
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/1796832397.html" title="Grass Wonder Build Guide |烏瑪媽媽漂亮的德比" class="phpgenera_Details_mainR4_bottom_title">Grass Wonder Build Guide |烏瑪媽媽漂亮的德比</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>4 周前</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/1796833110.html" title="<??>:在森林里99夜 - 所有徽章以及如何解鎖" class="phpgenera_Details_mainR4_bottom_title"><??>:在森林里99夜 - 所有徽章以及如何解鎖</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>3 周前</span>
										<span>By DDD</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/1796831605.html" title="烏瑪?shù)姆劢z漂亮的德比橫幅日程(2025年7月)" class="phpgenera_Details_mainR4_bottom_title">烏瑪?shù)姆劢z漂亮的德比橫幅日程(2025年7月)</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>4 周前</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/1796836699.html" title="Rimworld Odyssey溫度指南和Gravtech" class="phpgenera_Details_mainR4_bottom_title">Rimworld Odyssey溫度指南和Gravtech</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>3 周前</span>
										<span>By Jack chen</span>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/1796831905.html" title="Windows安全是空白或不顯示選項" class="phpgenera_Details_mainR4_bottom_title">Windows安全是空白或不顯示選項</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<span>4 周前</span>
										<span>By 下次還敢</span>
									</div>
								</div>
														</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_more">
								<a href="http://ipnx.cn/zh/article.html">顯示更多</a>
							</div>
						</div>
					</div>


											<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3">
							<div   id="wjcelcm34c"   class="phpmain1_4R_readrank">
								<div   id="wjcelcm34c"   class="phpmain1_4R_readrank_top">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/hottools2.png" alt="" />
									<h2>熱工具</h2>
								</div>
								<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_bottom">
																		<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/zh/toolset/development-tools/92" title="記事本++7.3.1" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/manual/000/000/001/58ab96f0f39f7357.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="記事本++7.3.1" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/zh/toolset/development-tools/92" title="記事本++7.3.1" class="phpmain_tab2_mids_title">
													<h3>記事本++7.3.1</h3>
												</a>
												<p>好用且免費(fèi)的代碼編輯器</p>
											</div>
										</div>
																			<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/zh/toolset/development-tools/93" title="SublimeText3漢化版" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/manual/000/000/001/58ab97a3baad9677.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="SublimeText3漢化版" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/zh/toolset/development-tools/93" title="SublimeText3漢化版" class="phpmain_tab2_mids_title">
													<h3>SublimeText3漢化版</h3>
												</a>
												<p>中文版,非常好用</p>
											</div>
										</div>
																			<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/zh/toolset/development-tools/121" title="禪工作室 13.0.1" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/manual/000/000/001/58ab97ecd1ab2670.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="禪工作室 13.0.1" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/zh/toolset/development-tools/121" title="禪工作室 13.0.1" class="phpmain_tab2_mids_title">
													<h3>禪工作室 13.0.1</h3>
												</a>
												<p>功能強(qiáng)大的PHP集成開發(fā)環(huán)境</p>
											</div>
										</div>
																			<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/zh/toolset/development-tools/469" title="Dreamweaver CS6" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/manual/000/000/001/58d0e0fc74683535.jpg?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="Dreamweaver CS6" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/zh/toolset/development-tools/469" title="Dreamweaver CS6" class="phpmain_tab2_mids_title">
													<h3>Dreamweaver CS6</h3>
												</a>
												<p>視覺化網(wǎng)頁開發(fā)工具</p>
											</div>
										</div>
																			<div   id="wjcelcm34c"   class="phpmain_tab2_mids_top">
											<a href="http://ipnx.cn/zh/toolset/development-tools/500" title="SublimeText3 Mac版" class="phpmain_tab2_mids_top_img">
												<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
													class="lazy"  data-src="https://img.php.cn/upload/manual/000/000/001/58d34035e2757995.png?x-oss-process=image/resize,m_fill,h_50,w_72" src="/static/imghw/default1.png" alt="SublimeText3 Mac版" />
											</a>
											<div   id="wjcelcm34c"   class="phpmain_tab2_mids_info">
												<a href="http://ipnx.cn/zh/toolset/development-tools/500" title="SublimeText3 Mac版" class="phpmain_tab2_mids_title">
													<h3>SublimeText3 Mac版</h3>
												</a>
												<p>神級代碼編輯軟件(SublimeText3)</p>
											</div>
										</div>
																	</div>
								<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_more">
									<a href="http://ipnx.cn/zh/ai">顯示更多</a>
								</div>
							</div>
						</div>
										

					
					<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4">
						<div   id="wjcelcm34c"   class="phpmain1_4R_readrank">
							<div   id="wjcelcm34c"   class="phpmain1_4R_readrank_top">
								<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
									src="/static/imghw/hotarticle2.png" alt="" />
								<h2>熱門話題</h2>
							</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottom">
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/laravel-tutori" title="Laravel 教程" class="phpgenera_Details_mainR4_bottom_title">Laravel 教程</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/eyess.png" alt="" />
											<span>1597</span>
										</div>
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/tiezi.png" alt="" />
											<span>29</span>
										</div>
									</div>
								</div>
															<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms">
									<a href="http://ipnx.cn/zh/faq/php-tutorial" title="PHP教程" class="phpgenera_Details_mainR4_bottom_title">PHP教程</a>
									<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_info">
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/eyess.png" alt="" />
											<span>1488</span>
										</div>
										<div   id="wjcelcm34c"   class="phpgenera_Details_mainR4_bottoms_infos">
											<img src="/static/imghw/tiezi.png" alt="" />
											<span>72</span>
										</div>
									</div>
								</div>
														</div>
							<div   id="wjcelcm34c"   class="phpgenera_Details_mainR3_more">
								<a href="http://ipnx.cn/zh/faq/zt">顯示更多</a>
							</div>
						</div>
					</div>
				</div>
			</div>
							<div   id="wjcelcm34c"   class="Article_Details_main2">
					<div   id="wjcelcm34c"   class="phpgenera_Details_mainL4">
						<div   id="wjcelcm34c"   class="phpmain1_2_top">
							<a href="javascript:void(0);" class="phpmain1_2_top_title">Related knowledge<img
									src="/static/imghw/index2_title2.png" alt="" /></a>
						</div>
						<div   id="wjcelcm34c"   class="phpgenera_Details_mainL4_info">

													<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/zh/faq/1796847484.html" title="什么是常見的CSS瀏覽器不一致?" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/253/068/175348469118409.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="什么是常見的CSS瀏覽器不一致?" />
								</a>
								<a href="http://ipnx.cn/zh/faq/1796847484.html" title="什么是常見的CSS瀏覽器不一致?" class="phphistorical_Version2_mids_title">什么是常見的CSS瀏覽器不一致?</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 26, 2025 am	 07:04 AM</span>
								<p class="Articlelist_txts_p">不同瀏覽器對CSS解析存在差異,導(dǎo)致顯示效果不一致,主要包括默認(rèn)樣式差異、盒模型計算方式、Flexbox和Grid布局支持程度及某些CSS屬性行為不一致。1.默認(rèn)樣式處理不一致,解決方法是使用CSSReset或Normalize.css統(tǒng)一初始樣式;2.舊版IE的盒模型計算方式不同,建議統(tǒng)一使用box-sizing:border-box;3.Flexbox和Grid在邊緣情況或舊版本中表現(xiàn)有差異,應(yīng)多測試并使用Autoprefixer;4.某些CSS屬性行為不一致,需查閱CanIuse并提供降級</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/zh/faq/1796847548.html" title="描述'垂直align”屬性及其典型用例" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/253/068/175348653092370.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="描述'垂直align”屬性及其典型用例" />
								</a>
								<a href="http://ipnx.cn/zh/faq/1796847548.html" title="描述'垂直align”屬性及其典型用例" class="phphistorical_Version2_mids_title">描述'垂直align”屬性及其典型用例</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 26, 2025 am	 07:35 AM</span>
								<p class="Articlelist_txts_p">1.ItAdjustSelementsLikeImagesRikeImagesOrformInputswithIntExtLineSustLineSlineSlineSlineSlikeLikeLikeBaseline,中間,Super,Super,Super和Sub.2.intablebecells,ItControlScontentalStalteNtalmscontentalMedwithThtop,Middle,Middle,Midder,Midder,經(jīng)常</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/zh/faq/1796847751.html" title="什么是口音色的物業(yè)?" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/431/639/175349310138463.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="什么是口音色的物業(yè)?" />
								</a>
								<a href="http://ipnx.cn/zh/faq/1796847751.html" title="什么是口音色的物業(yè)?" class="phphistorical_Version2_mids_title">什么是口音色的物業(yè)?</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 26, 2025 am	 09:25 AM</span>
								<p class="Articlelist_txts_p">accent-color是CSS中用于自定義復(fù)選框、單選按鈕和滑塊等表單元素高亮顏色的屬性;1.它直接改變表單控件選中狀態(tài)的默認(rèn)顏色,如將復(fù)選框的藍(lán)色勾選標(biāo)記改為紅色;2.支持的元素包括type="checkbox"、type="radio"和type="range"的輸入框;3.使用accent-color可避免復(fù)雜的自定義樣式和額外DOM結(jié)構(gòu),保持原生可訪問性;4.現(xiàn)代瀏覽器普遍支持,舊瀏覽器需降級處理;5.設(shè)置accent-col</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/zh/faq/1796848248.html" title="如何將SCSS編譯到CSS?" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/431/639/175355269194613.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="如何將SCSS編譯到CSS?" />
								</a>
								<a href="http://ipnx.cn/zh/faq/1796848248.html" title="如何將SCSS編譯到CSS?" class="phphistorical_Version2_mids_title">如何將SCSS編譯到CSS?</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 27, 2025 am	 01:58 AM</span>
								<p class="Articlelist_txts_p">installdartsassvianpmafterinstallingnode.jsusingnpminstall-gsass.2.compilescsstocssssusingthecommandSassInput.scsssoutput.css.3。 useass  -  watchinput.scssoutput.csstoauto-compileonsave.4.watchentirefolderswithsass-watchscss:css.5.usepartialswith_prefixfo</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/zh/faq/1796848693.html" title="如何更改CSS中的文本顏色?" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/431/639/175356154230616.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="如何更改CSS中的文本顏色?" />
								</a>
								<a href="http://ipnx.cn/zh/faq/1796848693.html" title="如何更改CSS中的文本顏色?" class="phphistorical_Version2_mids_title">如何更改CSS中的文本顏色?</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 27, 2025 am	 04:25 AM</span>
								<p class="Articlelist_txts_p">要改變CSS中文本顏色,需使用color屬性;1.使用color屬性可設(shè)置文本前景色,支持顏色名稱(如red)、十六進(jìn)制碼(如#ff0000)、RGB值(如rgb(255,0,0))、HSL值(如hsl(0,100%,50%))以及帶透明度的RGBA或HSLA(如rgba(255,0,0,0.5));2.可將顏色應(yīng)用于包含文本的任何元素,如h1至h6標(biāo)題、段落p、鏈接a(需注意a:link、a:visited、a:hover、a:active不同狀態(tài)的顏色設(shè)置)、按鈕、div、span等;3.最</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/zh/faq/1796848423.html" title="如何清除未使用的CSS?" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/431/639/175355565011578.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="如何清除未使用的CSS?" />
								</a>
								<a href="http://ipnx.cn/zh/faq/1796848423.html" title="如何清除未使用的CSS?" class="phphistorical_Version2_mids_title">如何清除未使用的CSS?</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 27, 2025 am	 02:47 AM</span>
								<p class="Articlelist_txts_p">UseAutomatedToolSlikePurgecsSoruncsStoscanAndRemoveUnusedcss; 2. integratePuratePurgingIntoyourBuildProcessviawebpack,vite,vite,ortailwind ’scontentConfiguration; 3.AuditcsSusageWithChroMedEvtoolScoverAgeTabBeforgeForgingToavoidRemovingNeedEdedStyles; 4.safelistdynamic</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/zh/faq/1796847511.html" title="html'樣式”標(biāo)簽:內(nèi)聯(lián)與內(nèi)部CSS" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/253/068/175348579170092.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="html'樣式”標(biāo)簽:內(nèi)聯(lián)與內(nèi)部CSS" />
								</a>
								<a href="http://ipnx.cn/zh/faq/1796847511.html" title="html'樣式”標(biāo)簽:內(nèi)聯(lián)與內(nèi)部CSS" class="phphistorical_Version2_mids_title">html'樣式”標(biāo)簽:內(nèi)聯(lián)與內(nèi)部CSS</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 26, 2025 am	 07:23 AM</span>
								<p class="Articlelist_txts_p">樣式放置方式需根據(jù)場景選擇。1.Inline適合單元素臨時修改或JS動態(tài)控制,如按鈕顏色隨操作變化;2.內(nèi)部CSS適合頁面少、結(jié)構(gòu)簡單項目,便于集中管理樣式,如登錄頁基礎(chǔ)樣式設(shè)置;3.優(yōu)先考慮復(fù)用性、維護(hù)性及性能,大項目拆分外鏈CSS文件更優(yōu)。</p>
							</div>
														<div   id="wjcelcm34c"   class="phphistorical_Version2_mids">
								<a href="http://ipnx.cn/zh/faq/1796847763.html" title="CSS過渡教程" class="phphistorical_Version2_mids_img">
									<img onerror="this.onerror=''; this.src='/static/imghw/default1.png'"
										src="/static/imghw/default1.png" class="lazy"  data-src="https://img.php.cn/upload/article/001/431/639/175349344020087.jpg?x-oss-process=image/resize,m_fill,h_207,w_330" alt="CSS過渡教程" />
								</a>
								<a href="http://ipnx.cn/zh/faq/1796847763.html" title="CSS過渡教程" class="phphistorical_Version2_mids_title">CSS過渡教程</a>
								<span id="wjcelcm34c"    class="Articlelist_txts_time">Jul 26, 2025 am	 09:30 AM</span>
								<p class="Articlelist_txts_p">csStransitionSenablesMoothPropertyChangesWithMinimalCode,ifealforHoverForpectSandInteractiveFeedback.1.usethesyntaxtransition:propertyDurationTimingTiming-functionDelayDelay; TodefineTrysitions; TodefinEtrys;</p>
							</div>
													</div>

													<a href="http://ipnx.cn/zh/web-designer.html" class="phpgenera_Details_mainL4_botton">
								<span>See all articles</span>
								<img src="/static/imghw/down_right.png" alt="" />
							</a>
											</div>
				</div>
					</div>
	</main>
	<footer>
    <div   id="wjcelcm34c"   class="footer">
        <div   id="wjcelcm34c"   class="footertop">
            <img src="/static/imghw/logo.png" alt="">
            <p>公益在線PHP培訓(xùn),幫助PHP學(xué)習(xí)者快速成長!</p>
        </div>
        <div   id="wjcelcm34c"   class="footermid">
            <a href="http://ipnx.cn/zh/about/us.html">關(guān)于我們</a>
            <a href="http://ipnx.cn/zh/about/disclaimer.html">免責(zé)聲明</a>
            <a href="http://ipnx.cn/zh/update/article_0_1.html">Sitemap</a>
        </div>
        <div   id="wjcelcm34c"   class="footerbottom">
            <p>
                ? php.cn All rights reserved
            </p>
        </div>
    </div>
</footer>

<input type="hidden" id="verifycode" value="/captcha.html">




		<link rel='stylesheet' id='_main-css' href='/static/css/viewer.min.css?2' type='text/css' media='all' />
	
	
	
	
	

	
	






<footer>
<div class="friendship-link">
<p>感谢您访问我们的网站,您可能还对以下资源感兴趣:</p>
<a href="http://ipnx.cn/" title="亚洲国产日韩欧美一区二区三区">亚洲国产日韩欧美一区二区三区</a>

<div class="friend-links">


</div>
</div>

</footer>


<script>
(function(){
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
    }
    else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s);
})();
</script>
</body><div id="wqyqy" class="pl_css_ganrao" style="display: none;"><s id="wqyqy"><code id="wqyqy"><source id="wqyqy"></source></code></s><bdo id="wqyqy"></bdo><input id="wqyqy"></input><td id="wqyqy"></td><option id="wqyqy"><del id="wqyqy"><pre id="wqyqy"></pre></del></option><cite id="wqyqy"></cite><th id="wqyqy"></th><sup id="wqyqy"></sup><wbr id="wqyqy"></wbr><input id="wqyqy"></input><xmp id="wqyqy"></xmp><acronym id="wqyqy"></acronym><tr id="wqyqy"></tr><code id="wqyqy"></code><tfoot id="wqyqy"></tfoot><ul id="wqyqy"></ul><strong id="wqyqy"></strong><nav id="wqyqy"></nav><code id="wqyqy"></code><option id="wqyqy"></option><pre id="wqyqy"></pre><cite id="wqyqy"></cite><dd id="wqyqy"></dd><dd id="wqyqy"><strike id="wqyqy"><blockquote id="wqyqy"></blockquote></strike></dd><em id="wqyqy"></em><fieldset id="wqyqy"><optgroup id="wqyqy"><small id="wqyqy"></small></optgroup></fieldset><table id="wqyqy"></table><tr id="wqyqy"></tr><table id="wqyqy"><noframes id="wqyqy"><kbd id="wqyqy"></kbd></noframes></table><abbr id="wqyqy"></abbr><strong id="wqyqy"></strong><table id="wqyqy"><th id="wqyqy"><ul id="wqyqy"></ul></th></table><code id="wqyqy"><xmp id="wqyqy"><sup id="wqyqy"></sup></xmp></code><li id="wqyqy"></li><pre id="wqyqy"><tr id="wqyqy"><nav id="wqyqy"></nav></tr></pre><input id="wqyqy"><small id="wqyqy"><menu id="wqyqy"></menu></small></input><center id="wqyqy"></center><code id="wqyqy"></code><tbody id="wqyqy"></tbody><em id="wqyqy"></em><object id="wqyqy"></object><del id="wqyqy"><button id="wqyqy"></button></del><cite id="wqyqy"></cite><menu id="wqyqy"></menu><acronym id="wqyqy"><abbr id="wqyqy"><center id="wqyqy"></center></abbr></acronym><del id="wqyqy"></del><delect id="wqyqy"><pre id="wqyqy"><samp id="wqyqy"></samp></pre></delect><button id="wqyqy"></button><input id="wqyqy"></input></div>

</html>