You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome正常Firefox异常:object-fit与margin:auto兼容的CSS替代方案求助

问题:Firefox中图片定位Pin偏移的纯CSS解决方案

现有CSS代码在Chrome中表现符合预期,但在Firefox中存在问题:调整视口尺寸时,图片上的定位pin无法保持正确位置,会出现偏移。推测是margin:auto与object-fit:contain的计算方式差异导致。已知可以用JavaScript实现,但希望找到纯CSS的解决办法。

HTML代码

<div class="site">
  <div class="wrapper">
    <div class="title">TITLE</div>
    <div class="subtitle">Subtitle</div>
    <div class="container">
      <div class="img-wrapper">
        <img src="./img.jpg" alt="">
      </div>
      <div class="pin-container">
        <div class="pin pin1">?</div>
        <div class="pin pin2">?</div>
        <div class="pin pin3">?</div>
        <div class="pin pin4">?</div>
      </div>
    </div>
  </div>
</div>

CSS代码

.site{
  position: relative;
  width: 100%;
  height: 80vh;
  display: flex;
  flex-direction: column;
  margin: 0 auto;
  max-width: 960px;
  border: 1px solid blue;
}

.wrapper{
  display: flex;
  flex-direction: column;
}

.container{
  position: relative;
  border-radius: 6px;
  margin: auto;
}

.img-wrapper{
  height: 100%;
}

.img-wrapper img{
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.pin-container{
  position: absolute;
  color: red;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.pin{
  position: absolute;
  background-color: white;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  text-align: center;
  transform: translate(-50%, -50%);
}

.pin1 {
  top: 50%;
  left: 24%;
}
.pin2 {
  top: 30%;
  left: 52%;
}
.pin3 {
  top: 87%;
  left: 79%;
}
.pin4 {
  top: 100%;
  left: 100%;
}

内容的提问来源于stack exchange,提问作者Metiu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 21:31:11