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.

Height attribute on img element not working

See original GitHub issue

I was using hyperapp 1.0.1 and this was working

<img src="https://i.pinimg.com/originals/87/b8/67/87b8671c2d08dc83554806539022bde7.gif" height="100px"/>

I update to hyperapp 1.1.2 and now it doesn’t work, but it does without the “px” like this

<img src="https://i.pinimg.com/originals/87/b8/67/87b8671c2d08dc83554806539022bde7.gif" height="100"/>

Here is the Codepen

So I went and check what changes were made, and apparently, this breaks after the version 1.0.2. The problem is here on line 138 where before it was always using setAttribute

if (value == null || value === false) {
    element.removeAttribute(name)	
} else {	
    element.setAttribute(name, value)	
}

and now is using

if (typeof value === "function" || (name in element && !isSVG)) {
    element[name] = value == null ? "" : value
} else if (value != null && value !== false) {
    element.setAttribute(name, value)
}

If the element has the attribute and because the image height attribute is an int and not a string it is set to 0.

It’s not a big issue you could just write the number without the “px”. However, there are people who use the “px” like I was doing and then the whole images on the site disappear. Probably there are similar cases like this with other elements.

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
frenzzycommented, Mar 3, 2018
const img = document.createElement('img')
img.height = 100                    // <img height="100" />
img.height = "100"                  // <img height="100" />
img.height = "100%"                 // <img height="0" />
img.height = "100px"                // <img height="0" />
img.setAttribute("height", 100)     // <img height="100" />
img.setAttribute("height", "100")   // <img height="100" />
img.setAttribute("height", "100%")  // <img height="100%" />
img.setAttribute("height", "100px") // <img height="100px" />

HTMLImageElement.height Is a unsigned long that reflects the height HTML attribute, indicating the rendered height of the image in CSS pixels. https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement

HTML <img> element height The intrinsic height of the image in pixels. In HTML 4, the height could be defined pixels or as a percentage. In HTML5, however, the value must be in pixels. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img

I would prefer to keep current behavior when you must specify image height and with without px, not a bug IMO.

0reactions
frenzzycommented, Aug 31, 2018

Agreed

Read more comments on GitHub >

github_iconTop Results From Across the Web

Image height and width not working? - Stack Overflow
The width and height attributes are being overridden by your stylesheet, ... Remove that and your width/height properties on the img tag should...
Read more >
Img height attribute not working - The freeCodeCamp Forum
hi, sorry for my English. Normaly when I write in CSS .image { width: 6%; height: 30%; }. it works, but now not…how...
Read more >
Img Height In HTML: How Not To Use The Height Attribute »
On a page with a lot of images, this can be a real problem. The simple solution is to include information about an...
Read more >
HTML img height Attribute - W3Schools
The height attribute specifies the height of an image, in pixels. Tip: Always specify both the height and width attributes for images. If...
Read more >
Setting Height And Width On Images Is Important Again
Fixing The Resizing Problem # · height is set on the element in HTML · width is set on the element in HTML...
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