CSS:样式外部链接

标签 css url css-selectors uri href

我想为我网站 (Wordpress) 中的所有外部链接设置样式。我正在尝试:

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

但是 Wordpress 将整个 url 放在链接中……那么,我怎么能设置所有不以 http://www.mywebsite.com 开头的链接的样式呢? ?

谢谢。

最佳答案

2021 年解决方案

a[href]:not(:where(
  /* exclude hash only links */
  [href^="#"],
  /* exclude relative but not double slash only links */
  [href^="/"]:not([href^="//"]),
  /* domains to exclude */
  [href*="//stackoverflow.com"],
  /* subdomains to exclude */
  [href*="//meta.stackoverflow.com"],
)):after {
  content: '↗️';
}
<strong>Internal sites:</strong>
<br>Lorem <a href="http://stackoverflow.com">http://stackoverflow.com</a> ipsum
<br>Lorem <a href="/a/5379820">/a/5379820</a> ipsum
<br>Lorem <a href="//stackoverflow.com/a/5379820">//stackoverflow.com/a/5379820</a> ipsum
<br>Lorem <a href="http://stackoverflow.com/a/5379820">http://stackoverflow.com/a/5379820</a> ipsum
<br>Lorem <a href="https://stackoverflow.com/a/5379820">https://stackoverflow.com/a/5379820</a> ipsum
<br>Lorem <a href="https://meta.stackoverflow.com/">https://meta.stackoverflow.com/</a> ipsum
<br>Lorem <a href="ftp://stackoverflow.com">ftp://stackoverflow.com</a> ipsum

<br><br>
<strong>External sites:</strong>
<br>Lorem <a href="ftp://google.com">ftp://google.com</a> ipsum
<br>Lorem <a href="https://google.com">https://google.com</a> ipsum
<br>Lorem <a href="http://google.com">http://google.com</a> ipsum
<br>Lorem <a href="https://www.google.com/search?q=stackoverflow">https://www.google.com/search?q=stackoverflow</a>
<br>Lorem <a href="//www.google.com/search?q=stackoverflow">//www.google.com/search?q=stackoverflow</a>

<br><br>
<strong>Other anchor types</strong>
<br>Lorem <a>no-href</a> ipsum
<br>Lorem <a href="#hash">#hash</a> ipsum

2014 解决方案

使用一些特殊的 CSS 语法,您可以轻松地做到这一点。这是一种适用于 HTTP 和 HTTPS 协议(protocol)的方法:

a[href^="http://"]:not([href*="stackoverflow.com"]):after,
a[href^="https://"]:not([href*="stackoverflow.com"]):after {
  content: '↗️';
}

关于CSS:样式外部链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5379752/

相关文章:

javascript - 我正在使用 Brad Frost 的 3-up Carousel,我想添加一个圆形包装

css - 相对定位 + 绝对定位 VS Floats Left an Float Right(你在你的 CSS 中使用哪种方法)?

用于 Web URL 的 iOS 孟加拉语文本解析器

所选项目的 Android 选择器不起作用

html - 响应式设计的字体大小

python - CSS 不使用 Apache、Django 和 mod_wsgi 呈现

javascript - java 6 与 java 7 string.matches 与正则表达式

java - 从基本链接获取 URL 层次结构

html - CSS3 数字匹配选择器

jquery - 使用 jquery 将 css3 选择器添加到 Internet Explorer 的最小 javascript 库