question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

preact ignores/rewrites <img> dimensions?

See original GitHub issue

I have a React-managed page with some 175+ images, and after switching from react to preact-compat (using the webpack alias solution) all my images come out as <img src="..." width="0" height="0"> in the DOM, despite having defined dimensions in the JSX source code (e.g. <img src="..." width="100px" height="50px"/> etc) - is there any reason why it does this? Because that seems like a pretty crazy bug.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:28 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
leeoniyacommented, Apr 2, 2017

dimension attrs must not have units [1]. so i’m not sure what all the fuss is about.

[1] https://www.w3.org/TR/html5/embedded-content-0.html#dimension-attributes

2reactions
robertknightcommented, Apr 2, 2017

@Pomax - It sounds like you’re still not sure what’s going on. I’ll try to see if I can explain:

Internally, what happens when Preact renders the <img src='invalid' width="400px" height="400px"/> in your JSX is roughly this:

  1. The JSX code above is turned by Babel into h('img', { src: 'invalid', width: '400px', height: '400px' })
  2. The result of the h(...) function call is a data structure that looks like this:
{
  nodeName: 'img',
  attributes: {
    src: 'invalid',
    width: '400px',
    height: '400px',
  },
  children: [],
}
  1. Internally, Preact then takes the data structure from step (2) (vdomElement below) and creates an actual DOM element:
var node = document.createElement(vdomElement.nodeName);
// ...
for (var key in vdomElement.attributes) {
  // Does `node` have a property matching the prop name?
  if (key in node) {
    // Yes, set the property.
    node[key] = vdomElement.attributes[key];
  } else {
    // No, use `setAttribute`
    node.setAttribute(key, vdomElement.attributes[key]);
  }
}
// ...
parentElement.appendChild(node);
  1. In the case when vdomElement.nodeName is img, node is an HTMLImageElement, which does have properties called width and height so what ends up getting executed is:
var node = document.createElement('img');
node.src = 'invalid';
node.width = '400px';
node.height = '400px';

Try the code from step (4) in your browser console and then print out the values of node.width and node.height.

Now on the other hand, try this:

// This is what happens when the browser parses the HTML string '<img width="400px" height="400px">'
var node = document.createElement('img');
node.setAttribute('width', '400px');
node.setAttribute('height', '400px');
console.log(node.width, node.height);

You’ll see different results.

Does this make things clearer?

Read more comments on GitHub >

github_iconTop Results From Across the Web

preact ignores/rewrites <img> dimensions? #619 - GitHub
I have a React-managed page with some 175+ images, and after switching from react ... What happens there is Preact doing img.width =...
Read more >
Setting initial img dimensions in state - REACT - Stack Overflow
The dimensions-state is set correctly when the loading of the img is completed. However there is no inline styling applied. Can someone help...
Read more >
preact-lazyimage - npm
Lazy image loading made easy and simple for preact. ... Start using preact-lazyimage in your project by running `npm i preact-lazyimage`.
Read more >
HTML img width Attribute - W3Schools
The width attribute specifies the width of an image, in pixels. Tip: Always specify both the height and width attributes for images. If...
Read more >
<img>: The Image Embed element - HTML - MDN Web Docs
The HTML element embeds an image into the document. ... Use both width and height to set the intrinsic size of the image,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found