Maintain aspect ratio of SVG image with fixed height using a slice technique

I'm having trouble with an SVG that needs to have a fixed height to avoid looking too big when the width exceeds 2000 pixels on widescreens. I want the clipping mask to always be visible and the background image to be sliced rather than stretched. So far, my attempts have been unsuccessful.

Here is my current code snippet: https://codepen.io/bucky208/pen/OEqbPp

div {
  width: 100%;
}
<div>
     <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1024 1381.5" preserveAspectRatio="none" style="display: block; position: absolute; width: 100%;height: 400px;">
        <g id="clipgroup">
          <defs>
            <polygon id="mask" points="0,572.1 0,1381.3 1024,1040.5 1024,337.6 0,0"/>
          </defs>
          <clipPath id="mask_1_">
            <use xlink:href="#mask"  style="overflow:visible;"/>
          </clipPath>
          <g style="clip-path:url(#mask_1_);">
              <image style="overflow:visible;" width="331" height="444" id="theimage" xlink:href="https://image.ibb.co/ipbNkJ/56_E0406_E5_C8_EF897.jpg"  transform="matrix(3.1119 0 0 3.1111 -3.0528 -2.604167e-04)"></image>
          </g>
        </g>
    </svg>
  </div>

Do I need another wrapper around everything? How can I restore the image ratio?

Many thanks to all those trying to assist me with this issue.

Answer №1

If you're looking to have image fills that fill their container while maintaining the original aspect ratio, using a filter along with objectBoundingBox sizing and preserveAspectRatio is key. The code below accomplishes what I believe you are trying to achieve:

svg {
  background: red;
}

#svgcont {
  position: absolute;
  width: 100%;
}
<div id="svgcont">
     <svg  x="0" y="0" width="100%" height="800px">

          <defs>
            <filter id="image-fill-nostretch" primitiveUnits="objectBoundingBox">
            <feImage x="0" y="0" width="1" height="1" id="theimage" xlink:href="https://image.ibb.co/ipbNkJ/56_E0406_E5_C8_EF897.jpg" preserveAspectRatio="xMidYMid slice"/>
              <feComposite operator="in" x1="SourceGraphic"/>
            </filter>
            
          <clipPath id="mask_1_" clipPathUnits="objectBoundingBox">
               <polygon id="mask" points="0,0.5 0,1 1,0.75 1,0.25 0,0"/>
          </clipPath>
          </defs>

          <g clip-path="url(#mask_1_)">
              <rect width="100%"height="100%" x="0%" y="0%" filter="url(#image-fill-nostretch)"/>
          </g>
    </svg>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to ensure an element dynamically resizes to fill the remaining space completely?

I have a grid layout with 3 sections. My goal is to make the middle "map" section resizable so that when it's shrunk, the "points" section expands to fill up the remaining space. #grid { display: grid; grid-template-rows: 1fr 6fr 2fr; gr ...

Using AJAX to post button values

