html - SVG + CSS : sizing one image inside another

标签 html css svg

目标是将一张图像放入另一张图像中。

iPhone 边框和屏幕截图均为 1242x2688。框架的内边距为 76(左/右)和 74(上/下)。然而这些值仍然在屏幕截图的顶部和底部之间留下垂直间隙。为什么?

代码笔:https://codepen.io/anon/pen/vvoKjO?editors=1000

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: rgba(0, 0, 0, 0);
}

.elemBox,
.backgroundBox.design,
.elemBox>* {
  position: absolute;
}

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

.elemBox.graphic img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
<svg id="designBox" width="100%" height="100%" viewBox="0 0 1242 2688">
  <foreignObject width="100%" height="100%">
      <img class="backgroundBox design" src="/images/general/transparent.gif" style="left:0%; top:0%; width:100%; height:100%; background:#00B9FC">
      <div class="elemBox graphic movable" style="left: 0px; width: 1242px; height: 2688px; top: 0px;" id="g3kIEZGGog71">
        <div class="foregroundBox" style="top: 74px; left: 76px; width: 1090px; height: 2540px;">
          <img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2707120/Screenshot.png">
        </div>
        <img class="backgroundBox" src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2707120/XS_Max_Space_GrayNEW.png">
    </div>
  </foreignObject>
</svg>

最佳答案

您的图像尺寸为 2688x1242 和 1792x828。得出的比率为 2.1642。

在另一边,在 foregroundBox 中,将尺寸设置为 2540x1090,比率为 2.33

然后,object-fit: contains 将强制调整大小,因为比例不合适。

您需要将 foregroundBox 尺寸更改为与图像比例相同的尺寸,一切都会好起来的

关于html - SVG + CSS : sizing one image inside another,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54266460/

相关文章:

css - Atom 无法识别 CSS 网格命令 "justify-items"和 "justify-self"。为什么?

html - Bootstrap,从导航栏中删除响应

html - 使用文本作为单选按钮/将字符串变成单选按钮

html - 带有纯 html 和 css 的子菜单消失了

css - 带有 svg 和 css 的动画背景

colors - SVG 填充颜色透明度/alpha?

javascript - 如何在 d3 v4 中绘制带有点数组的圆弧

javascript - 获取 html 节点的 HREF

javascript - 在使用脚本执行 php 时加载微调器图像

java - 我们可以使用 Wicket :id to make CSS?