简体   繁体   中英

Css Pseudo-class :first-letter

Can it be used inside an a tag?

a.x:first-letter
{
   color:red;
}

My name is <a class=x>Lionel</a>

I can't seem to make it work.

根据css2.1 :first-letter仅适用于块容器元素

Yes, that should work fine.

Can you provide a JSFiddle example of it not working?

It certainly should work -- See here for a test page which demonstrates it in action , and here for a browser compatibility chart (it says it should work in any browser).

[EDIT]

As @SilentGhost says, it only works for block level elements, which <p> is and <a> isn't.

You can make inline elements like <a> act as block elements by using the display:block; style. However this can mess up your page layout.

Fortunately, there is a half-way house option: display:inline-block; which should make your element get treated as a block element without disrupting your page layout. Try adding that to your stylesheet as follows:

a.x {
  display:block;
}

Your :first-letter style should now work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM