Distinguishing Between the Heights and Maximum Heights of WebKit SVG

When running the following two examples in Chrome or Safari (Firefox remains unaffected), one can notice the differences in the width of the SVG image.

Initially, the first example appears to exhibit the correct behavior in my opinion. However, in the second example, it seems like the SVG is being stretched. Could this be a bug? Is there a solution to this issue?

Example 1 - height:100%, functions as intended.

html, body {
  height: 100%;
  margin: 0;
}
img {
  background: silver;
  vertical-align: top;
  height: 100%;
}
<img src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/mozilla.svg">

Example 2 - max-height:100%, results in the main part of the image shifting towards the center, leaving a significant amount of space on the left and right.

html, body {
  height: 100%;
  margin: 0;
}
img {
  background: silver;
  vertical-align: top;
  max-height: 100%;
}
<img src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/mozilla.svg">

Answer №1

It appears to be a bug that needs addressing. The CSS 2.1 documentation states

for replaced elements with both width and height computed as auto, use the algorithm under Minimum and maximum widths above to find the used width and height. Then apply the rules under "Computing heights and margins" above, using the resulting width and height as if they were the computed values.

Within the Minimum and maximum widths algorithm, there is a case outlined:

  • Constraint violation: h > max-height
  • Resolved Width: max(max-height * w/h, min-width)
  • Resolved Height: max-height

Considering that min-width equals 0, the resolved width should be max-height * w/h.

It seems that Webkit is instead using w, indicating a bug in the implementation.

To work around this issue, you can utilize a common technique to force an aspect ratio.

html, body {
  height: 100%;
  margin: 0;
}
#wrapper {
  position: relative;
  display: inline-block; /* Shrink-to-fit width */
  vertical-align: top;
  height: 100%;
  max-width: 100%;
  overflow: hidden;
}
.ratio {
  height: 100%;
  vertical-align: top;
  visibility: hidden;
}
.filler {
  position: absolute;
  max-height: 100%;
  max-width: 100%;
  background: silver;
  max-height: 100%;
  left: 0;
  top: 0;
}
<div id="wrapper">
  <img class="ratio" src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/mozilla.svg">
  <img class="filler" src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/mozilla.svg">
</div>

However, please note that this effect will only be active upon initial page load. Resizing the window afterwards may cause it to malfunction. To resolve this, trigger a rerender using JavaScript:

window.addEventListener('resize', function() {
  var el = document.getElementById('wrapper'),
      parent = el.parentNode,
      next = el.nextSibling;
  parent.removeChild(el);
  parent.insertBefore(el, next);
});
html, body {
  height: 100%;
  margin: 0;
}
#wrapper {
  position: relative;
  display: inline-block; /* Shrink-to-fit width */
  vertical-align: top;
  height: 100%;
  max-width: 100%;
  overflow: hidden;
}
.ratio {
  height: 100%;
  vertical-align: top;
  visibility: hidden;
}
.filler {
  position: absolute;
  max-height: 100%;
  max-width: 100%;
  background: silver;
  max-height: 100%;
  left: 0;
  top: 0;
}
<div id="wrapper">
  <img class="ratio" src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/mozilla.svg">
  <img class="filler" src="https://dev.w3.org/SVG/tools/svgweb/samples/svg-files/mozilla.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

Align the <div> vertically within the <td> tag

My current set up looks something like this: <tr> <td> <div class="blue">...</div> <div class="yellow">...</div> </td> <td> <div class="blue">...</div> <div class="yellow ...

Troubles arise when hovering over the <strong> tag and the <h4> element

While hovering over my h4 tag in the table, everything functions correctly. However, when I hover over the strong tag inside the h4 element, the strong tag inherits the same hover effect as the h4 tag. The structure of each td element within the table is ...

Is there a way to potentially utilize window.open() to open a specific section of a webpage?

My HTML page consists of 2 div blocks and 2 links. I want to open the content of the first div in a new window when the first link is clicked, and the content of the second div in another new window when the second link is clicked. You can find the code h ...

Guide on making a color legend with JavaScript hover effects

On my website, there is a dynamic element that displays a table when values are present and hides it when there are no values. The values in the table are color-coded to represent different definitions. I wanted to add hover text with a color key for these ...

