Home  >  Article  >  Web Front-end  >  margin的BUG(2)_html/css_WEB-ITnose

margin的BUG(2)_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-24 11:15:03945browse

      继续上一次的学习,这次又发现了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转换为内联块属性后,两者之间的间隙就能正常相加而正确显示了.

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn