7

What's the difference between p ::first-letter and p::first-letter?

p::first-letter can successfully select the first letter inside a paragraph, but p ::first-letter cannot.

Harry
  • 87,580
  • 25
  • 202
  • 214
Yishu Fang
  • 9,448
  • 21
  • 65
  • 102

2 Answers2

8

The selector p::first-letter selects the first letter inside the p whereas the p ::first-letter selects the first letter within the child elements of the p.

p ::first-letter is equivalent to p *::first-letter. The below is what the specs say:

If a universal selector represented by * (i.e. without a namespace prefix) is not the only component of a sequence of simple selectors selectors or is immediately followed by a pseudo-element, then the * may be omitted and the universal selector's presence implied.

Note: Even though the selector (p ::first-letter) itself points to the first letter inside all child elements, the ::first-letter selector works only on block or inline-block elements and hence wouldn't work on a span unless its display is modified.

p ::first-letter {
  color: red;
}
p::first-letter {
  color: blue;
}

span{
  display: inline-block;
}
<p>Some text <span>inside a span</span> and <span>inside this span too</span>
</p>
Harry
  • 87,580
  • 25
  • 202
  • 214
0

p ::first-letter means change the style of the first letter of any element which is a descendant of p. Whereas p::first-letter means change the first letter of the p element.

kojow7
  • 10,308
  • 17
  • 80
  • 135
  • Not true. The space indicates that the selector is a [descendant combinator](https://developer.mozilla.org/en-US/docs/Web/CSS/Descendant_selectors) (i.e. select the first letter of all children, grandchildren, etc. elements within the `p` tag)... – War10ck Mar 03 '16 at 15:17
  • I stand corrected. I have modified my answer. – kojow7 Mar 03 '16 at 15:28