html - 元素在响应式 header 中的绝对定位

标签 html css css-position absolute

我正在尝试获得与此处完全相同的效果:Responsive images positioned over image ,所以我希望我的绝对定位元素保持在同一个位置,但在调整浏览器大小时会变小或变大。在使用此代码之前,我尝试了许多不同的可能性,但我不明白为什么我的包装容器 (id="wrapper") 被放置在图像下方。为了在图像上得到问号,我需要使用负百分比。在我从 stackoverflow 中的另一个问题复制的示例中,有 Bootstrap 样式。我不知道,但我不想使用 Bootstrap 。对于我做错的所有建议,我将不胜感激。

#wrapper {
    position: relative;
    display: inline;
}
#questionMark {
    position:absolute;
    left:33%;
    top:-43%;
    max-width: 10%;
}

这是我的 fiddle :https://jsfiddle.net/8obzf2c8/2/

最佳答案

inline 元素无法获取其中元素的高度。
您应该从 #wrapper 元素中删除 display: inline

#wrapper {
  position: relative;
  margin-top: 150px;
}
#questionMark {
  position:absolute;
  left:33%;
  top:-43%;
  max-width: 10%;
}
<div id=wrapper>
  <img src="http://e.allegroimg.pl/s400/19/53/89/71/60/5389716014"/>
  <img id="questionMark" src="https://image.freepik.com/free-icon/speech-bubble-with-question-mark_318-78800.png"/>
</div>

I also set margin-top to make sure the question mark image is in the viewport.

关于html - 元素在响应式 header 中的绝对定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39145792/

相关文章:

php - 浏览器忽略/覆盖 CSS block

javascript - 在HTML幻灯片中使用jQuery触发音频播放吗?

html - z-index 在另一个 div 中的单个 div

CSS:文本阴影 buggy ?

javascript - 你能将图像与位置 :relative with an image that has position:absolute? 对齐吗

css - 居中一个 div 垂直/水平同时调整它的大小

具有定位图层的背景图像上的 CSS 灰度

javascript - 使用 Jquery 过滤器检查重复的列数据

html - 列表样式随着内联 block 消失了——但我希望我的列表装饰器保留下来

css - 从 node_modules 到 angular-cli 的自定义字体