Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
::first-letter also styles first character

I have this Text:

p::first-letter {
  color: red;
}
<p>"Welcome!"</p>

but then i realized that also the " got styled.

I tried using ::before but that didn't work either:

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

p::before {
  content: '"';
}
<p>Welcome!"</p>

How could I manage to only style the first letter (In clean code)?

over 4 years ago · Santiago Trujillo
4 Respostas
Responde à pergunta

0

Here is what I came up with after trying and thinking a bit of CSS/HTML principles:

I will be using display: inline-block; because ::first-letter requires a block for its content to work.

Please pay attention that I am using <span></span> tag and not <p></p> tag as the HTML W3 Consortium consider it as invalid html code. You can check your code about validity here

Take a look at the sandbox or at the live example here:

.quote span {
  display: inline-block;
}

.quote span::first-letter {
  color: red;
}
<q class="quote" cite="https://www.imdb.com/title/tt0062622/quotes/qt0396921"><span>This is a great quote</span></q>

More info on tags here:

  • see what is <q></q> tag is and how to use it;
  • see more about what is <span></span> (e.g. text wrapper to any text you want to display and style depending on your needs);
  • look through this to see what is the ::first-letter, how it works, and what is needed for it to work properly.
over 4 years ago · Santiago Trujillo Relatório

0

p::first-letter, it will always style the first character, to style W put a span element like W then style the span.

over 4 years ago · Santiago Trujillo Relatório

0

You can text-indent the paragraph and use a before positioned in absolute, but you have to manually set the indent that depends on text-size. It is not a super-clean solution: it is a "not so dirty" one.

p {
  text-indent: 5px;
}
p::first-letter {
  position:relative;
  color: red;
}

p::before {
  position:absolute;
  content: '"';
  left: 0;
}
<p>Welcome"</p>

over 4 years ago · Santiago Trujillo Relatório

0

simply float the first character and use :after not :before

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

p::after {
  content: '"';
  float:left;
}
<p>Welcome!"</p>

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda