问题描述
我的 h1 标签中有以下内容:(Hello World)",所以我将以下内容添加到我的 css 以更改此元素的第一个字符:
I have the following content in my h1 tag: "(Hello World)" so I add the following to my css to change the first character of this element:
h1:first-letter { font-size:6**x; color:#510007; font-family:Helvetica; }
但是,正如我所注意到的,第一个字母仅用于字母,那么是否有任何解决方法可以将此样式应用于第一个字符?在这种情况下是(".
But, as I noticed, first-letter is only for letters, so is there any workarounds to apply this style to the first char? Which in this case is "(".
推荐答案
来自 规格:
标点符号(即 Unicode 中定义的字符 [UNICODE] 在open"(Ps)、close"(Pe)、initial"(Pi)、final"(Pf)和other"(Po)标点类)中,在第一个字母之前或之后应该是包括
Punctuation (i.e, characters defined in Unicode [UNICODE] in the "open" (Ps), "close" (Pe), "initial" (Pi). "final" (Pf) and "other" (Po) punctuation classes), that precedes or follows the first letter should be included
所以你的括号和字母H被:first-letter选中,因为(被认为是标点符号,而不是字母.
So your bracket and the letter H are selected by :first-letter, because ( is considered a punctuation symbol, not a letter.
有两种解决方法:
将左括号括在 span 标记中:
<!-- To style both (), wrap both in <span> tags --> <h1><span>(</span>Hello World)</h1>
和目标h1 span:
h1 span { font-size: 6**x; color: #510007; font-family: Helvetica; }
从文本中删除括号:
Drop the brackets from your text:
<h1>Hello World</h1>
并使用 :before 和/或 :after 代替(IE7 和更早版本不支持):
and use :before and/or :after instead (not supported in IE7 and older):
/* To style both (), use h1:before, h1:after */ h1:before { font-size: 6**x; color: #510007; font-family: Helvetica; } h1:before { content: '('; } h1:after { content: ')'; }