I understand you’re asking a performance-related question, but IMO you should ignore that. Yes, .underline would be less intensive than a ul span but the difference is negligible and developers should really focus on the specificity of selectors and how they will cascade in larger sites. At least, that’s how I think people should think about it .
The smaller client sites means CSS files are too small to really notice discernible differences in rendernig time, and larger sites need to focus more on the cascading aspect because larger sites are harder to maintain and not run into conflicting situations.
There is never “one solution fits all” in CSS, as circumstances vary, but I would say in general, the selector seems over specified. If there is an element with the .underline class, do we really need ul li in front of it in the selector?
Well, if the .underline class does something entirely different when not nested in an li in a ul, you may, but if the .underline class only ever applies an underline, then the selector is over specified.
As for span, does there even need to be a span? Is the whole li underlined, or just part of it?
If the whole li is underlined, apply the class to the li element.
If the whole ul is underlined, apply the class to that.
If only part of the li is underlined, you do need a span.
After coding about 500 sites I realised I could have saved myself loads of headaches if only I’d been sensible and added an extra simple class
.underline {}
I used to be a strong advocate of using descendant selectors to target elements in order to keep the mark up clean and while this is fine for a one off small site under your control it simply does not work efficiently on larger projects with many people working on the same code and where the code changes often.
If you use ul span then there’s a risk that someone else adds another span inside your span and you get styles being applied to the new element. If the span had a class to start with then it would be unlikely that anyone else would add the span with the class and not realise what is was being used for.
Of course there are times when you certainly do want every element in your parent styled and then you should use a descendant selector (e.g. a long list where every list is the same). However if I added a span inside the list I would not say ul.parent li span {}. It would be much easier to add a class to the span and keep specificity and maintenance low.
If adding extra classes to the html makes your life easier and the site easier to maintain then it is worth the slight overhead in markup. Always keep specificity as low as possible and try not to undo styles later on.
As with any of these questions though it does boil down to personal preference and in what environment you are working.
Hey I really appreciate you doing this & if you have the time I have a question before I upload the code you kindly sorted out… “Won’t the above code underline everything in the list?” Thing is I just need specific words in the list underlined as can be seen here: https://www.english-teacher-david.co.uk/blog/modal-verbs-eating-out.html
Then just add the class of .underline to the spans in question.
.underline{text-decoration:underline}
<span class="underline"> I am underlined but I am not a link and you will feel really stupid when you try to click me as you will do many times ...ha ha got you.</span>
Given the potential to confuse underlined text with links, using text-decoration: underline; on anything other than <a> is bad pretty much all the time.