CSS:外部链接样式
我想为我的网站 (Wordpress) 中的所有外部链接设置样式。我正在尝试:
.post p a[href^="http://"]:after
但是Wordpress将整个url放在链接中...那么,我如何设计所有不以 http://www.mywebsite.com ?
谢谢。
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 url in the links... So, how could I style all links that doesn't start with http://www.mywebsite.com ?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
2021年解决方案
2014 解决方案
使用一些特殊的 CSS 语法,您可以轻松地做到这一点。以下是一种适用于 HTTP 和 HTTPS 协议的方法:
2021 Solution
2014 Solution
Using some special CSS syntax you can easily do this. Here is one way that should work for both the HTTP and HTTPS protocols:
这种方式显示外部链接 ALA 维基百科:
可以在此处找到示例: http://jsfiddle.net/ZkbKp/
This way shows external links ALA Wikipedia:
An example can be found here: http://jsfiddle.net/ZkbKp/
一个简单的规则,不涉及硬编码:
适用于所有方案。具有识别错误输入的内部 URL 的额外好处。对于外部重定向的隐蔽内部链接,只需在相对 URL 前加上
//
前缀即可。感谢 Mark Battistella 留下了此代码段 2012 年的 CSS-Tricks。
更新: 基于实际使用,我个人发现上述问题,因为它对所有绝对链接进行样式设置,这可能会导致某些内容出现意外的样式情况(例如,在 Brave 中,当您下载页面以供离线查看时)。我的建议是使用
a[rel*="external"]::after
代替并装饰您的外部链接。One simple rule, no hardcoding involved:
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.我结合了 @superological + @Shaz 为我的 Foundation WP 主题制作了一些东西...
您可以使用此 HTML 片段在您的网站上进行测试:
这是我用于自定义图像(如网站图标)的
I combined @superlogical + @Shaz to make something for my Foundation WP theme...
You can use this HTML snippet to test on your site:
This is what I used for a custom image (like a favicon)
假设您通过
Justin Bieber Fan Club
设置外部链接的目标,你可以玩:
CSS:
Suppose you are setting the target for external links by
<a href='http://' target='_blank'>Justin Bieber Fan Club</a>
, You can do play:
CSS:
伪元素在语法中使用“::”而不是“:”。
还只需要匹配以“http”开头的 URL,而不是“http://”或“https://”。
因此,我不确定接受的答案和其他评分的答案在技术上是否正确。
要包含一个简洁的图标,您可以将 SVG 放入 CSS 变量中,然后在 CSS 规则中使用它。
这看起来很繁琐,但它是一个简洁的外部链接图标,可以对其进行编辑以适合您的主题,而无需返回桌面发布应用程序。因为图标是样式表的一部分,所以您不必让浏览器为其进行额外的下载。也因为它是 SVG,所以很清晰。 “矩形”是盒子,“线”和“多边形”的组是箭头。所有数字都是整数,您可以根据需要进行调整。
然后在您的规则中,您可以像这样引用它:
匹配的链接是以 http 开头且不包含您的域名的链接。 “http://”和“https://”均以“http”开头,因此无需根据接受的答案重复规则。
如果您在内容中使用以“//”开头的无协议链接,则需要考虑这一点。
您可能还想严格限制打开外部链接的方式。例如,您可能希望始终使用目标空白方法在新选项卡中打开外部链接。要正确执行此操作,您需要在链接中包含
target="_blank"
以及rel="noopener noreferrer"
。因此,要找出标记中不存在的位置,您可能只想定位具有正确“rel”属性的链接。Pseudo elements have a '::' instead of a ':' in the syntax.
There is also only a need to match a URL starting with 'http' rather than 'http://' or 'https://'.
Therefore I am not sure the accepted answer and other rated answers are technically correct.
To include a neat icon you can place an SVG in a CSS variable and then use it in your CSS rule.
That looks fiddly but it is a neat external link icon that can be edited to suit your theme without having to go back to a desktop publishing application. Because the icon is part of the stylesheet you don't have to have the browser do an extra download for it. Also because it is SVG is is crisp. The 'rect' is the box, the group with the 'line' and 'polygon' is the arrow. All the numbers are integers and you can adjust them if you want to.
Then in your rule you can reference it like so:
The matched links are the ones that start with http and don't include your domain name. Both 'http://' and 'https://' start with 'http' so there is no need to duplicate the rule as per the accepted answer.
If you are using protocol less links in your content that start with '//' then this will need to be taken into account.
You may also want to be strict about how you open external links. For instance you might want to always have external links open in a new tab with the target blank approach. To do this properly you need to include
target="_blank"
as well asrel="noopener noreferrer"
in your links. Therefore, to spot where this is not present in your markup you might want to target only the links that have the correct 'rel' attributes.这是使用网站图标图像设置外部链接样式的方法:
我们设置要插入的图像的路径,指定不重复,这样我们只插入一个副本,然后将位置指定为 100% 的位置文本内容的右侧,距顶部 0 像素。
This is how you can style external links with a favicon image:
We set the path to the image we want to insert, specify no-repeat so we only get one copy inserted, and then specify the position as 100% of the way over to the right of the text content, and 0 pixels from the top.
我喜欢 @henryscat 分享的方法,但由于不是 SVG 专家,我很难更改图标。为了让我更容易,我删除了蒙版并仅使用线条创建了图标。图标定义前面的注释描述了哪个
是图标的哪一部分。I like the approach @henryscat shared, but not being an SVG expert I struggled to change the icon. To make it easier for me, I removed the mask and created the icon using lines only. The comment in front of the icon definition describes which
<line/>
is which part of the icon.