【问题标题】:div container shrinks after position: absolute [duplicate]div容器在位置后缩小:绝对[重复]
【发布时间】: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: “我认为让父 &lt;div&gt; 相对和子绝对应该在 &lt;div&gt; 中保留徽标”。请指出任何说明这将发生或应该发生的文档。 CSS 中最常见的缺陷之一是在不知道(或研究)每个属性的情况下使用属性。出于最好的意图并希望我听起来不粗鲁,我必须提醒您,您应该阅读有关您正在使用的技术的基本文档和标准,并且仅在发生的情况与您在文档中找到的内容发生冲突时才在此处询问。

标签: css


【解决方案1】:

是的,它应该表现得像这样,因为position: absolute 和position: fixed 不会从它的父级或容器中发生。当您使用这两个 css 属性时,它们会超出常规文档的范围。这就是为什么父处理内部没有内容并且高度不会自动增加,直到通过 height: 100px(例如)或 min-height: 100px(例如)等 css 属性显式设置您的高度。

根据您当前的结构,您可以设置基于最小高度的徽标。

header {
    min-height: 100px; // 100px here logo size assumed
}

或者保持logo图片的相对位置:

#logo img {
   position: relative;
}

【讨论】:

  • 谢谢。我忘记了位置值的变化会扰乱文档流。
【解决方案2】:

我会避免绝对位置,如果您将图像包装在 div 中,您可以将图像居中。

    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;
    max-width: 220px;
    overflow: hidden;
}

#logo figure {
  width:52%;
  margin: 0 auto;
}

#logo img {
    width: 100%;
    position:relative;
    border-style: solid;
    border-color: red;
    border-width: 2px;
    overflow: hidden
}
<body>
    <div class="wrapper">
        <header>
            <div id="logo">
               <figure>
                  <img src="https://upload.wikimedia.org/wikipedia/en/9/9d/Pepsilogo.png">
               </figure>
            </div>
        </header>
    </div>
</body>

img 将占用由figure 分配的总空间的 100%。

您可以更改figure 的宽度,然后图像会做出响应,并保持居中。

【讨论】:

  • 我不确定向包装器添加另一个包装器是否有助于解决 OP 的问题。
猜你喜欢
  • 1970-01-01
  • 2013-12-16
  • 2020-07-11
  • 1970-01-01
  • 2011-07-30
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
相关资源
最近更新 更多