The Javafx WebEngine's executescript() function is unable to send a multiline string

Issue: I'm struggling to make the JavaFX WebEngine trigger a JavaScript function when using a Java String object with multiple lines. If I input the following example in HTML: "asd" ENTER "qwe" into the textArea, and then click the send button, the f ...

How to Adjust the Padding of Tree Row in GWT?

Have you ever seen a GWT tree before? It sort of resembles the following structure: <div class="gwt-Tree"> <div style="padding-top: 3px; padding-right: 3px; padding-bottom: 3px; margin-left: 0px; padding-left: ...

Creating a design with multiple `<thead>` and `<tbody>` elements

Seeking advice on usability and design elements. I am currently utilizing JQuery to hide/show entire sections within a single large table structure. The layout consists of multiple thead sections, with the main header at the top, followed by sub-headers fo ...

What steps can I take to streamline and simplify this tab controller code?

I'm looking to simplify this jQuery code because I feel like there's repetition. As someone new to JavaScript and jQuery, I've created two tabs with their respective containers containing miscellaneous information. My goal is to have each co ...

align image vertically within a floated container

There are 5 floated divs with heights stretched to 100% of the document height using JavaScript. Each of the 5 divs contains an img element. <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <lin ...

Ways to identify when the scroll bar reaches the end of the modal dialog box

I have been working on a modal that should display an alert when the scrollbar reaches the bottom. Despite my efforts to research a solution, I am struggling to detect this specific event within the modal. The desired outcome is for an alert to pop up once ...

Come up with various transition animations for multiple child elements that activate when the cursor hovers over the parent element

I have a CSS & HTML code snippet that looks like this: .item-video-kami { width: 480px; overflow: hidden; position: relative; } .item-video-kami>.play-icon.full-height>svg { width: 106px; color: #ffffff50; } .item-video-kami img { ...

When the Bootstrap navbar is collapsed, clicking on it does not result in it opening

I'm attempting to create a collapsible navbar for smaller screens that expands when a button is clicked. However, upon clicking the button, nothing happens and I'm unsure why. <nav class="navbar navbar-expand-lg navbar-light" style="backgro ...

A method to retrieve the content of an input field and assign it to an onclick event

I encountered an issue with an ajax function that requires the lat and lng variables. Here is a simple HTML code snippet: <fieldset> <legend>Geocoding Services</legend> Latitude:<br><input type="text" id="lat" value="42.3600077 ...

Change the background image when hovering over an element with vanilla JavaScript by utilizing data attributes

I would like to be able to change the background image of a <div> when hovering over a link. Initially, the <div> should not have a background image when the page loads for the first time. This is my current setup: <div class="background"& ...

Tip: Using jQuery to Wrap Characters Based on Count

Within my table, there is a specific cell in each row that contains exactly 13 characters. <td class="need-to-wrap">0123456789012</td> I am looking to wrap characters #10, 11 and 12 with a span element like so: <td class="need-to-wrap"> ...

Updating the email header for responding to New Order and shipped email notifications in Woocommerce

Looking for a solution to customize the reply-to email addresses specifically for New Order and Shipped emails sent to customers. I attempted to implement a suggested fix from this resource Change "reply to" email address in all Woocommerce email ...

Optimizing HTML and Script loading sequences for efficient execution

I have a query regarding the loading order of scripts in web browsers. I am interested in knowing the most efficient way to redirect users to a mobile website on the client side. For example, if I place a mobile detection script within the <head> se ...

Code snippet that will emphasize the active page if there are multiple pages, such as highlighting page 2

My current project involves implementing a script to highlight the current page on a Tumblr blog. I found the script I am using here: $(function(){ $('a').each(function() { if ($(this).prop('href') == window.location.href) { ...

Adjusting the alignment of button text in an Angular Kendo grid

I am currently utilizing the grid view feature of Kendo UI for Angular. While I have buttons on the grid, the issue is that the text within the buttons is not centered properly despite applying styles such as text-align:center. Here is the template for my ...

The menu icon in the Bootstrap 5 navigation bar is not functioning on a specific webpage

I am facing an issue with my menu icon not working properly on mobile devices or when I try to inspect the page and run it on a responsive device. How can I resolve this problem so that users can click on the menu icon to view the navigation items? &l ...