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?
This should be the right answer. Cleanest and simplest:
Try the following example. I have added examples for each category: horizontal and vertical
add the line
display: table-cell;
to your css for that div. only table cells support the vertical-align:middle; but you can give that [table-cell] definition to the div..live example here: http://jsfiddle.net/tH2cc/
This worked for me
You can try the following methods:
1. If you have a single word or one line sentence, then the following code can do the trick.
Have a text inside a div tag and give it an id. Define the following properties for that id.
Note: Make sure the line-height property is same as the height of the division.
Image
But, if the content is more than one single word or a line then this doesn’t work. Also, there will be times when you cannot specify the size of a divison in px or %.(when the divison is really small and you want the content to be exactly in the middle)
2. To solve this issue, we can try the following combination of properties.
Image
These 3 lines of code sets the content exactly in the middle of a division (Irrespective of the size of the display). The "align-items: center" helps in vertical centering while "justify-content: center" will make it horizontally centered.
Note: Flex does not work in all browsers. Make sure you add appropriate vendor prefixes for additional browser support.