附件的 CSS 样式 - 自动在文本之前添加图标

发布于 2024-12-19 10:09:10 字数 81 浏览 3 评论 0原文

我想知道制作一个下载类,在文本前面放置一个图标,并且文本和图标都是附加文件链接的一部分,这有多难。有没有一种简单的方法可以用 CSS 来做到这一点?

I was wondering how hard it is to make a class for downloads that places an icon right before the text and both text and icon are part of the link to the attached file. Is there a simple way to do this with CSS?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女 2024-12-26 10:09:10

假设所有下载链接都是类名“downloadLink”的 a 元素:

.downloadLink {
    background-image: url(path/to/image.png);
    background-position: 0 50%;
    background-repeat: no-repeat;
}

如果这些链接的格式为:

<a href="http://example.com/downloads/document.pdf">document.pdf</a>
<a href="http://example.com/downloads/image.png">image.png</a>

那么 css:

.downloadLink[href$=pdf] {
    background-image: url(path/to/pdf_background_image.png);
    background-position: 0 50%;
    background-repeat: no-repeat;
}
.downloadLink[href$=png] {
    background-image: url(path/to/png_background_image.png);
    background-position: 0 50%;
    background-repeat: no-repeat;
}

将为带有 href< 的链接提供特定背景以 pdfpng 结尾的 /code> 属性。这种方法基于 CSS3 的属性选择器,这可能会降低跨浏览器兼容性,但是根据 Quirksmode 的兼容性表 不是(基本上上面的所有内容,包括 IE7 都应该在 IE 或其他浏览器中支持此功能(尽管它没有指定支持哪些选择器))浏览器)。

参考文献:

Assuming that all links to downloads are a elements, of class-name 'downloadLink':

.downloadLink {
    background-image: url(path/to/image.png);
    background-position: 0 50%;
    background-repeat: no-repeat;
}

If those links are of the format:

<a href="http://example.com/downloads/document.pdf">document.pdf</a>
<a href="http://example.com/downloads/image.png">image.png</a>

Then the css:

.downloadLink[href$=pdf] {
    background-image: url(path/to/pdf_background_image.png);
    background-position: 0 50%;
    background-repeat: no-repeat;
}
.downloadLink[href$=png] {
    background-image: url(path/to/png_background_image.png);
    background-position: 0 50%;
    background-repeat: no-repeat;
}

Will give specific backgrounds to links with a href attribute that ends with pdf or png. This approach is based around CSS3's attribute-selectors, which might reduce cross-browser compatibility but, according to Quirksmode's compatibility table not by much (basically everything above, and including, IE7 should support this (albeit it doesn't specify which of the selectors are supported) in IE or other browsers).

References:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文