CSS pseudo-element to apply the first character of an element

倖福魔咒の 提交于 2019-11-28 02:02:43

From the spec:

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

So your bracket and the letter H are selected by :first-letter, because ( is considered a punctuation symbol, not a letter.

There are two workarounds:

  1. Wrap your opening bracket in span tags:

    <!-- To style both (), wrap both in <span> tags -->
    <h1><span>(</span>Hello World)</h1>
    

    and target h1 span:

    h1 span {
        font-size: 63px;
        color: #510007;
        font-family: Helvetica;
    }
    
  2. Drop the brackets from your text:

    <h1>Hello World</h1>
    

    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: 63px;
        color: #510007;
        font-family: Helvetica;
    }
    
    h1:before { content: '('; }
    h1:after { content: ')'; }
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!