skip to Main Content

I have two images, a JPG and an SVG, that must be overlaid, the JPG on top of the SVG. They both must resize exactly proportionally as the viewport dimensions change, and not just at specific breakpoints, but continually. The JPG is an image in a figure (<figure><img/></figure>) tag set and the SVG is a CSS background: url(/url/to/image.svg). The background image looks like a picture frame around the <figure> image, so to keep them looking like they’re one unit, it’s critical that they each maintain the same aspect ratio as the viewport resizes to any dimension.

I am at a loss as to how to make this happen without either or both getting out of sync and the <figure> image is overlapping the background, or the background is getting too large for the <figure>. Here’s what I’ve tried.

<figure class="outline-img">

    <img 
        width="1024" height="682" 
        src="//localhost:8181/wp-content/uploads/2021/08/istockphoto-1034140728-1024x1024-1.jpeg" 
        class="attachment-large size-large" 
        alt="" 
        loading="lazy" 
        data-src="//localhost:8181/wp-content/uploads/2021/08/istockphoto-1034140728-1024x1024-1.jpeg" 
        data-srcset="//localhost:8181/wp-content/uploads/2021/08/istockphoto-1034140728-1024x1024-1.jpeg 1024w, //localhost:8181/wp-content/uploads/2021/08/istockphoto-1034140728-1024x1024-1-300x200.jpeg 300w, //localhost:8181/wp-content/uploads/2021/08/istockphoto-1034140728-1024x1024-1-768x512.jpeg 768w" 
        data-sizes="(min-width: 960px) 75vw, 100vw"
        srcset="//localhost:8181/wp-content/uploads/2021/08/istockphoto-1034140728-1024x1024-1.jpeg 1024w, //localhost:8181/wp-content/uploads/2021/08/istockphoto-1034140728-1024x1024-1-300x200.jpeg 300w, //localhost:8181/wp-content/uploads/2021/08/istockphoto-1034140728-1024x1024-1-768x512.jpeg 768w" 
        sizes="(min-width: 960px) 75vw, 100vw">
            
</figure>

This is the CSS for the background as it’s currently constituted:

.outline-img {
    padding: 9.4%;
    width: 118%!important;
    background: url(/wp-content/uploads/2021/09/frame.svg);
    background-repeat: space;
    background-position: 50%;
}

The weird padding and width values are how I’m currently attempting to "hold" the image within the SVG image’s dimensions. Any padding less than that clips the outer edges of the SVG image. Any width less than that makes the image not fit in the SVG’s inner edges. And when I resize the browser, the framed image doesn’t "stick" to the inner dimensions of the SVG.

I’d love to know if there’s an easier way to do this. I tried suggesting making both of these into one JPG image, but the designer is interested in this solution primarily.

3

Answers


  1. You could try in 2 different ways:

    1. Merge the 2 images together in a unique image, asking the designer again, that would save you time. svg and jpg behave differently

    2. if 1. is not an option, convert the .jpg image in .png and choosing the same size of the svg, the image itself will be the same but the transparency will give you the same size. (the important here is actually the aspect ratio)

    In both case, add the property object-fit:contain to keep the aspect ratio during scaling and leave width and height set to auto

    Going in the direction of using the same images the designer provided will lead to not necessary extra code, plus, even keeping the same aspect ratio, you should take care of the positioning during scale, another hassle which is going to create you problem with particular devices like iPadPro not being exactly targeted from more than one CSS framework around.

    Login or Signup to reply.
  2. Let’s look at the question a little differently.
    We have a picture and we need to make it a responsive frame.
    But this frame does not have to be one picture.

    Instead of one frame, we make a set of images from which, using css, we will assemble a responsive frame around the image.

    What you need to do:

    Cut the frame into 4 corner pieces.

    We also make 4 elements for the top / bottom and left / right edges.

    Next, we make a table like this
    https://jsfiddle.net/n5zfue27/1/

    This solution supports any browser including IE6.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    
      <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style type="text/css">
          html,
          body {
            height: 100%;
            margin: 0;
          }
    
          table {
            table-layout: fixed;
    
            border-collapse: collapse;
            padding: 0;
            margin: 0;
          }
    
          table td {
            padding: 0;
            border: 1px solid black;
          }
    
          .left-top,
          .left-down,
          .right-top,
          .right-down,
          .left,
          .right {
            height: 30px;
            width: 30px;
            background: red;
          }
    
          .left,
          .right {
            background-color: #e5e5f7;
            opacity: 0.8;
            background-image: linear-gradient(to right, #444cf7, #444cf7 5px, #e5e5f7 5px, #e5e5f7);
            background-size: 10px 100%;
            height: 100%;
          }
    
          .center-top,
          .center-down {
            background-color: #e5e5f7;
            opacity: 0.8;
            background-image: linear-gradient(0deg, #e5e5f7 50%, #444cf7 50%);
            background-size: 10px 10px;
    
          }
    
        </style>
      </head>
    
      <body>
        <table>
          <tr>
            <td class="left-top">
    
            </td>
            <td class="center-top">
    
            </td>
            <td class="right-top">
    
            </td>
          </tr>
          <tr>
            <td class="left">
    
            </td>
            <td class="center"><img style="width: 100%; display: block;" src="https://dunham-bush.com/wp-content/uploads/2020/01/Heron-Tower-London-900x556.jpg"></td>
            <td class="right">
    
            </td>
          </tr>
          <tr>
            <td class="left-down">
    
            </td>
            <td class="center-down">
    
            </td>
            <td class="right-down">
    
            </td>
          </tr>
        </table>
      </body>
    
    </html>
    
    Login or Signup to reply.
  3. I assume the dimension of the image remains same. You can try the following approach.

    figure {
      background: url(https://cdn.pixabay.com/photo/2017/11/10/16/59/frame-2936877_960_720.png) no-repeat;
      background-size: 100% 100%;
      padding: 5%; /*adjust for your need*/
      width: 30%; /*adjust for your need*/
    }
      
    img {
      display: block;
      width: 100%;
      height: auto;
    }
    <figure class="outline-img">
        <img width="1000" height="600" 
            src="https://dummyimage.com/600x1000/000/fff"/>
    </figure>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search