CSS :first-letter Not Working
Answer :
::first-letter does not work on inline elements such as a span. ::first-letter works on block elements such as a paragraph, table caption, table cell, list item, or those with their display property set to inline-block.
Therefore it's better to apply ::first-letter to a p instead of a span.
p::first-letter {font-size: 500px;}
or if you want a ::first-letter selector in a span then write it like this:
p b span::first-letter {font-size: 500px !important;}
span {display:block}
MDN provides the rationale for this non-obvious behaviour:
The
::first-letterCSS pseudo-element selects the first letter of the first line of a block, if it is not preceded by any other content (such as images or inline tables) on its line....
A first line has only meaning in a block-container box, therefore the
::first-letterpseudo-element has only an effect on elements with adisplayvalue ofblock,inline-block,table-cell,list-itemortable-caption. In all other cases,::first-letterhas no effect.
Another odd case(apart from not working on inline items) is if you use :before the :first-letter will apply to the before not the actual first letter see codepen
Examples
- http://jsfiddle.net/sandeep/KvGr2/9/
- http://krijnhoetmer.nl/stuff/css/first-letter-inline-block/
References
https://developer.mozilla.org/en-US/docs/Web/CSS/::first-letter
http://reference.sitepoint.com/css/pseudoelement-firstletter
You can get the intended behavior by setting the span's display property to inline-block:
.heading span {
display: inline-block;
}
.heading span:first-letter {
color: red;
}<div class="heading">
<span>An</span>
<span>Interesting</span>
<span>Heading</span>
</div>This is because :first-letter only operates on block / inline-block elements. SPAN is an inline element.
Taken from http://reference.sitepoint.com/css/pseudoelement-firstletter:
The :first-letter pseudo-element is mainly used for creating common
typographical effects like drop caps. This pseudo-element represents
the first character of the first formatted line of text in a
block-level element, an inline block, a table caption, a table cell,
or a list item.
Comments
Post a Comment