Rumah >hujung hadapan web >tutorial css >深入理解css中的margin属性

深入理解css中的margin属性

巴扎黑
巴扎黑asal
2017-06-28 13:34:201891semak imbas
<p>深入理解css中的margin属性</p> <p>  之前我一直认为margin属性是一个非常简单的属性,但是最近做项目时遇到了一些问题,才发现margin属性还是有一些“坑”的,下面我会介绍margin的基本知识以及那些“坑”。这篇博文主要分为以下几个部分:</p> <ul class=" list-paddingleft-2"> <li><p>margin--基础知识</p></li> <li><p>margin--在同级元素(非父子关系)之间应用</p></li> <li><p>margin--在父元素和子元素之间应用(<strong>重点</strong>)</p></li> <li> <p>margin--margin值的单位为%时的几种情况</p> <p> </p> </li> </ul> <h2>第一部分:margin--基础知识</h2> <p>  要介绍margin的基础知识,我们不可回避地要谈到<a href="http://www.php.cn/code/790.html" target="_blank">css盒子模型</a>(Box Model),一般而言,css盒子模型是用来设计和布局的。它本质上是一个盒子,包括:外边距(margin)、边框(border)、内边距(padding)以及最中间的内容(content)。下图即为盒子模型(这里只谈W3C规范的标准盒模型,而不谈IE5和IE6在怪异模式中使用的非标准的盒子模型):</p> <p> <img src="https://img.php.cn/upload/article/000/000/007/d27cd8e47c9fd00076f4232fc6379b18-0.png" alt="" style="max-width:90%" style="max-width:90%"></p> <p>   我们要介绍的margin在最外层,因为margin(外边距)一定是透明的,所以它可以用来使得不同的盒子之间留有一定的间隙从而达到布局美观等效果。从上面的盒子模型中我们可以看到,margin在四周均存在,我们可以使用margin-top、<a href="http://www.php.cn/wiki/934.html" target="_blank">margin-right</a>、<a href="http://www.php.cn/wiki/935.html" target="_blank">margin-bottom</a>、margin-left分别设置这四个方向的margin值。(注:由于这部分知识较为基础,所以我不再在这部分不做更多介绍)</p> <p> </p> <p> </p> <h2>第二部分:margin--在同级元素(非父子关系)之间应用</h2> <p>  这一部分主要介绍水平方向和竖直方向的外边距的合并问题。</p> <h2> (1)水平方向的外边距合并</h2> <p>     <strong>两个水平方向的盒子相遇,那么最终两者之间的距离为左边盒子的右外边距和右边盒子的做外边距之和。</strong></p> <p><strong>     例1:</strong></p> <p><strong>       代码如下:</strong></p> <p><strong>    </strong></p> <p class="cnblogs_Highlighter sh-gutter"><br></p> <p class="syntaxhighlighter html"><br></p> <table border="0"><tbody><tr class="firstRow"> <td class="gutter"> <p class="line number1 index0 alt2">1</p> <p class="line number2 index1 alt1">2</p> <p class="line number3 index2 alt2">3</p> <p class="line number4 index3 alt1">4</p> <p class="line number5 index4 alt2">5</p> <p class="line number6 index5 alt1">6</p> <p class="line number7 index6 alt2">7</p> <p class="line number8 index7 alt1">8</p> <p class="line number9 index8 alt2">9</p> <p class="line number10 index9 alt1">10</p> <p class="line number11 index10 alt2">11</p> <p class="line number12 index11 alt1">12</p> <p class="line number13 index12 alt2">13</p> <p class="line number14 index13 alt1">14</p> <p class="line number15 index14 alt2">15</p> <p class="line number16 index15 alt1">16</p> <p class="line number17 index16 alt2">17</p> <p class="line number18 index17 alt1">18</p> <p class="line number19 index18 alt2">19</p> <p class="line number20 index19 alt1">20</p> <p class="line number21 index20 alt2">21</p> <p class="line number22 index21 alt1">22</p> <p class="line number23 index22 alt2">23</p> <p class="line number24 index23 alt1">24</p> <p class="line number25 index24 alt2">25</p> <p class="line number26 index25 alt1">26</p> <p class="line number27 index26 alt2">27</p> <p class="line number28 index27 alt1">28</p> <p class="line number29 index28 alt2">29</p> <p class="line number30 index29 alt1">30</p> <p class="line number31 index30 alt2">31</p> <p class="line number32 index31 alt1">32</p> <p class="line number33 index32 alt2">33</p> <p class="line number34 index33 alt1">34</p> <p class="line number35 index34 alt2">35</p> <p class="line number36 index35 alt1">36</p> <p class="line number37 index36 alt2">37</p> </td> <td class="code"> <p class="container"><br></p> <p class="line number1 index0 alt2"><code class="html plain"><!</code><code class="html keyword">DOCTYPE</code> <code class="html plain">html></code></p> <p class="line number2 index1 alt1"><code class="html plain"><</code><code class="html keyword">html</code> <code class="html plain">lang="en"></code></p> <p class="line number3 index2 alt2"><code class="html plain"><</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number4 index3 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">meta</code> <code class="html plain">charset="UTF-8"></code></p> <p class="line number5 index4 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">title</code><code class="html plain">>水平方向的两个盒子</</code><code class="html keyword">title</code><code class="html plain">></code></p> <p class="line number6 index5 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number7 index6 alt2"><code class="html spaces">        </code><code class="html plain">*{</code></p> <p class="line number8 index7 alt1"><code class="html spaces">            </code><code class="html plain">margin:0;</code></p> <p class="line number9 index8 alt2"><code class="html spaces">            </code><code class="html plain">padding:0;</code></p> <p class="line number10 index9 alt1"><code class="html spaces">            </code><code class="html plain">border:0;</code></p> <p class="line number11 index10 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number12 index11 alt1"><code class="html spaces">        </code><code class="html plain">body{</code></p> <p class="line number13 index12 alt2"><code class="html spaces">            </code><code class="html plain">font-size: 0;</code></p> <p class="line number14 index13 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number15 index14 alt2"><code class="html spaces">        </code><code class="html plain">.left{</code></p> <p class="line number16 index15 alt1"><code class="html spaces">            </code><code class="html plain">width: 100px;</code></p> <p class="line number17 index16 alt2"><code class="html spaces">            </code><code class="html plain">height: 100px;</code></p> <p class="line number18 index17 alt1"><code class="html spaces">            </code><code class="html plain">background: red;</code></p> <p class="line number19 index18 alt2"><code class="html spaces">            </code><code class="html plain">display: inline-block;</code></p> <p class="line number20 index19 alt1"><code class="html spaces">            </code><code class="html plain">margin-right: 50px;</code></p> <p class="line number21 index20 alt2"><code class="html spaces">            </code><code class="html plain">font-size: 20px;</code></p> <p class="line number22 index21 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number23 index22 alt2"><code class="html spaces">        </code><code class="html plain">.right{</code></p> <p class="line number24 index23 alt1"><code class="html spaces">            </code><code class="html plain">width: 100px;</code></p> <p class="line number25 index24 alt2"><code class="html spaces">            </code><code class="html plain">height: 100px;</code></p> <p class="line number26 index25 alt1"><code class="html spaces">            </code><code class="html plain">background: yellow;</code></p> <p class="line number27 index26 alt2"><code class="html spaces">            </code><code class="html plain">display: inline-block;</code></p> <p class="line number28 index27 alt1"><code class="html spaces">            </code><code class="html plain">margin-left: 50px;</code></p> <p class="line number29 index28 alt2"><code class="html spaces">            </code><code class="html plain">font-size: 20px;</code></p> <p class="line number30 index29 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number31 index30 alt2"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number32 index31 alt1"><code class="html plain"></</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number33 index32 alt2"><code class="html plain"><</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number34 index33 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="left">宽为100px,右边距为50px</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number35 index34 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="right">宽为100px,左边距为50px</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number36 index35 alt1"><code class="html plain"></</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number37 index36 alt2"><code class="html plain"></</code><code class="html keyword">html</code><code class="html plain">></code></p> </td> </tr></tbody></table> <p>  效果如下:</p> <p><img src="https://img.php.cn/upload/article/000/000/007/d27cd8e47c9fd00076f4232fc6379b18-1.png" alt=""></p> <p>这时两者之间的距离刚好为100px。</p> <p>  补充说明:大家可以看到,为了使得两个p(块状元素)脱离正常的文档流我使用了display:inline-block;属性,<strong>另外,我还把body的font-size设置为0,这样可以解决inline-block自身的问题(</strong>如果不清楚这里的描述可以看我的博文《理解与应用css中的display属性》,这篇文章介绍了inline-block存在的问题<strong>),</strong>否则两个p的举例会大于100px。当然使用float也可以使得两个p出现在同一行中。</p> <p> </p> <h2>  (2)竖直方向的外边距合并</h2> <p>  <strong>两个竖直方向的盒子相遇时,其竖直方向的距离等于上方盒子的下外边距和下方盒子的上外边距中较大的一个。</strong></p> <p><strong>  例2:</strong></p> <p><strong>  </strong></p> <p class="cnblogs_Highlighter sh-gutter"><br></p> <p class="syntaxhighlighter html"><br></p> <table border="0"><tbody><tr class="firstRow"> <td class="gutter"> <p class="line number1 index0 alt2">1</p> <p class="line number2 index1 alt1">2</p> <p class="line number3 index2 alt2">3</p> <p class="line number4 index3 alt1">4</p> <p class="line number5 index4 alt2">5</p> <p class="line number6 index5 alt1">6</p> <p class="line number7 index6 alt2">7</p> <p class="line number8 index7 alt1">8</p> <p class="line number9 index8 alt2">9</p> <p class="line number10 index9 alt1">10</p> <p class="line number11 index10 alt2">11</p> <p class="line number12 index11 alt1">12</p> <p class="line number13 index12 alt2">13</p> <p class="line number14 index13 alt1">14</p> <p class="line number15 index14 alt2">15</p> <p class="line number16 index15 alt1">16</p> <p class="line number17 index16 alt2">17</p> <p class="line number18 index17 alt1">18</p> <p class="line number19 index18 alt2">19</p> <p class="line number20 index19 alt1">20</p> <p class="line number21 index20 alt2">21</p> <p class="line number22 index21 alt1">22</p> <p class="line number23 index22 alt2">23</p> <p class="line number24 index23 alt1">24</p> <p class="line number25 index24 alt2">25</p> <p class="line number26 index25 alt1">26</p> <p class="line number27 index26 alt2">27</p> <p class="line number28 index27 alt1">28</p> <p class="line number29 index28 alt2">29</p> <p class="line number30 index29 alt1">30</p> </td> <td class="code"> <p class="container"><br></p> <p class="line number1 index0 alt2"><code class="html plain"><!</code><code class="html keyword">DOCTYPE</code> <code class="html plain">html></code></p> <p class="line number2 index1 alt1"><code class="html plain"><</code><code class="html keyword">html</code> <code class="html plain">lang="en"></code></p> <p class="line number3 index2 alt2"><code class="html plain"><</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number4 index3 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">meta</code> <code class="html plain">charset="UTF-8"></code></p> <p class="line number5 index4 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">title</code><code class="html plain">>水平方向的两个盒子</</code><code class="html keyword">title</code><code class="html plain">></code></p> <p class="line number6 index5 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number7 index6 alt2"><code class="html spaces">        </code><code class="html plain">*{</code></p> <p class="line number8 index7 alt1"><code class="html spaces">            </code><code class="html plain">margin:0;</code></p> <p class="line number9 index8 alt2"><code class="html spaces">            </code><code class="html plain">padding:0;</code></p> <p class="line number10 index9 alt1"><code class="html spaces">            </code><code class="html plain">border:0;</code></p> <p class="line number11 index10 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number12 index11 alt1"><code class="html spaces">        </code><code class="html plain">.top{</code></p> <p class="line number13 index12 alt2"><code class="html spaces">            </code><code class="html plain">width: 100px;</code></p> <p class="line number14 index13 alt1"><code class="html spaces">            </code><code class="html plain">height: 100px;</code></p> <p class="line number15 index14 alt2"><code class="html spaces">            </code><code class="html plain">margin-bottom: 100px;</code></p> <p class="line number16 index15 alt1"><code class="html spaces">            </code><code class="html plain">background: red;</code></p> <p class="line number17 index16 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number18 index17 alt1"><code class="html spaces">        </code><code class="html plain">.bottom{</code></p> <p class="line number19 index18 alt2"><code class="html spaces">            </code><code class="html plain">width: 100px;</code></p> <p class="line number20 index19 alt1"><code class="html spaces">            </code><code class="html plain">height: 100px;</code></p> <p class="line number21 index20 alt2"><code class="html spaces">            </code><code class="html plain">margin-top: 50px;</code></p> <p class="line number22 index21 alt1"><code class="html spaces">            </code><code class="html plain">background: green;</code></p> <p class="line number23 index22 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number24 index23 alt1"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number25 index24 alt2"><code class="html plain"></</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number26 index25 alt1"><code class="html plain"><</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number27 index26 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="top">高为100px,下边距为100px</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number28 index27 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="bottom">高为100px,上边距为50px</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number29 index28 alt2"><code class="html plain"></</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number30 index29 alt1"><code class="html plain"></</code><code class="html keyword">html</code><code class="html plain">></code></p> </td> </tr></tbody></table> <p>  效果如下:</p> <p><img src="https://img.php.cn/upload/article/000/000/007/d27cd8e47c9fd00076f4232fc6379b18-2.png" alt=""></p> <p>这时我们肉眼都可以观察出来,两者竖直方向的举例大约为100px(实际就是100px)而非100+50=150px;这正是因为<strong>两个竖直方向的盒子相遇时,其竖直方向的距离等于上方盒子的下外边距和下方盒子的上外边距中较大的一个。</strong></p> <p><strong>  </strong></p> <p><strong>  另外一个有趣的例子就是:假设有一个元素同时设置了margin-top和margin-bottom,但是内容为空,那么这两个margin值也会叠加,值为两者最大的一个,它类似与竖直方向上两个盒子margin值的叠加。代码如下:</strong></p> <p class="cnblogs_Highlighter sh-gutter"><br></p> <p class="syntaxhighlighter html"><br></p> <table border="0"><tbody><tr class="firstRow"> <td class="gutter"> <p class="line number1 index0 alt2">1</p> <p class="line number2 index1 alt1">2</p> <p class="line number3 index2 alt2">3</p> <p class="line number4 index3 alt1">4</p> <p class="line number5 index4 alt2">5</p> <p class="line number6 index5 alt1">6</p> <p class="line number7 index6 alt2">7</p> <p class="line number8 index7 alt1">8</p> <p class="line number9 index8 alt2">9</p> <p class="line number10 index9 alt1">10</p> <p class="line number11 index10 alt2">11</p> <p class="line number12 index11 alt1">12</p> <p class="line number13 index12 alt2">13</p> <p class="line number14 index13 alt1">14</p> <p class="line number15 index14 alt2">15</p> <p class="line number16 index15 alt1">16</p> <p class="line number17 index16 alt2">17</p> <p class="line number18 index17 alt1">18</p> <p class="line number19 index18 alt2">19</p> <p class="line number20 index19 alt1">20</p> <p class="line number21 index20 alt2">21</p> <p class="line number22 index21 alt1">22</p> <p class="line number23 index22 alt2">23</p> <p class="line number24 index23 alt1">24</p> <p class="line number25 index24 alt2">25</p> <p class="line number26 index25 alt1">26</p> <p class="line number27 index26 alt2">27</p> <p class="line number28 index27 alt1">28</p> <p class="line number29 index28 alt2">29</p> <p class="line number30 index29 alt1">30</p> <p class="line number31 index30 alt2">31</p> <p class="line number32 index31 alt1">32</p> <p class="line number33 index32 alt2">33</p> <p class="line number34 index33 alt1">34</p> </td> <td class="code"> <p class="container"><br></p> <p class="line number1 index0 alt2"><code class="html plain"><!</code><code class="html keyword">DOCTYPE</code> <code class="html plain">html></code></p> <p class="line number2 index1 alt1"><code class="html plain"><</code><code class="html keyword">html</code> <code class="html plain">lang="en"></code></p> <p class="line number3 index2 alt2"><code class="html plain"><</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number4 index3 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">meta</code> <code class="html plain">charset="UTF-8"></code></p> <p class="line number5 index4 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">title</code><code class="html plain">>水平方向的两个盒子</</code><code class="html keyword">title</code><code class="html plain">></code></p> <p class="line number6 index5 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number7 index6 alt2"><code class="html spaces">        </code><code class="html plain">*{</code></p> <p class="line number8 index7 alt1"><code class="html spaces">            </code><code class="html plain">margin:0;</code></p> <p class="line number9 index8 alt2"><code class="html spaces">            </code><code class="html plain">padding:0;</code></p> <p class="line number10 index9 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number11 index10 alt2"> </p> <p class="line number12 index11 alt1"><code class="html spaces">        </code><code class="html plain">.top{</code></p> <p class="line number13 index12 alt2"><code class="html spaces">            </code><code class="html plain">width: 500px;</code></p> <p class="line number14 index13 alt1"><code class="html spaces">            </code><code class="html plain">height: 100px;</code></p> <p class="line number15 index14 alt2"><code class="html spaces">            </code><code class="html plain">background: red;</code></p> <p class="line number16 index15 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number17 index16 alt2"><code class="html spaces">        </code><code class="html plain">.middle{</code></p> <p class="line number18 index17 alt1"><code class="html spaces">            </code><code class="html plain">margin-top: 100px;</code></p> <p class="line number19 index18 alt2"><code class="html spaces">            </code><code class="html plain">margin-bottom:50px;</code></p> <p class="line number20 index19 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number21 index20 alt2"><code class="html spaces">        </code><code class="html plain">.footer{</code></p> <p class="line number22 index21 alt1"><code class="html spaces">            </code><code class="html plain">width: 500px;</code></p> <p class="line number23 index22 alt2"><code class="html spaces">            </code><code class="html plain">height: 100px;</code></p> <p class="line number24 index23 alt1"><code class="html spaces">            </code><code class="html plain">background: green;</code></p> <p class="line number25 index24 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number26 index25 alt1"> </p> <p class="line number27 index26 alt2"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number28 index27 alt1"><code class="html plain"></</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number29 index28 alt2"><code class="html plain"><</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number30 index29 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="top">上面的p,高100px</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number31 index30 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="middle"></</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number32 index31 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="footer">下面的p,高100px</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number33 index32 alt2"><code class="html plain"></</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number34 index33 alt1"><code class="html plain"></</code><code class="html keyword">html</code><code class="html plain">></code></p> </td> </tr></tbody></table> <p>  最终的效果如下:</p> <p><img src="https://img.php.cn/upload/article/000/000/007/d27cd8e47c9fd00076f4232fc6379b18-3.png" alt=""></p> <p>  我们发现这时在上面的p和在下面的p之间的举例并不是100+50=150px,而是两者中的最大者,即100px。</p> <p><strong>  </strong></p> <p>  那么W3C为什么会设定这样的标准而不设定和水平方向一样的标准呢?即margin值的叠加,实际上这也是有一定的道理的。比如我们需要设计一个由若干个段落构成的一个页面。我们需要设置margin-top和margin-bottom使得第一段和页面的最上方有一段距离,使得最后一段和最下方有一段距离。下面是不叠加和叠加的效果图:</p> <p><img src="https://img.php.cn/upload/article/000/000/007/d27cd8e47c9fd00076f4232fc6379b18-4.png" alt=""></p> <p>我们可以看到左边的页面没有重叠,那么两个段落之间的举例就是最上方的两倍间距了,而右边的页面发生了重叠,则所有的间距都是相等的。或许这就是这样设定标准的目的吧,谁知道呢?</p> <p> </p> <p> </p> <h2>第三部分:margin--在父元素和子元素之间应用(<strong>重点</strong>)</h2> <p>    第二部分介绍了同级元素之间使用margin,而这一部分将要介绍最有意思的父元素和子元素之间margin的应用。这一部分,我们同样从两个方面来讨论。一方面是子元素设置水平方向上的margin值,另一方面是子元素设置竖直方向的margin值。</p> <h3>  (1)在子元素中设置水平方向的margin值</h3> <p>    我们可以设置margin-left来控制子元素的左边框和父元素的左边框之间的举例。</p> <p>    例3:</p> <p class="cnblogs_Highlighter sh-gutter"><br></p> <p class="syntaxhighlighter html"><br></p> <table border="0"><tbody><tr class="firstRow"> <td class="gutter"> <p class="line number1 index0 alt2">1</p> <p class="line number2 index1 alt1">2</p> <p class="line number3 index2 alt2">3</p> <p class="line number4 index3 alt1">4</p> <p class="line number5 index4 alt2">5</p> <p class="line number6 index5 alt1">6</p> <p class="line number7 index6 alt2">7</p> <p class="line number8 index7 alt1">8</p> <p class="line number9 index8 alt2">9</p> <p class="line number10 index9 alt1">10</p> <p class="line number11 index10 alt2">11</p> <p class="line number12 index11 alt1">12</p> <p class="line number13 index12 alt2">13</p> <p class="line number14 index13 alt1">14</p> <p class="line number15 index14 alt2">15</p> <p class="line number16 index15 alt1">16</p> <p class="line number17 index16 alt2">17</p> <p class="line number18 index17 alt1">18</p> <p class="line number19 index18 alt2">19</p> <p class="line number20 index19 alt1">20</p> <p class="line number21 index20 alt2">21</p> <p class="line number22 index21 alt1">22</p> <p class="line number23 index22 alt2">23</p> <p class="line number24 index23 alt1">24</p> <p class="line number25 index24 alt2">25</p> <p class="line number26 index25 alt1">26</p> </td> <td class="code"> <p class="container"><br></p> <p class="line number1 index0 alt2"><code class="html plain"><!</code><code class="html keyword">DOCTYPE</code> <code class="html plain">html></code></p> <p class="line number2 index1 alt1"><code class="html plain"><</code><code class="html keyword">html</code> <code class="html plain">lang="en"></code></p> <p class="line number3 index2 alt2"><code class="html plain"><</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number4 index3 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">meta</code> <code class="html plain">charset="UTF-8"></code></p> <p class="line number5 index4 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">title</code><code class="html plain">>margin</</code><code class="html keyword">title</code><code class="html plain">></code></p> <p class="line number6 index5 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number7 index6 alt2"><code class="html spaces">        </code><code class="html plain">*{padding:0; margin:0; border:0;}</code></p> <p class="line number8 index7 alt1"><code class="html spaces">        </code><code class="html plain">.father{</code></p> <p class="line number9 index8 alt2"><code class="html spaces">            </code><code class="html plain">width: 500px;</code></p> <p class="line number10 index9 alt1"><code class="html spaces">            </code><code class="html plain">height: 500px;</code></p> <p class="line number11 index10 alt2"><code class="html spaces">            </code><code class="html plain">background: red;</code></p> <p class="line number12 index11 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number13 index12 alt2"><code class="html spaces">        </code><code class="html plain">.son{</code></p> <p class="line number14 index13 alt1"><code class="html spaces">            </code><code class="html plain">width: 100px;</code></p> <p class="line number15 index14 alt2"><code class="html spaces">            </code><code class="html plain">height: 100px;</code></p> <p class="line number16 index15 alt1"><code class="html spaces">            </code><code class="html plain">background: green;</code></p> <p class="line number17 index16 alt2"><code class="html spaces">            </code><code class="html plain">margin-left: 100px;</code></p> <p class="line number18 index17 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number19 index18 alt2"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number20 index19 alt1"><code class="html plain"></</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number21 index20 alt2"><code class="html plain"><</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number22 index21 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="father"></code></p> <p class="line number23 index22 alt2"><code class="html spaces">        </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="son">宽度为100px,margin-left为100px。</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number24 index23 alt1"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number25 index24 alt2"><code class="html plain"></</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number26 index25 alt1"><code class="html plain"></</code><code class="html keyword">html</code><code class="html plain">></code></p> </td> </tr></tbody></table> <p>  我将子元素的margin-left设置为了100px;效果如下:</p> <p><img src="https://img.php.cn/upload/article/000/000/007/b14182e42c329966dffd9ff54c3928c5-5.png" alt="" style="max-width:90%" style="max-width:90%"></p> <p>即子元素的左边框和父元素的左边框之间的距离为100px。<strong>与在同级元素之间设置margin不同,因为同级元素之间的margin不会考虑到padding,但是在父元素和子元素就不同了,那么如果父元素中如果有padding,效果会是什么样的呢?</strong>请看下面一个例子:</p> <p>  例4:</p> <p>  下面我们在上面例子的基础上给父元素添加padding值。</p> <p class="cnblogs_Highlighter sh-gutter"><br></p> <p class="syntaxhighlighter html"><br></p> <table border="0"><tbody><tr class="firstRow"> <td class="gutter"> <p class="line number1 index0 alt2">1</p> <p class="line number2 index1 alt1">2</p> <p class="line number3 index2 alt2">3</p> <p class="line number4 index3 alt1">4</p> <p class="line number5 index4 alt2">5</p> <p class="line number6 index5 alt1">6</p> <p class="line number7 index6 alt2">7</p> <p class="line number8 index7 alt1">8</p> <p class="line number9 index8 alt2">9</p> <p class="line number10 index9 alt1">10</p> <p class="line number11 index10 alt2">11</p> <p class="line number12 index11 alt1">12</p> <p class="line number13 index12 alt2">13</p> <p class="line number14 index13 alt1">14</p> <p class="line number15 index14 alt2">15</p> <p class="line number16 index15 alt1">16</p> <p class="line number17 index16 alt2">17</p> <p class="line number18 index17 alt1">18</p> <p class="line number19 index18 alt2">19</p> <p class="line number20 index19 alt1">20</p> <p class="line number21 index20 alt2">21</p> <p class="line number22 index21 alt1">22</p> <p class="line number23 index22 alt2">23</p> <p class="line number24 index23 alt1">24</p> <p class="line number25 index24 alt2">25</p> <p class="line number26 index25 alt1">26</p> <p class="line number27 index26 alt2">27</p> </td> <td class="code"> <p class="container"><br></p> <p class="line number1 index0 alt2"><code class="html plain"><!</code><code class="html keyword">DOCTYPE</code> <code class="html plain">html></code></p> <p class="line number2 index1 alt1"><code class="html plain"><</code><code class="html keyword">html</code> <code class="html plain">lang="en"></code></p> <p class="line number3 index2 alt2"><code class="html plain"><</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number4 index3 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">meta</code> <code class="html plain">charset="UTF-8"></code></p> <p class="line number5 index4 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">title</code><code class="html plain">>margin</</code><code class="html keyword">title</code><code class="html plain">></code></p> <p class="line number6 index5 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number7 index6 alt2"><code class="html spaces">        </code><code class="html plain">*{padding:0; margin:0; border:0;}</code></p> <p class="line number8 index7 alt1"><code class="html spaces">        </code><code class="html plain">.father{</code></p> <p class="line number9 index8 alt2"><code class="html spaces">            </code><code class="html plain">width: 500px;</code></p> <p class="line number10 index9 alt1"><code class="html spaces">            </code><code class="html plain">height: 500px;</code></p> <p class="line number11 index10 alt2"><code class="html spaces">            </code><code class="html plain">padding:100px;</code></p> <p class="line number12 index11 alt1"><code class="html spaces">            </code><code class="html plain">background: red;</code></p> <p class="line number13 index12 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number14 index13 alt1"><code class="html spaces">        </code><code class="html plain">.son{</code></p> <p class="line number15 index14 alt2"><code class="html spaces">            </code><code class="html plain">width: 100px;</code></p> <p class="line number16 index15 alt1"><code class="html spaces">            </code><code class="html plain">height: 100px;</code></p> <p class="line number17 index16 alt2"><code class="html spaces">            </code><code class="html plain">background: green;</code></p> <p class="line number18 index17 alt1"><code class="html spaces">            </code><code class="html plain">margin-left: 100px;</code></p> <p class="line number19 index18 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number20 index19 alt1"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number21 index20 alt2"><code class="html plain"></</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number22 index21 alt1"><code class="html plain"><</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number23 index22 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="father"></code></p> <p class="line number24 index23 alt1"><code class="html spaces">        </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="son">宽度为100px,margin-left为100px。</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number25 index24 alt2"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number26 index25 alt1"><code class="html plain"></</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number27 index26 alt2"><code class="html plain"></</code><code class="html keyword">html</code><code class="html plain">></code></p> </td> </tr></tbody></table> <p>  上面的代码给父元素添加了100px的padding值,效果如下:</p> <p><img src="https://img.php.cn/upload/article/000/000/007/b14182e42c329966dffd9ff54c3928c5-6.png" alt="" style="max-width:90%" style="max-width:90%"></p> <p>我们可以看到子元素举例上方的距离为100px,因为子元素一定是在父元素的content的部分的,这点毫无疑问。</p> <p><strong>但是经过测量可以发现子元素的左边框距离父元素的左边框之间的距离为200px</strong>,因为其中还有100px的左padding值,前面的例子因为我没有设置padding值,所以没有观察出来,<strong>因此这就说明了在子元素中设置margin-left,其值实际上是子元素的左边框距离父元素左padding内侧的距离。</strong></p> <p> </p> <p> 例5:margin-right的使用和margin-left的使用是相似的,我在这里只举一个例子。</p> <p>  这个例子在子元素中设置了margin-right值,如下所示:</p> <p class="cnblogs_Highlighter sh-gutter"><br></p> <p class="syntaxhighlighter html"><br></p> <table border="0"><tbody><tr class="firstRow"> <td class="gutter"> <p class="line number1 index0 alt2">1</p> <p class="line number2 index1 alt1">2</p> <p class="line number3 index2 alt2">3</p> <p class="line number4 index3 alt1">4</p> <p class="line number5 index4 alt2">5</p> <p class="line number6 index5 alt1">6</p> <p class="line number7 index6 alt2">7</p> <p class="line number8 index7 alt1">8</p> <p class="line number9 index8 alt2">9</p> <p class="line number10 index9 alt1">10</p> <p class="line number11 index10 alt2">11</p> <p class="line number12 index11 alt1">12</p> <p class="line number13 index12 alt2">13</p> <p class="line number14 index13 alt1">14</p> <p class="line number15 index14 alt2">15</p> <p class="line number16 index15 alt1">16</p> <p class="line number17 index16 alt2">17</p> <p class="line number18 index17 alt1">18</p> <p class="line number19 index18 alt2">19</p> <p class="line number20 index19 alt1">20</p> <p class="line number21 index20 alt2">21</p> <p class="line number22 index21 alt1">22</p> <p class="line number23 index22 alt2">23</p> <p class="line number24 index23 alt1">24</p> <p class="line number25 index24 alt2">25</p> <p class="line number26 index25 alt1">26</p> <p class="line number27 index26 alt2">27</p> <p class="line number28 index27 alt1">28</p> </td> <td class="code"> <p class="container"><br></p> <p class="line number1 index0 alt2"><code class="html plain"><!</code><code class="html keyword">DOCTYPE</code> <code class="html plain">html></code></p> <p class="line number2 index1 alt1"><code class="html plain"><</code><code class="html keyword">html</code> <code class="html plain">lang="en"></code></p> <p class="line number3 index2 alt2"><code class="html plain"><</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number4 index3 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">meta</code> <code class="html plain">charset="UTF-8"></code></p> <p class="line number5 index4 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">title</code><code class="html plain">>margin</</code><code class="html keyword">title</code><code class="html plain">></code></p> <p class="line number6 index5 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number7 index6 alt2"><code class="html spaces">        </code><code class="html plain">*{padding:0; margin:0; border:0;}</code></p> <p class="line number8 index7 alt1"><code class="html spaces">        </code><code class="html plain">.father{</code></p> <p class="line number9 index8 alt2"><code class="html spaces">            </code><code class="html plain">width: 500px;</code></p> <p class="line number10 index9 alt1"><code class="html spaces">            </code><code class="html plain">height: 500px;</code></p> <p class="line number11 index10 alt2"><code class="html spaces">            </code><code class="html plain">padding:100px;</code></p> <p class="line number12 index11 alt1"><code class="html spaces">            </code><code class="html plain">background: red;</code></p> <p class="line number13 index12 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number14 index13 alt1"><code class="html spaces">        </code><code class="html plain">.son{</code></p> <p class="line number15 index14 alt2"><code class="html spaces">            </code><code class="html plain">float: right;</code></p> <p class="line number16 index15 alt1"><code class="html spaces">            </code><code class="html plain">width: 100px;</code></p> <p class="line number17 index16 alt2"><code class="html spaces">            </code><code class="html plain">height: 100px;</code></p> <p class="line number18 index17 alt1"><code class="html spaces">            </code><code class="html plain">background: green;</code></p> <p class="line number19 index18 alt2"><code class="html spaces">            </code><code class="html plain">margin-right: 100px;</code></p> <p class="line number20 index19 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number21 index20 alt2"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number22 index21 alt1"><code class="html plain"></</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number23 index22 alt2"><code class="html plain"><</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number24 index23 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="father"></code></p> <p class="line number25 index24 alt2"><code class="html spaces">        </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="son">宽度为100px,margin-right为100px。</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number26 index25 alt1"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number27 index26 alt2"><code class="html plain"></</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number28 index27 alt1"><code class="html plain"></</code><code class="html keyword">html</code><code class="html plain">></code></p> </td> </tr></tbody></table> <p>  这个例子与例4的区别仅在与子元素的位置不同。效果如下:</p> <p><img src="https://img.php.cn/upload/article/000/000/007/b14182e42c329966dffd9ff54c3928c5-7.png" alt="" style="max-width:90%" style="max-width:90%"></p> <p>通过<strong>这个例子可以说明margin-right的值是子元素的右边框和父元素的右padding内侧的距离</strong>。只是前面的几个例子我没有使用padding,所以无法观察出来。</p> <p> </p> <p> </p> <p> </p> <h3>  (2)在子元素中设置竖直方向的margin值</h3> <p>    按照前面的经验,理论上来说,我们同样可以通过设置margin-top的值使得<strong>子元素的上边框和父元素的上padding的内侧</strong>留有一定的距离。那么我们就试试吧!</p> <p>  例6:</p> <p>        </p> <p class="cnblogs_Highlighter sh-gutter"><br></p> <p class="syntaxhighlighter html"><br></p> <table border="0"><tbody><tr class="firstRow"> <td class="gutter"> <p class="line number1 index0 alt2">1</p> <p class="line number2 index1 alt1">2</p> <p class="line number3 index2 alt2">3</p> <p class="line number4 index3 alt1">4</p> <p class="line number5 index4 alt2">5</p> <p class="line number6 index5 alt1">6</p> <p class="line number7 index6 alt2">7</p> <p class="line number8 index7 alt1">8</p> <p class="line number9 index8 alt2">9</p> <p class="line number10 index9 alt1">10</p> <p class="line number11 index10 alt2">11</p> <p class="line number12 index11 alt1">12</p> <p class="line number13 index12 alt2">13</p> <p class="line number14 index13 alt1">14</p> <p class="line number15 index14 alt2">15</p> <p class="line number16 index15 alt1">16</p> <p class="line number17 index16 alt2">17</p> <p class="line number18 index17 alt1">18</p> <p class="line number19 index18 alt2">19</p> <p class="line number20 index19 alt1">20</p> <p class="line number21 index20 alt2">21</p> <p class="line number22 index21 alt1">22</p> <p class="line number23 index22 alt2">23</p> <p class="line number24 index23 alt1">24</p> <p class="line number25 index24 alt2">25</p> <p class="line number26 index25 alt1">26</p> </td> <td class="code"> <p class="container"><br></p> <p class="line number1 index0 alt2"><code class="html plain"><!</code><code class="html keyword">DOCTYPE</code> <code class="html plain">html></code></p> <p class="line number2 index1 alt1"><code class="html plain"><</code><code class="html keyword">html</code> <code class="html plain">lang="en"></code></p> <p class="line number3 index2 alt2"><code class="html plain"><</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number4 index3 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">meta</code> <code class="html plain">charset="UTF-8"></code></p> <p class="line number5 index4 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">title</code><code class="html plain">>margin</</code><code class="html keyword">title</code><code class="html plain">></code></p> <p class="line number6 index5 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number7 index6 alt2"><code class="html spaces">        </code><code class="html plain">*{padding:0; margin:0; border:0;}</code></p> <p class="line number8 index7 alt1"><code class="html spaces">        </code><code class="html plain">.father{</code></p> <p class="line number9 index8 alt2"><code class="html spaces">            </code><code class="html plain">width: 500px;</code></p> <p class="line number10 index9 alt1"><code class="html spaces">            </code><code class="html plain">height: 500px;</code></p> <p class="line number11 index10 alt2"><code class="html spaces">            </code><code class="html plain">background: red;</code></p> <p class="line number12 index11 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number13 index12 alt2"><code class="html spaces">        </code><code class="html plain">.son{</code></p> <p class="line number14 index13 alt1"><code class="html spaces">            </code><code class="html plain">width: 100px;</code></p> <p class="line number15 index14 alt2"><code class="html spaces">            </code><code class="html plain">height: 100px;</code></p> <p class="line number16 index15 alt1"><code class="html spaces">            </code><code class="html plain">background: green;</code></p> <p class="line number17 index16 alt2"><code class="html spaces">            </code><code class="html plain">margin-top: 100px;</code></p> <p class="line number18 index17 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number19 index18 alt2"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number20 index19 alt1"><code class="html plain"></</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number21 index20 alt2"><code class="html plain"><</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number22 index21 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="father"></code></p> <p class="line number23 index22 alt2"><code class="html spaces">        </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="son">高度为100px,margin-top为100px。</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number24 index23 alt1"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number25 index24 alt2"><code class="html plain"></</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number26 index25 alt1"><code class="html plain"></</code><code class="html keyword">html</code><code class="html plain">></code></p> </td> </tr></tbody></table> <p>  这个例子我设置了margin-top为100px,效果如下:</p> <p><img src="https://img.php.cn/upload/article/000/000/007/b14182e42c329966dffd9ff54c3928c5-8.png" alt="" style="max-width:90%" style="max-width:90%"></p> <p><strong>这并不是我们想要的效果啊,我们希望子元素的上部距离父元素的上部为100px,可是我们看到的却是父元素的上部距离浏览器页面的上部有100px的距离,这是为什么呢?哪里出现问题了呢?</strong></p> <p><strong>  </strong></p> <p><strong>  实际上这是因为当父元素没有设置padding值以及border值时,出现了一个bug--父元素的上方与子元素的上方完全重合在了一起,无法分开。所以才会导致上述这种父元素和子元素同时向下的情况。</strong></p> <p><strong>  对于这种问题解决方法有下面几种:</strong></p> <ul class=" list-paddingleft-2"> <li><p><strong>方法一:给父元素添加<a href="http://www.php.cn/wiki/949.html" target="_blank">padding-top</a>值</strong></p></li> <li><p><strong>方法二:给父元素添加border值</strong></p></li> <li><p><strong>方法三:给父元素添加属性overflow:hidden;</strong></p></li> <li><p><strong>方法四:给父元素或者子元素声明浮动float</strong></p></li> <li><p><strong>方法五:使父元素或子元素声明为<a href="http://www.php.cn/code/6074.html" target="_blank">绝对定位</a>:position:absolute;</strong></p></li> <li><p><strong>方法六:给父元素添加属性</strong> overflow:auto; positon:relative;(注:此方法为后续添加,感谢博友@小精灵Pawn提供此方法)</p></li> </ul> <p>  <strong>方法一:基于例6,在父元素的css代码中添加padding-top:1px;效果如下:</strong></p> <p><strong><img src="https://img.php.cn/upload/article/000/000/007/b14182e42c329966dffd9ff54c3928c5-9.png" alt="" style="max-width:90%" style="max-width:90%"></strong></p> <p> </p> <p><strong>方法的唯一缺点就是增加了1px的误差。</strong></p> <p><strong>  </strong></p> <p><strong>  方法二:基于例6,在父元素的css代码中添加border-top:1px solid transparent;效果如下:</strong></p> <p><strong><img src="https://img.php.cn/upload/article/000/000/007/424a4b2edfa6594d295cefdcd1ee676f-10.png" alt="" style="max-width:90%" style="max-width:90%"></strong></p> <p> </p> <p><strong>同样达到了效果, 缺点同方法一。</strong></p> <p><strong>  </strong></p> <p><strong>  方法三:基于例6,在父元素的css代码中添加overflow:hidden;效果如下:</strong></p> <p><strong> <strong><img src="https://img.php.cn/upload/article/000/000/007/424a4b2edfa6594d295cefdcd1ee676f-11.png" alt="" style="max-width:90%" style="max-width:90%"></strong></strong></p> <p> </p> <p><strong>同样达到了效果,并且没有任何误差的存在。堪称perfect!!!!</strong></p> <p><strong>  </strong></p> <p><strong>  方法四:给父元素或者子元素声明float;基于例6,在子元素css代码添加float:left;或者在父元素css代码添加float:left;均达到效果,这里不再展示相同的图片。</strong></p> <p><strong>    优点:没有像素的误差。   缺点:float有时是不必要的。</strong></p> <p> </p> <p><strong>  方法五:给父元素或者子元素添加position:absolute;属性。 同样达到效果。</strong></p> <p><strong>    优点:同方法四。  且只要我们不使用top和left也不会有任何影响,所以这也是一种不错的方法。</strong></p> <p>(说明:博友 @laden666666 指出,上述方法三、四、五实际上都是去除子元素margin穿透父容器的方法,可以归类为bfc法,本质相同。 在此表示感谢) </p> <p>      <strong>方法六:给父元素添加overflow:auto;和position:relative;同样达到效果。</strong></p> <p><strong>      此方法亲测有效,是博友 @小精灵Pawn提供,在此表示感谢。</strong></p> <p> </p> <p> </p> <h2>第四部分:margin值的单位为%时的几种情况</h2> <p>    之前我举例子时使用margin,它的值都是以px为单位的,这个理解起来没有问题。<strong>但是如果margin值是以%为单位呢?实际上这时候百分比(%)是相对于该元素的父元素(容器),对于同级元素和父子元素都是如此。(</strong>再次感谢 博友@小精灵Pawn 提供的建议!!基于此建议补充这部分内容) 但是在同级元素中使用竖直方向的margin时会出现意想不到的结果,下面举例说明。</p> <h3>  (1)同级元素在水平方向使用值为%的margin</h3> <p>  例7:</p> <p> </p> <p class="cnblogs_Highlighter sh-gutter"><br></p> <p class="syntaxhighlighter html"><br></p> <table border="0"><tbody><tr class="firstRow"> <td class="gutter"> <p class="line number1 index0 alt2">1</p> <p class="line number2 index1 alt1">2</p> <p class="line number3 index2 alt2">3</p> <p class="line number4 index3 alt1">4</p> <p class="line number5 index4 alt2">5</p> <p class="line number6 index5 alt1">6</p> <p class="line number7 index6 alt2">7</p> <p class="line number8 index7 alt1">8</p> <p class="line number9 index8 alt2">9</p> <p class="line number10 index9 alt1">10</p> <p class="line number11 index10 alt2">11</p> <p class="line number12 index11 alt1">12</p> <p class="line number13 index12 alt2">13</p> <p class="line number14 index13 alt1">14</p> <p class="line number15 index14 alt2">15</p> <p class="line number16 index15 alt1">16</p> <p class="line number17 index16 alt2">17</p> <p class="line number18 index17 alt1">18</p> <p class="line number19 index18 alt2">19</p> <p class="line number20 index19 alt1">20</p> <p class="line number21 index20 alt2">21</p> <p class="line number22 index21 alt1">22</p> <p class="line number23 index22 alt2">23</p> <p class="line number24 index23 alt1">24</p> <p class="line number25 index24 alt2">25</p> <p class="line number26 index25 alt1">26</p> <p class="line number27 index26 alt2">27</p> <p class="line number28 index27 alt1">28</p> </td> <td class="code"> <p class="container"><br></p> <p class="line number1 index0 alt2"><code class="html plain"><</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number2 index1 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">meta</code> <code class="html plain">charset="UTF-8"></code></p> <p class="line number3 index2 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">title</code><code class="html plain">>margin</</code><code class="html keyword">title</code><code class="html plain">></code></p> <p class="line number4 index3 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number5 index4 alt2"><code class="html spaces">        </code><code class="html plain">*{</code></p> <p class="line number6 index5 alt1"><code class="html spaces">            </code><code class="html plain">margin:0;</code></p> <p class="line number7 index6 alt2"><code class="html spaces">            </code><code class="html plain">padding:0;</code></p> <p class="line number8 index7 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number9 index8 alt2"><code class="html spaces">        </code><code class="html plain">.first{</code></p> <p class="line number10 index9 alt1"><code class="html spaces">            </code><code class="html plain">float: left;</code></p> <p class="line number11 index10 alt2"><code class="html spaces">            </code><code class="html plain">width: 200px;</code></p> <p class="line number12 index11 alt1"><code class="html spaces">            </code><code class="html plain">height: 200px;</code></p> <p class="line number13 index12 alt2"><code class="html spaces">            </code><code class="html plain">background: green;</code></p> <p class="line number14 index13 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number15 index14 alt2"><code class="html spaces">        </code><code class="html plain">.second{</code></p> <p class="line number16 index15 alt1"><code class="html spaces">            </code><code class="html plain">float: left;</code></p> <p class="line number17 index16 alt2"><code class="html spaces">            </code><code class="html plain">width: 200px;</code></p> <p class="line number18 index17 alt1"><code class="html spaces">            </code><code class="html plain">height: 200px;</code></p> <p class="line number19 index18 alt2"><code class="html spaces">            </code><code class="html plain">background: red;</code></p> <p class="line number20 index19 alt1"><code class="html spaces">            </code><code class="html plain">margin-left: 20%;</code></p> <p class="line number21 index20 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number22 index21 alt1"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number23 index22 alt2"><code class="html plain"></</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number24 index23 alt1"><code class="html plain"><</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number25 index24 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="first">宽为200,无margin</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number26 index25 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="second">宽为200,margin-left为20%;</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number27 index26 alt2"><code class="html plain"></</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number28 index27 alt1"><code class="html plain"></</code><code class="html keyword">html</code><code class="html plain">></code></p> </td> </tr></tbody></table> <p> </p> <p>这个例子中,设置两个元素向左浮动,以便于观察两者水平方向的margin。其中左边p无margin,右边p的margin-left为20%,效果如下:</p> <p><img src="https://img.php.cn/upload/article/000/000/007/424a4b2edfa6594d295cefdcd1ee676f-12.gif" alt="" style="max-width:90%" style="max-width:90%"></p> <p><strong>从效果图可以看出两个p之间的间距始终为父元素(这里右边p的父元素即为body,其宽度为浏览器宽度)的20%。</strong></p> <p> </p> <p> </p> <h3>  (2)同级元素在竖直方向使用值为%的margin</h3> <p>   根据例7的启发,我们可以猜想,如果在竖直方向上使用margin,且值的单位为%,那么最终两者之间的距离将是父元素(上例中为body)的百分数。那么究竟是不是这样呢?看下面的例子。</p> <p><strong>例8</strong></p> <p class="cnblogs_Highlighter sh-gutter"><br></p> <p class="syntaxhighlighter html"><br></p> <table border="0"><tbody><tr class="firstRow"> <td class="gutter"> <p class="line number1 index0 alt2">1</p> <p class="line number2 index1 alt1">2</p> <p class="line number3 index2 alt2">3</p> <p class="line number4 index3 alt1">4</p> <p class="line number5 index4 alt2">5</p> <p class="line number6 index5 alt1">6</p> <p class="line number7 index6 alt2">7</p> <p class="line number8 index7 alt1">8</p> <p class="line number9 index8 alt2">9</p> <p class="line number10 index9 alt1">10</p> <p class="line number11 index10 alt2">11</p> <p class="line number12 index11 alt1">12</p> <p class="line number13 index12 alt2">13</p> <p class="line number14 index13 alt1">14</p> <p class="line number15 index14 alt2">15</p> <p class="line number16 index15 alt1">16</p> <p class="line number17 index16 alt2">17</p> <p class="line number18 index17 alt1">18</p> <p class="line number19 index18 alt2">19</p> <p class="line number20 index19 alt1">20</p> <p class="line number21 index20 alt2">21</p> <p class="line number22 index21 alt1">22</p> <p class="line number23 index22 alt2">23</p> <p class="line number24 index23 alt1">24</p> <p class="line number25 index24 alt2">25</p> <p class="line number26 index25 alt1">26</p> </td> <td class="code"> <p class="container"><br></p> <p class="line number1 index0 alt2"><code class="html plain"><</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number2 index1 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">meta</code> <code class="html plain">charset="UTF-8"></code></p> <p class="line number3 index2 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">title</code><code class="html plain">>margin</</code><code class="html keyword">title</code><code class="html plain">></code></p> <p class="line number4 index3 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number5 index4 alt2"><code class="html spaces">        </code><code class="html plain">*{</code></p> <p class="line number6 index5 alt1"><code class="html spaces">            </code><code class="html plain">margin:0;</code></p> <p class="line number7 index6 alt2"><code class="html spaces">            </code><code class="html plain">padding:0;</code></p> <p class="line number8 index7 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number9 index8 alt2"><code class="html spaces">        </code><code class="html plain">.first{</code></p> <p class="line number10 index9 alt1"><code class="html spaces">            </code><code class="html plain">width: 200px;</code></p> <p class="line number11 index10 alt2"><code class="html spaces">            </code><code class="html plain">height: 200px;</code></p> <p class="line number12 index11 alt1"><code class="html spaces">            </code><code class="html plain">background: green;</code></p> <p class="line number13 index12 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number14 index13 alt1"><code class="html spaces">        </code><code class="html plain">.second{</code></p> <p class="line number15 index14 alt2"><code class="html spaces">            </code><code class="html plain">width: 200px;</code></p> <p class="line number16 index15 alt1"><code class="html spaces">            </code><code class="html plain">height: 200px;</code></p> <p class="line number17 index16 alt2"><code class="html spaces">            </code><code class="html plain">background: red;</code></p> <p class="line number18 index17 alt1"><code class="html spaces">            </code><code class="html plain">margin-top: 10%;</code></p> <p class="line number19 index18 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number20 index19 alt1"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number21 index20 alt2"><code class="html plain"></</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number22 index21 alt1"><code class="html plain"><</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number23 index22 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="first">高为200,无margin</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number24 index23 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="second">高为200,margin-top为20%;</</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number25 index24 alt2"><code class="html plain"></</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number26 index25 alt1"><code class="html plain"></</code><code class="html keyword">html</code><code class="html plain">></code></p> </td> </tr></tbody></table> <p>这里设置上面的p无margin,下面的p的margin-top为10。效果如下:</p> <p><img src="https://img.php.cn/upload/article/000/000/007/73737215d222f61d1e7c7f1447c45d1b-13.gif" alt="" style="max-width:90%" style="max-width:90%"></p> <p><strong> </strong></p> <p><strong>我们发现,当我在缩小浏览器的高度时,竖直方向上的间距并没有缩小!!! 而当我缩小浏览器的宽度时,竖直方向上的距离缩小了!!!</strong></p> <p><strong>这就说明:统计元素之间在竖直方向上使用margin,当值的单位为%时,它是相对于父元素的宽度。</strong></p> <p><strong>  那么这里为什么不是如我们所希望的那样相对于浏览器的高度呢?知乎上有大神是这样解释的(原文地址:https://www.zhihu.com/question/20983035?rf=27109182):</strong></p> <p><strong><img src="https://img.php.cn/upload/article/000/000/007/ba8858fe462ad9a8fb9cdcbc308e9ef1-14.png" alt="" style="max-width:90%" style="max-width:90%"></strong></p> <p> </p> <h3> </h3> <h3> </h3> <h3>  (3)父子元素使用值为%的margin</h3> <p>    <strong>对于父子元素,如果在子元素中使用单位为%margin,那么这个margin值是相对于父元素的宽度和高度(注意:这时的确是相对于父元素的高度!)的。</strong></p> <p>        <strong>例9  </strong></p> <p><strong>    </strong>代码如下:</p> <p class="cnblogs_Highlighter sh-gutter"><br></p> <p class="syntaxhighlighter html"><br></p> <table border="0"><tbody><tr class="firstRow"> <td class="gutter"> <p class="line number1 index0 alt2">1</p> <p class="line number2 index1 alt1">2</p> <p class="line number3 index2 alt2">3</p> <p class="line number4 index3 alt1">4</p> <p class="line number5 index4 alt2">5</p> <p class="line number6 index5 alt1">6</p> <p class="line number7 index6 alt2">7</p> <p class="line number8 index7 alt1">8</p> <p class="line number9 index8 alt2">9</p> <p class="line number10 index9 alt1">10</p> <p class="line number11 index10 alt2">11</p> <p class="line number12 index11 alt1">12</p> <p class="line number13 index12 alt2">13</p> <p class="line number14 index13 alt1">14</p> <p class="line number15 index14 alt2">15</p> <p class="line number16 index15 alt1">16</p> <p class="line number17 index16 alt2">17</p> <p class="line number18 index17 alt1">18</p> <p class="line number19 index18 alt2">19</p> <p class="line number20 index19 alt1">20</p> <p class="line number21 index20 alt2">21</p> <p class="line number22 index21 alt1">22</p> <p class="line number23 index22 alt2">23</p> <p class="line number24 index23 alt1">24</p> <p class="line number25 index24 alt2">25</p> <p class="line number26 index25 alt1">26</p> <p class="line number27 index26 alt2">27</p> <p class="line number28 index27 alt1">28</p> <p class="line number29 index28 alt2">29</p> <p class="line number30 index29 alt1">30</p> <p class="line number31 index30 alt2">31</p> </td> <td class="code"> <p class="container"><br></p> <p class="line number1 index0 alt2"><code class="html plain"><!</code><code class="html keyword">DOCTYPE</code> <code class="html plain">html></code></p> <p class="line number2 index1 alt1"><code class="html plain"><</code><code class="html keyword">html</code> <code class="html plain">lang="en"></code></p> <p class="line number3 index2 alt2"><code class="html plain"><</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number4 index3 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">meta</code> <code class="html plain">charset="UTF-8"></code></p> <p class="line number5 index4 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">title</code><code class="html plain">>Document</</code><code class="html keyword">title</code><code class="html plain">></code></p> <p class="line number6 index5 alt1"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number7 index6 alt2"><code class="html spaces">        </code><code class="html plain">*{</code></p> <p class="line number8 index7 alt1"><code class="html spaces">            </code><code class="html plain">margin:0;</code></p> <p class="line number9 index8 alt2"><code class="html spaces">            </code><code class="html plain">padding:0;</code></p> <p class="line number10 index9 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number11 index10 alt2"><code class="html spaces">        </code><code class="html plain">.father{</code></p> <p class="line number12 index11 alt1"><code class="html spaces">            </code><code class="html plain">width: 500px;</code></p> <p class="line number13 index12 alt2"><code class="html spaces">            </code><code class="html plain">height: 300px;</code></p> <p class="line number14 index13 alt1"><code class="html spaces">            </code><code class="html plain">background: red;</code></p> <p class="line number15 index14 alt2"><code class="html spaces">            </code><code class="html plain">overflow: hidden;</code></p> <p class="line number16 index15 alt1"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number17 index16 alt2"><code class="html spaces">        </code><code class="html plain">.son{</code></p> <p class="line number18 index17 alt1"><code class="html spaces">            </code><code class="html plain">width: 100px;</code></p> <p class="line number19 index18 alt2"><code class="html spaces">            </code><code class="html plain">height: 100px;</code></p> <p class="line number20 index19 alt1"><code class="html spaces">            </code><code class="html plain">background: green;</code></p> <p class="line number21 index20 alt2"><code class="html spaces">            </code><code class="html plain">margin-top: 20%;</code></p> <p class="line number22 index21 alt1"><code class="html spaces">            </code><code class="html plain">margin-left: 20%;</code></p> <p class="line number23 index22 alt2"><code class="html spaces">        </code><code class="html plain">}</code></p> <p class="line number24 index23 alt1"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">style</code><code class="html plain">></code></p> <p class="line number25 index24 alt2"><code class="html plain"></</code><code class="html keyword">head</code><code class="html plain">></code></p> <p class="line number26 index25 alt1"><code class="html plain"><</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number27 index26 alt2"><code class="html spaces">    </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="father"></code></p> <p class="line number28 index27 alt1"><code class="html spaces">        </code><code class="html plain"><</code><code class="html keyword">p</code> <code class="html plain">class="son"></</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number29 index28 alt2"><code class="html spaces">    </code><code class="html plain"></</code><code class="html keyword">p</code><code class="html plain">></code></p> <p class="line number30 index29 alt1"><code class="html plain"></</code><code class="html keyword">body</code><code class="html plain">></code></p> <p class="line number31 index30 alt2"><code class="html plain"></</code><code class="html keyword">html</code><code class="html plain">></code></p> </td> </tr></tbody></table> <p>  在这个例子中,我设置了margin-left的值为20%,margin-top的值为20%,<strong>父元素的width为500px,父元素的height为300px。</strong>下面看看效果吧。</p> <p><img src="https://img.php.cn/upload/article/000/000/007/856d2f11c3d8af9a226d1a5f5414cef7-15.png" alt="" style="max-width:90%" style="max-width:90%"></p> <p>从上图可以看出<strong>子元素的margin-top值最终同样是相对与父元素的宽度而非高度。</strong></p>

Atas ialah kandungan terperinci 深入理解css中的margin属性. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn