I know this question is old but since I still found it in 2024, here's an update:
The :has()
pseudo-selector class can be used to access its parent elements.
div.with-link:has(> a:target) {
background: yellow
}
Note that this method requires you to define a parent selector div.with-link
in this case. All elements matching this selector that have the target in them will be highlighted.
All major browsers support this selector. Refer to https://caniuse.com/css-has to check if your target browser or browser versions have support for it.
If this does not fit your requirements, you may still fall back to the JavaScript alternative.