CSS: Style external links

前端 未结 8 1801
甜味超标
甜味超标 2021-01-30 16:55

I want to style all external links in my website (Wordpress). I\'m trying with:

.post p a[href^=\"http://\"]:after

But Wordpress put the entire

8条回答
  •  遥遥无期
    2021-01-30 18:00

    One simple rule, no hardcoding involved:

    a[href*="//"] { /* make me do stuff */ }
    

    Works for all schemes. Has the added benefit of identifing mistyped internal URLs. For cloaked internal links redirecting externally, simply prefix the relative URL with //.

    Credit goes to Mark Battistella who left this snippet on CSS-Tricks in 2012.

    Update: Based on actual use I've personally found the above problematic as it styles all absolute links which can lead to unexpected styling in some situations (e.g. In Brave, when you download a page for offline viewing). My suggestion is to use a[rel*="external"]::after instead and decorate your external links.

提交回复
热议问题