1

I am trying to figure out how to make B red in the following snippet.

I am using react-markdown to render HTML from markdown so, unfortunately, I can’t use HTML attributes.

p:first-of-type::first-letter {
  color: red;
}
<div>
  <p>
    <span>Aa</span>
  </p>
  <p>
    Bb
  </p>
  <p>
    Cc
  </p>
</div>
sunknudsen
  • 6,356
  • 3
  • 39
  • 76

1 Answers1

3

The reason you cannot set F to be non-bold is because first-letter does not apply to inline elements.

You can demonstrate this easily by looking at the following snippet. The span is displayed as a block element and then correctly removes the bold from the first-letter:

p::first-letter {
  font-weight: bold;
}

p > span {
  display: block;
}

p > span::first-letter {
  font-weight: normal;
}
<div>
  <p>
    <span>
      Foo
    </span>
  </p>
  <p>
    Bar
  </p>
</div>

The reason is described further in the documentation here:

The ::first-letter CSS pseudo-element applies styles to the first letter of the first line of a block-level element, but only when not preceded by other content (such as images or inline tables).

Martin
  • 16,093
  • 1
  • 29
  • 48
  • Thanks for the answer. I needed `first-of-type` to target only the first occurrence. I updated my question. – sunknudsen Mar 06 '20 at 13:24