0 votes
1 view
in Web Technology by (22.4k 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.3k 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
Welcome to Intellipaat Community. Get your technical queries answered by top developers !