This solution may seem simple, but I wholeheartedly believe it's the exact answer you've been searching for...
It's impossible.
After defining a height
for an img
in CSS, any HTML declaration is immediately disregarded. There's no way within the CSS to then override that declaration and utilize the height specified in the HTML because terms like initial pertain to properties determined by CSS, not HTML attributes.
Refer to this guide on CSS Height, outlining all Property Values: *Experimental/non-supported values omitted
height: auto | length | % | inherit | initial
If you try to redefine the height
of img
using any of these property values, they won't be effective - I've tested each one individually to confirm this.
length
and %
necessitate a specified height, precisely what you're aiming to avoid
initial
will simply grab the original CSS declaration of height: auto;
inherit
lacks a parent to inherit from, thus resorting to the default value of auto
Your sole option is to override the height in CSS, utilizing inline styles (suggested by Carter)...
<img style="height: 150px;" />
Alternatively, employ selectors such as IDs or classes...
<img id="my_image" />
#my_image {
height: 150px;
}
Or, if you require JS to automatically generate overrides, consider implementing a jQuery solution like this:
$("img").each(function() { //Loop through all images
var $t = $(this); //$t represents the current iteration
var ht = $t.attr("height"); //Retrieve the HTML height
ht = ht ? ht+"px" : "auto"; //Add "px" if HTML height is defined | Use "auto" otherwise
$t.css("height", ht); //Apply the height to the current element
});
This script loops through all images on the page and assigns a CSS height corresponding to the HTML height. In the absence of an HTML height, it defaults to height: auto;
.