For anybody running across this problem in 2023 (like me), I have some good news to share.
Currently, as mentioned by Temani back in 2018, the behavior of underlines is defined in CSS 2.1:
Underlines, overlines, and line-throughs are applied only to text (including white space, letter spacing, and word spacing): margins, borders, and padding are skipped. User agents must not render these text decorations on content that is not text. For example, images and inline blocks must not be underlined.
However, the CSS Text Decoration Module, level 3, says the following (emphasis my own):
Note that CSS 2.1 required skipping margins, borders, and padding always. In this level, by default only the margins, borders, and padding of the decorating box are skipped. In the future CSS2.1 may be updated to match this new default. Also, control over decorating leading/trailing spaces is expected in Level 4, and will be applied by default to the HTML <ins>
and <del>
elements.
What this means is that any descendants of a decorated box will have their entire margin box decorated over, unless specified otherwise using the text-decoration-skip-*
properties, defined in level 4.
Therefore, one new possible answer to this question is to simply wait until Text Decoration level 3 is becomes standard (or at least until it's implemented by browser vendors). It's currently a Candidate Recommendation Draft, which browsers may implement, but needs to become a Recommendation before it's officially considered a "standard."
As far as when that will be, I have no idea—but I feel like it's worth mentioning.