Say, an HTML file contains the following elements:
<div class="mydiv">
<img src="img.jpg"/>
</div>
I would like to know if it is safe to programmatically transform it into
<img class="mydiv" src="img.jpg" />
That is, adding the parent’s class to the child element and ripping the parent itself.
Do the two versions of HTML code yield the same rendering when a CSS rule is present for the class? If not, which are the cases when it does not happen in fact?
Notes:
-the parent just features the class, it has no other meaningful attributes
-the CSS files cannot be altered
-the transformation is performed by javascript after the HTML is loaded
-the goal is getting rid of the parent but so that all CSS rules render the same
Addressing the title of the question first: classes are never inherited. CSS rules can be.
There are a couple of general cases where things could go wrong.
There might be rules which only apply under certain conditions (e.g.
margin: autohas different effects depending on the value ofdisplay(which has different defaults for different elements).Selectors might be more specific that
.mydiv. There might bediv.mydivor.mydiv img