Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (20.3k points)

I have a div set to display: block (90px height and width), and I have some text inside.

I need the text to be aligned in the center both vertically and horizontally.

I have tried text-align: center, but it doesn't do the horizontal part, so I tried vertical-align: middle, but it didn't work.

Any ideas?

1 Answer

0 votes
by (40.7k points)

Try this :

text-align: center;

vertical-align: middle;

line-height: 90px;       /* The same as your div height */

The below code will support multiple lines of text:

position: relative;

top: 50%;

transform: translateY(-50%);

Example: https://jsfiddle.net/wb8u02kL/1/

To shrink-wrap the width use the code given below:

float: left;

top: 50%;

left: 50%;

transform: translate(-50%, -50%);

Example: https://jsfiddle.net/wb8u02kL/2/

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...