绝对定位的位置问题

为什么设置绝对定位和bottom:0之后,

标签却不在父元素的底部?之后加了margin:0后才可以?

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <style type="text/css">
        .is{position: relative;width: 480px;}
        .childs{background-color: orangered;height: 200px;}
        .is .title{position: absolute;bottom:0;width: 100%;line-height: 45px;background-color: #000;opacity: 0.7;}
        .is .title a{margin-left: 20px;color: #fff;text-decoration: none;}
    </style>
</head>
<body>
    <div class="is">
      <div class="childs">去哪儿</div>
      <p class="title"><a href="#">视线</a></p>
    </div>
</body>
</html>
阅读 3.8k
3 个回答

p标签在浏览器环境下默认带有margin属性,而绝对定位并不影响margin值起作用。
可以看一下这篇文章:浅谈块元素绝对定位的margin属性

刚开始学习css布局的时候,先上手熟悉浏览器的开发者工具,比如chrome浏览器自带的,或者火狐浏览器的Firebug。Window下F12或Mac下option+command+i调出。通过开发者工具,自己就能学着分析碰到的百分之八九十的布局问题了……强烈建议!

因为p默认有margin值

题主看这个图,chrome调出开发者工具,然后选中这个p元素,你会发现
图片描述

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题