Centralizar um texto verticalmente com CSS não é fácil quanto centralizar horizontalmente. Existem várias maneiras de se fazer isso e cada é mais adequada que a outra dependendo do caso. Vamos passar por cada uma dessas maneiras.
Para alinhar verticalmente uma única linha de texto, você precisa usar a propriedade line-height.
<html lang="en"> <head> <meta charset="utf-8"> <title>Hello World</title> </head> <body> <div id="vertical"> Hello, Livecoding! </div> </body> </html>
O código CSS é o seguinte:
#vertical { height: 90px; line-height: 90px; text-align: center; border: 5px solid green; }
Para tratar várias linhas de texto, você precisa usar a tag <span>. Então fazer isso? Vamos dar uma olhada no seguinte código:
<html lang="en"> <head> <meta charset="utf-8"> <title>Hello World</title> </head> <body> <div id="vertical"> <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</span> </div> </body> </html>
CSS:
#vertical { height: 150px; line-height: 150px; text-align: center; border: 5px solid green; } span { display: inline-block; vertical-align: middle; line-height: normal; }
Então, como tudo isso funciona?
O segundo método pode ser um pouco complexo. No entanto, ele funciona muito bem em todos os navegadores. Você tem alguma coisa para adicionar ao tutorial? Se sim, não se esqueça de comentar abaixo.
Você também pode assistir em nosso site a vídeos sobre HTML e CSS.
Abaixo estão alguns exemplos:
Another week to bring you the top yield platforms for three of the most prominent…
If you hold a large volume of LEDU tokens above 1 million units and wish…
It’s another week and like always we have to explore the top yield platforms for…
At a time where we’re constantly seeking tools and strategies to simplify our crypto investments,…
As we kick off another week, it's time to explore the top yield platforms for…
We're excited to share that Education Ecosystem was recently featured in an article on Business…