【发布时间】:2018-09-16 19:27:34
【问题描述】:
定位我的徽标后,<div> 容器缩小到“零”。
你能检查一下是什么问题吗?我希望它以#logo <div> 为中心。我认为让父母 <div> relative 和孩子 absolute 应该在 <div> 中保留徽标。
HTML
<body>
<div class="wrapper">
<header>
<div id="logo">
<img src="./img/logo.png">
</div>
</header>
</div>
</body>
CSS
body{
font-family: Arial, Helvetica, sans-serif;
font-size: 15px;
line-height: 1.5;
padding:0;
margin:0;
background-color: #DCB894;
}
.wrapper{
margin-right: auto;
margin-left: auto;
max-width: 960px;
padding-right: 10px;
padding-left: 10px;
border-style: solid;
border-color: blue;
border-width: 2px;
}
header {
margin-top: 10px;
background-color: #BCD34C;
}
#logo{
border-style: solid;
border-color: magenta;
border-width: 2px;
position: relative;
height: auto;
}
#logo img {
width: 150px;
height: auto;
position: absolute;
margin: auto;
border-style: solid;
border-color: red;
border-width: 2px;
}
【问题讨论】:
-
它会缩小或将自己定位在屏幕外吗?
-
由于包装器具有高度,并且唯一的元素是具有绝对位置的图像,因此它实际上不包含任何内容或像您说的那样“收缩”。
-
为什么需要绝对定位?简单地从 img 中删除
position不会解决所有问题吗?只需将display更改为inline-block即可使用边距。 -
Ref: “我认为让父
<div>相对和子绝对应该在<div>中保留徽标”。请指出任何说明这将发生或应该发生的文档。 CSS 中最常见的缺陷之一是在不知道(或研究)每个属性的情况下使用属性。出于最好的意图并希望我听起来不粗鲁,我必须提醒您,您应该阅读有关您正在使用的技术的基本文档和标准,并且仅在发生的情况与您在文档中找到的内容发生冲突时才在此处询问。
标签: css