代码之家  ›  专栏  ›  技术社区  ›  Crashalot

SVG+CSS:调整图像内部的大小

  •  2
  • Crashalot  · 技术社区  · 6 年前

    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>
    3 回复  |  直到 6 年前
        1
  •  2
  •   vals    6 年前

    您的图像是2688x1242和1792x828。这个比率是2.1642。

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

    你需要把前接地盒的尺寸改成和图片比例一样的尺寸,一切都会好的

        2
  •  1
  •   Alexandr_TT    6 年前

    最好将图像添加到 svg svg格式 <image>

    HTML 所以没必要用 <foreignObject>

    图片大小不一,所以我们以智能手机的图片为基础,带有文字的图片大小会与第一张图片相适应。

    <image transform="translate(65 77) scale(0.9 0.99)"

    .container {
    width:25%;
    height:25%;
    
    }
    <div class="container">
    </style>
    <svg id="designBox" width="100%" height="100%" viewBox="0 0 1242 2688">
             
              <image transform="translate(65 77) scale(0.9 0.99)" width="100%" height="100%" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2707120/Screenshot.png"  />
    
             <image class="backgroundBox" width="100%" height="100%" xlink:href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/2707120/XS_Max_Space_GrayNEW.png" />
      
    </svg>
    </div>

    该应用程序可在所有现代浏览器中自适应工作。

        3
  •  0
  •   Mark Schultheiss    6 年前

    调整CSS,删除一些和简化一些应该可以解决这个问题。

    html,
    body,
    .backgroundBox,
    .elemBox.graphic img {
      width: 100%;
      height: 100%;
    }
    
    html,
    body {
      margin: 0;
      overflow: hidden;
      background: rgba(0, 0, 0, 0);
    }
    
    .elemBox,
    .backgroundBox.design,
    .elemBox>* {
      position: absolute;
    }
    <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>