csscss-selectorsparagraph

Target first letter of each word in css


I need to change size, color, and weight of every first letter of each word. I am not talking about Capitalize each first letter. I mean that target first letter and apply style according to my choice.

enter image description here


Solution

  • You should wrap every single word with a tag and use ::first-letter CSS selector .

    Also, note that this selector does not work on inline elements. If you want to use it with an inline element, such a <span>, make sure you set display:inline-block (see here for more details: https://stackoverflow.com/a/7631782/11298742)

    example :

    p span { display: inline-block; font-family: 'Roboto', sans-serif }
    p span::first-letter {
        color: red;
        font-weight: bold;
    }
    <p><span>Lorem</span> <span>ipsum</span> <span>dolor</span> <span>sit</span> <span>amet</span></p>