Heim  >  Artikel  >  Web-Frontend  >  margin的BUG(2)_html/css_WEB-ITnose

margin的BUG(2)_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:15:03916Durchsuche

      继续上一次的学习,这次又发现了margin的第二个bug.既当同时对两个相邻的div盒子设置margin时,他们之间的间隙不能正常显示.

    具体表述为:先建立两个div1和div2

<div class="div1"></div><div class="div2"></div>

div2设置成margin-top,div1设置margin-bottom,代码如下:

.div1{    width: 200px;height: 200px;    background: green;    margin-bottom: 50px;  }  .div2{    width: 200px;height: 200px;    background: blue;    margin-top: 80px;  }

  设置背景色和宽高,是为了方便比对观察

   那么两者之间的间隙会变成margin-top和margin-bottom比较出来的最大值.

  如果真想要间隙进行相加,则应把div2元素变为inline-block,代码如下:

.div2{    width: 200px;height: 200px;    background: blue;    margin-top: 80px;    display:inline-block;  }

 将div2转换为内联块属性后,两者之间的间隙就能正常相加而正确显示了.

 

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn