Question

My CSS :first-letter selector works with all p elements, but with a span that has an id #fletter it won't work...


#fletter span:first-letter {
    font-weight: 600;
font-size: 25px;
}

<span id="fletter">

The selector works fine with my p elements, though.


p:first-letter {
font-weight: 600;
font-size: 25px;
}

<p>Lorem Ipsum</p>
Was it helpful?

Solution

That is because :first-letter can only be used on block level elements.

One solution could be to display the span as a block level element:

#fletter span {
   display:block;
}

#fletter span:first-letter {
   font-weight: 600;
   font-size: 25px;
}

jsFiddle here.

OTHER TIPS

:first-letter does not work on inline elements such as a span. others inline elements are:

b, big, i, small, tt abbr, acronym, cite, code, dfn, em, kbd, strong, samp, var a, bdo, br, img, map, object, q, script, span, sub, sup button, input, label, select, textarea

:first-letter works on block elements such as a paragraph, table caption, table cell, list item, or those with the inline-block property applied.

if you want a :first-letter selector in a span then write it like this:

p span:first-letter {font-size: 500px !important;}
span {display:block}

<p>
 <span>text here</span>
</p>

According to the W3C, the first-letter property applies to block elements.

So, not spans. Sorry. You may have to create an inner span containing the first letter, maybe with Javascript or something. Or, if it's possible, give the span display:inline-block.

As written, you're targeting a span inside of something with the id fletter. You either need to do

#fletter:first-letter {
    font-weight: 600;
    font-size: 25px;
    display: block;
}

or, if you know this will always be a span:

span#fletter:first-letter {
    font-weight: 600;
    font-size: 25px;
    display: block;
}

I might be missing something but you are lacking the requires 2 colons. This works perfectly fine for me in Chrome etc:

#<idname>::first-letter{
    color: green;
    font-size: 100px;
}

:first-letter only works on block elements, and span is, by default, inline. You can either set the span to be a block element (display:block) or use a div instead (divs are block by default).

#fletter:first-letter {
font-size: 100px;
color: #8A2BE2;
}

Try this one. In my case it is working fine

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top