I have encountered an issue while attempting to post a value to a PHP script using the POST method in AJAX. The error message is as follows: Notice: Undefined index: gamekey Here is the JavaScript code I am working with: function adminchat(row) { ...

Html5 canvas not resizing to fit container

I'm currently working on creating a square canvas using CSS to ensure it is instantly sized when the page loads. To achieve this, I am wrapping the canvas in a div and applying the following styles: http://jsfiddle.net/evopgwzd/ body { backgrou ...

Failure to display sub menu upon hover

While exploring the website and visiting the "onze klanten" page, I noticed that when hovering over the menu, the submenu disappears behind the slider. Do you have any ideas on how to fix this issue? ...

issue encountered when attempting to execute the function during the on-change event

While attempting to develop a todo app, I encountered an issue where calling the function markComplete on each checked or unchecked checkbox resulted in an error message stating cannot read property 'completed' of undefined. What could be the so ...

Integrate JavaScript code with HTML pages

After creating a basic HTML structure that I am proud of, I encountered some challenges with getting the Javascript to function properly. I am new to working with Javascript and received some initial assistance, but unfortunately, it is no longer working. ...

Uninterrupted text streaming with dynamic content that seamlessly transitions without any gaps

I'm currently facing a challenge with an outdated element like <marquee>. Here's a fiddle where you can check it out: https://jsfiddle.net/qbqz0kay/1/ This is just one of the many attempts I've made, and I'm struggling with two m ...

The input field within the button stubbornly resists aligning with the right corner

Can anyone help me with using the class=float-right on the button labeled "Add to cart"? It seems to be not aligning properly, especially when I increase the width of the card. The "Add to cart" button is sticking to the "Descripti ...

Issues arising on my website when accessed through Internet Explorer version 8

Let me try to outline the issue I'm facing: I recently developed a new website that works smoothly on Firefox, Safari, Internet Explorer, and Chrome. However, there seems to be an issue with Internet Explorer when it is set at a high level of securit ...

The UL list-style does not seem to be working properly on the Woocommerce checkout page

After creating my e-commerce website and implementing the woocommerce plugin to display products, I encountered an issue. The main menu pages are all showing up with the list-style, but the pages associated with the plugins are not displaying the list-styl ...

Tips for implementing event handlers on dynamically generated li elements in VueJS

Creating multiple ul elements using v-for in the following way <div v-for="item in info"> <ul> <li><a>{{item.number}}</a></li> <li><a>{{item.alphabet}}</a></li> </ul> </div&g ...

What could be causing the footer to not show up at the bottom of the page?

I've encountered an issue with my code where the footer appears to be stuck at the bottom of the h2_c div instead of the bottom of the .content div where it should be. I have tried using positioning but it hasn't resolved the problem. Thank You. ...

The URL in $.mobile.changePage has not been updated

One of the challenges I encountered was switching between two pages - a menu list page and a menu detail page. The transition from the menu to the menu detail page was achieved using $.mobile.changePage. While everything seemed to be working correctly, the ...

Utilizing ng-if within ng-repeat for dynamically generated option tags in HTML and AngularJS

I am using AngularJS to create a dropdown menu with select and option tags. The menu is referencing a model and looks like this: <select id="edit-location" class="" ng-model="packageLoc"> <option ng-repeat="x in loc" value="{{ x.locationId }} ...

Initial size of a div element with relative positioning

When a div tag has an absolute position, its height and width default to 0. In contrast, when it has a relative position, the default width is set to 300px. I am looking for a way to have a div tag with a width of 0 and a relative position... ...

Unable to assign a value to a data attribute using jQuery

For some unknown reason, the code $('element').data('data_attribute_name', 'value'); is not functioning properly in this scenario: Here is the HTML structure: <ul class="main_menu__list" data-scheme="light"> ... </u ...

Ways to dynamically modify the CSS attributes of a webpage during the page load event

I'm looking to incorporate the background:rgba(226, 93, 141, 0.76) attribute into the body:after psuedo-element once my page has finished loading. Below is my HTML and CSS code: <style runat="server" id="myStyle" > body:after{ position:fix ...

Iterate through Javascript quotes and automatically navigate to the first quote once the loop is completed

I've encountered an issue with the script where I am unable to loop back to the first element once all child elements have been traversed. https://jsfiddle.net/pad5bp0o/2/ Here is the jQuery code snippet: $(document).ready(function() { var length ...

Adding data to MySQL database utilizing jQuery and PHP

I'm facing an issue that I can't seem to figure out. The code is not throwing any PHP errors, so I suspect it might be a client-side problem, although I could be mistaken. The objective is to have a form submit the content of a text field to a M ...

How can you refresh the .replaceWith method in jQuery?

Is there a way to reset the .replaceWith function so that the html, css and javascript elements are restored to their original state? I am looking to have an icon replace the text when the user clicks on "contact", and then have the text return when the u ...