在CSS中使用:before伪类插入特殊字符

发布于 2024-10-02 06:32:30 字数 538 浏览 11 评论 0原文

我正在摆弄 css 中的 :before 伪类,试图插入一个特殊字符,但结果不是我所希望的。

使用:

.read_more:before {
    content: "»";
    margin-right: 6px;
}

我得到了我想要的字符,但在它之前有一个 â 字符并使用:

.read_more:before {
    content: "»";
    margin-right: 6px;
}

我在 html 页面上得到了完整的 »

我可以想出几种方法来解决我的问题,但我想知道如果我想使用 :before 伪类,正确的语法是什么。

顺便说一句,我的文档类型是:

<!doctype html>
<html lang="en">

I was toying around with the :before pseudo class in css, trying to insert a special character but the result is not what I was hoping for.

Using:

.read_more:before {
    content: "»";
    margin-right: 6px;
}

I get the character I want, but with an  character before it and using:

.read_more:before {
    content: "»";
    margin-right: 6px;
}

I get the complete » on the html page.

I can think of a couple of ways to solve my problem, but I was wondering what the correct syntax would be if I wanted to use the :before pseudo class.

By the way, my doctype is:

<!doctype html>
<html lang="en">

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

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

发布评论

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

评论(6

热风软妹 2024-10-09 06:32:30

试试这个

.read_more:before {
    content: "\00BB";
    margin-right: 6px;
}

\00BB 是该字符的 unicode 表示形式。它应该合理地工作=)

try this

.read_more:before {
    content: "\00BB";
    margin-right: 6px;
}

\00BB is the unicode representation of that character. It should reasonably works =)

避讳 2024-10-09 06:32:30

答案已经告诉了,但我想参考一下:

我在 html 页面上获得了完整的 »

这是因为 CSS content 属性不被视为 HTML。它不会附加到 DOM,因此不会解析任何特定于 HTML 的标记。您可以直接插入字符:content: ""; 或使用 Unicode 表示法:content: "\0504";

The answer has been already told, but I want to refer to:

I get the complete » on the html page.

That's because CSS content property isn't treated as HTML. It's not appended to the DOM, therefore any HTML-specific markup isn't parsed. You can insert a character directly: content: "Ԃ"; or use Unicode notation: content: "\0504";.

海之角 2024-10-09 06:32:30

尝试指定 。理想情况下,您希望在服务器中进行设置。

Try specifying <meta charset="utf-8">. Ideally you want to set this in the server.

自此以后,行同陌路 2024-10-09 06:32:30

我知道自从提出这个问题以来已经有一段时间了,但如果现在有人可能需要它,我找到了解决方案。

这里有一个包含大量字形的图表。找到您想要的并复制它的十六进制代码。

然后将其粘贴到此处进行转换。

您将得到一个如下所示的值:\00E1(CSS 值)

将此值粘贴到您的“内容:”上并感到高兴:)

I know it's been a while since this question was asked but in case someone might need it nowadays, I found the solution for it.

Here's a chart with lots of glyphs. Find the one you want and copy the hex code for it.

Then paste it here to convert.

You'll get a value that looks like this: \00E1 (CSS Value)

Paste this value on your 'content:' and be happy :)

倾城花音 2024-10-09 06:32:30

您的浏览器没有使用正确的文本编码——也就是说,它没有使用与您的编辑器相同的文本编码。如果您从 Web 服务器接收页面,最好的方法是确保服务器发送正确的 Content-Type 标头。如果您无法控制 Web 服务器的标头,或者您将使用本地 HTML 文件进行大量测试,请添加 针对您所使用的编码和 HTML 版本的文档添加适当的标签。我建议使用 UTF-8。 CSS 文件(如果它与 HTML 分开)应使用相同的编码。

Your browser isn't using the correct text encoding -- that is, it isn't using the same text encoding as your editor. If you are receiving the page from a Web server, the best approach is to make sure the server is sending the proper Content-Type header. If you don't have control over the Web server's headers, or if you will be doing a lot of testing using local HTML files, add appropriate tags to your document for the encoding and HTML version you are using. I recommend using UTF-8. The CSS file (if it is separate from the HTML) should use the same encoding.

入画浅相思 2024-10-09 06:32:30

将其添加到 html 的 部分中

<meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8" />

但是如果 html 页面是用 PHP 编码的,我会更喜欢以下内容:

<?php
    header("Content-Encoding: utf-8");
    header("Content-type: text/html; charset=utf-8");
?>

并且不要忘记保存任何文件(css、html、 php) 使用 UTF-8 编码

Add this on the html, inside the <head> section

<meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8" />

But if the html page is coded in PHP, I would prefer the following:

<?php
    header("Content-Encoding: utf-8");
    header("Content-type: text/html; charset=utf-8");
?>

And don't forget to save any file (css, html, php) with UTF-8 encoding

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