Position SVG elements over an image

后端 未结 1 406
逝去的感伤
逝去的感伤 2021-02-14 16:03

Is it possible to have the following elements and style them so that the SVG objects appear over the image (i.e. like part of the image)?

Currently they are displayed b

相关标签:
1条回答
  • 2021-02-14 16:28

    Here's a generic example of how to do an image overlay. Basically you wrap the image and the overlay content in a relative positioned element and then absolute position the overlay content.

    .img-overlay-wrap {
      position: relative;
      display: inline-block; /* <= shrinks container to image size */
      transition: transform 150ms ease-in-out;
    }
    
    .img-overlay-wrap img { /* <= optional, for responsiveness */
       display: block;
       max-width: 100%;
       height: auto;
    }
    
    .img-overlay-wrap svg {
      position: absolute;
      top: 0;
      left: 0;
    }
    
    .img-overlay-wrap:hover {
      transform: rotate( 15deg );
    }
    <div class="img-overlay-wrap">
    
      <img src="http://placehold.it/400x400/fc0">
      <svg viewBox="0 0 200 200">
        <circle cx="75" cy="75" r="50" fill="rebeccapurple"/>
      </svg>
    
    </div>

    Added a bit-o rotation fun since you mentioned rotation (might be different than what you intended).

    0 讨论(0)
提交回复
热议问题