I'm having some trouble with my CSS selector. The goal is to style anchor <a>
tags with some custom styles, but to leave it alone if it contains an icon. In the example below the icon would be the <span>
tag.
I've tried lots if things with :not()
selectors, but to no luck. Here's what I've got so far showing the problem:
a {
color: red;
}
a span {
color: unset;
}
<a href="#">styled</a>
<br>
<a href="#"><span>not styled</span></a>
The problem here is that first <a>
tags are styled, and then after only the <a><span>
tags are unstyled. And because in the selector the span
is last, that will be where the style is applied on, not the parent <a>
element.
We also can't use a selector like a *
in the first part, because in the HTML there is an anchor without a child. The *
would only match <a>
tags with any child, not empty ones.
Maybe there is some way or trick to only match when the element has a <span>
tag, but then apply the style to the <a>
tag. Another valid solution to me would be matching just an empty <a>
tag without children, because this would not include the <a><span>
tags. I thought the :empty
selector might help with that but it seems this just matches if it has no content at all, but in this example the <a>
tag does have content, just no child element.
Note: Changing the HTML is not really an option because this comes from user input using a WYSIWYG editor. I would have to make a function that rewrites some parts of the HTML, which is not desirable.
I'm not sure if this is possible in CSS, but it seems like such a simple example. If anyone could help me out here that would be awesome. Thanks