html - 使用 object-fit 将 div 放在图像的顶部

标签 html css image

我想在这张图片(纸杯蛋糕)上放置一个 div。 enter image description here

例如,我想把我的div放在红樱桃上面,像这样enter image description here

因为我的图像具有动态宽度和高度,并且我在我的 css 中使用了 object-fit: cover;, 所以当我的图像调整大小时,div 位置必须相应地移动。

如果我的图像具有固定尺寸,使用 position: absolute; 即可。但是,在我的图像中使用 object-fit: cover; 时,我的图像会随着用户调整大小而四处移动。调整大小时如何使我的 div 出现在准确位置?

谢谢。如果我的问题很乱,你无法理解,请告诉我,因为我的母语不是英语。

最佳答案

试试这个,要获得 div 的准确顶部和中心定位,您可以使用 CSS calc() 函数

#box{
  width:auto;
  height:auto;
  position:relative;
  overflow:hidden;
}
img{
  width:100%;
  min-height:100%;
  object-fit:cover;
}
#box > .bx{
  position:absolute;
  width:60px;
  height:60px;
  background:#111;
  top:5%;
  left:calc(100% - 60%);
  color:#fff;
}
<div id="box">
<div class="bx">Text</div>
<img src="http://i.stack.imgur.com/7Mj25.jpg">
</div>

关于html - 使用 object-fit 将 div 放在图像的顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39487960/

相关文章:

javascript - 消除 Internet Explorer 中 Thread.Sleep 上的死锁?

javascript - 变量如何将其值绑定(bind)到 DOM?

html - Bootstrap div 拉伸(stretch)到页面底部

php - 以编程方式检查图像元素颜色和颜色百分比

c - 使用 sdl 进行像素操作

java - 有哪些好的java图像处理工具或API?

javascript - 在这些条件下使用 Javascript 或 jquery 滚动到页面底部

javascript - 在 ng-repeat 中的 ng-click 上添加多个类到选定的行

css - Bootstrap 行 map 宽度最大化

javascript - CSS 用省略号填充表 TD 内的空白