为什么img的margin-top无效,margin-bottom有效?img已经设置为block。

恋喵大鲤鱼
博客专家认证
2016-09-19 03:14:59
为什么img的margin-top无效,margin-bottom有效?img已经设置为block。具体见下面的代码:

<style type="text/css">
#albumContainer{background-color:rgba(0,0,0,0.1);border-radius:10px;width:750px;margin:30px auto;}
#albumContainer img{display:block;width:25%;margin:10px auto;}
</type>

<li id="iLi2">
<div id="albumContainer">
<img alt="album" src="res/img/album001.png">
<ul id="albumList">
<li id="family">
<img alt="相册" src="res/img/photo/family/family0.jpg">
<span>family</span>
</li>
<li id="sister">
<img alt="相册" src="res/img/photo/sister/sister0.jpg">
<span>dear sister</span>
</li>
<li id="cat">
<img alt="相册" src="res/img/photo/cat/cat0.jpg">
<span>big lazy cat</span>
</li>
<li id="me">
<img alt="相册" src="res/img/photo/me/me000.png">
<span>me</span>
</li>
</ul>
</div>
</li>

显示的结果为:


为什么在上面对img使用了margin:10px auto;对下外边距有效,对上外边距却没有效果呢?
...全文
1073 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
给谁看 2016-09-20
  • 打赏
  • 举报
回复
楼上说的对,两种方法 1:使用padding-top 2:给父元素overflow:hidden
恋喵大鲤鱼 2016-09-20
  • 打赏
  • 举报
回复
引用 1 楼 jslang 的回复:
这是外边距合并特性 http://www.w3school.com.cn/css/css_margin_collapsing.asp 解决方法是为父元素设置overflow:hidden;
引用 2 楼 u010080006 的回复:
楼上说的对,两种方法 1:使用padding-top 2:给父元素overflow:hidden
正解,也谢谢楼上的指点。
天际的海浪 2016-09-19
  • 打赏
  • 举报
回复
这是外边距合并特性 http://www.w3school.com.cn/css/css_margin_collapsing.asp 解决方法是为父元素设置overflow:hidden;

61,129

社区成员

发帖
与我相关
我的任务
社区描述
层叠样式表(英文全称:Cascading Style Sheets)是一种用来表现HTML(标准通用标记语言的一个应用)或XML(标准通用标记语言的一个子集)等文件样式的计算机语言。
社区管理员
  • HTML(CSS)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