Essentially,
CSS can NOT effect a "parent" element (a
) of a targeted element (img
).
Therefore CSS can not be applied to an element based on if it does (or does not) contain any children and can not count any of these children if they do or do not exist.
Therefore, in the eyes of the anchor CSS definition, the fact it contains an image can't be articulated.
To work aound this we have a few options:
This CSS Tricks Article is a bit old but is a good concept introduction to CSS Specificity.
This link May also be of use.
For your specific case:
David's comment would fit the bill.
How I would fix the problem (general concept)
I would fix your problem using the :not
operator and selecting certain situations as required.
CSS:
a {
/* Rules for ALL anchors, primarily for anchors that contain images etc. */
background-color:none;
color: green;
}
/* Followed by rules ONLY for anchors that do NOT contain the .img class */
/* These will overwrite the above rule on the applicable elements */
a:not(.img){
background-color:#0078D7;
color:white;
}
HTML:
<a href="/images/infobox.png" class='img'><img src="/images/infobox.png" width="300px"></a>
<a href='https://www.google.com'>Goooooogle me!</a>
And here is a working demo:
a {
background-color:none;
color: green;
}
a:not(.img){
background-color:#0078D7;
color:white;
font-size: 1.2rem;
letter-spacing:0.15;
}
<div>
<a href="https://i.pinimg.com/474x/f6/98/d5/f698d58d7201bbdb19d66377f13a3704--german-soldier-german-army.jpg" class='img'><img src="https://i.pinimg.com/474x/f6/98/d5/f698d58d7201bbdb19d66377f13a3704--german-soldier-german-army.jpg" width="300px"></a><br><BR>
<a href='https://www.bing.com'>Goooooogle me!</a>
</div>