div align text middle center - ContentEssaywritercsClub

div align text middle center

Stack Overflow



  1. Log In
    Sign Up

  2. current community


    • Stack Overflow

      help
      chat

    • Meta Stack Overflow

    your communities

    Sign up or log in to customize your list.

    more stack exchange communities

    company blog

    • Tour

      Start here for a quick overview of the site

    • Help Center

      Detailed answers to any questions you might have

    • Meta

      Discuss the workings and policies of this site

    • About Us

      Learn more about Stack Overflow the company

    • Business

      Learn more about hiring developers or posting ads with us

This site uses cookies to deliver our services and to show you relevant ads and job listings.
By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service .
Your use of Stack Overflow’s Products and Services, including the Stack Overflow Network, is subject to these policies and terms.

Join us in building a kind, collaborative learning community via our updated
Code of Conduct .

Join Stack Overflow to learn, share knowledge, and build your career.

Email Sign Up
Sign Up

or sign in with


Google

Facebook

CSS center text (horizontally and vertically) inside a div block

Ask Question


up vote
583
down vote

favorite

187

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?

html css
share | improve this question

edited Jul 4 ’17 at 5:31

CyanCoding

446122

asked Apr 18 ’11 at 13:22

Satch3000

14.7k71169299

  • 9

    you mean text-align:center doesn’t do the "vertical part"?
    –  太極者無極而生
    Apr 18 ’11 at 13:24

  • try with "padding:20px";
    –  diEcho
    Apr 18 ’11 at 13:25

  • 2

    I finally found a solution that works with relative sizes and no fixed heights: stackoverflow.com/a/16195362/1301331
    –  cirrus
    Apr 24 ’13 at 15:04

  • 2

    It is very important to know how to center / middle elements. The below document has given a very clear picture. w3.org/Style/Examples/007/center.en.html
    –  shibualexis
    Oct 5 ’14 at 17:21


  • Here are two simple methods to center objects within divs, vertically, horizontally or both (pure CSS): stackoverflow.com/a/31977476/3597276
    –  Michael_B
    Aug 20 ’15 at 14:43


 | 
show 2 more comments

24 Answers
24

active

oldest

votes


up vote
956
down vote

accepted

If it is one line of text and/or image, then it is easy to do. Just use

text-align: center;
vertical-align: middle;
line-height: 90px; 

that’s it. If it can be multiple lines, then it is somewhat more complicated. But there are solutions on http://pmob.co.uk/ Look for “vertical align”.

Since they tend to be hacks or adding complicated divs… I usually use a table with a single cell to do it… to make it as simple as possible.


Update for 2016 / 2017:

It can be more commonly done with transform, and it works well even in older browsers such as IE10 and IE11. It can support multiple lines of text:

position: relative;
top: 50%;
transform: translateY(-50%); 

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

To shrink wrap the width:

The solution above used a fixed width for the content area. To use a shrink wrap width, use

position: relative;
float: left;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

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

I tried flexbox, but it wasn’t as widely supported, as it would break in some older version of IE such as IE10.

share | improve this answer

edited Feb 2 ’17 at 13:27

answered Apr 18 ’11 at 13:29

太極者無極而生

67.2k95361619

  • 44

    There’s now a display: table-cell property for that, FYI. Using it makes the element behave like a table cell and allows vertical-align: middle;
    –  Shauna
    Apr 18 ’11 at 14:38

  • 1

    If you’re using font: with these, make sure you put it ahead of line-height`.
    –  user1135469
    Aug 2 ’13 at 10:03

  • 4

    This might not be working as intended when the div contains <br/> elements.
    –  Martin Meeser
    Jun 5 ’14 at 15:47

  • 5

    What if my height in %?
    –  CandleCoder
    Aug 3 ’16 at 1:24

  • 1

    with this approach text text has to be in one line, otherwise you will have second line 90px below first.
    –  Tomasz Mularczyk
    Dec 10 ’16 at 12:35

 | 
show 15 more comments


up vote
380
down vote

Common techniques as of 2014:


  • Approach 1 – transform translateX/translateY:

    Example Here / Full Screen Example

    In supported browsers (most of them), you can use top: 50%/left: 50% in combination with translateX(-50%) translateY(-50%) to dynamically vertically/horizontally center the element.

    .container position: absolute; top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%);

  • Approach 2 – Flexbox method:

    Example Here / Full Screen Example

    In supported browsers , set the display of the targeted element to flex and use align-items: center for vertical centering and justify-content: center for horizontal centering. Just don’t forget to add vendor prefixes for additional browser support ( see example ).

    html, body, .container height: 100%;
    .container display: flex; align-items: center; justify-content: center;

  • Approach 3 – table-cell/vertical-align: middle:

    Example Here / Full Screen Example

    In some cases, you will need to ensure that the html/body element’s height is set to 100%.

    For vertical alignment, set the parent element’s width/height to 100% and add display: table. Then for the child element, change the display to table-cell and add vertical-align: middle.

    For horizontal centering, you could either add text-align: center to center the text and any other inline children elements. Alternatively, you could use margin: 0 auto assuming the element is block level.

    html, body height: 100%;
    .parent width: 100%; height: 100%; display: table; text-align: center;
    .parent > .child display: table-cell; vertical-align: middle;

  • Approach 4 – Absolutely positioned 50% from the top with displacement:

    Example Here / Full Screen Example

    This approach assumes that the text has a known height – in this instance, 18px. Just absolutely position the element 50% from the top, relative to the parent element. Use a negative margin-top value that is half of the element’s known height, in this case – -9px.

    html, body, .container height: 100%;
    .container position: relative; text-align: center;
    .container > p position: absolute; top: 50%; left: 0; right: 0; margin-top: -9px;

  • Approach 5 – The line-height method (Least flexible – not suggested):

    Example Here

    In some cases, the parent element will have a fixed height. For vertical centering, all you have to do is set a line-height value on the child element equal to the fixed height of the parent element.

    Though this solution will work in some cases, it’s worth noting that it won’t work when there are multiple lines of text – like this .

    .parent height: 200px; width: 400px; text-align: center;
    .parent > .child line-height: 200px;

Methods 4 and 5 aren’t the most reliable. Go with one of the first 3.

share | improve this answer

edited Sep 12 ’14 at 1:28

answered Sep 12 ’14 at 1:23

Josh Crozier

145k34254213

  • 8

    +1 for a comprehensive answer. Personally I prefer the first method because method 2 has lesser support and I like to avoid table-cell as much as possible.
    –  Harry
    Sep 13 ’14 at 16:02

  • 3

    2. Flexbox method is the only one which works if .containers parent is absolute. 1. method makes parent to shrink because of absolute .container and 3. method simply didnt work while .parent had absolute position.
    –  Jānis Gruzis
    Apr 13 ’15 at 8:03

  • Flex worked for vertical centering multiline text on ipad chrome browser.
    –  bob
    Feb 19 ’16 at 20:10


  • 5

    Flexbox method is the best of the lot.
    –  rorykoehler
    May 12 ’16 at 4:07

  • Approach1 fails for me when the the is scrollable.
    –  Vishal Gulati
    Mar 2 ’17 at 11:36

 | 
show 2 more comments


up vote
49
down vote

Using flexbox/CSS:

<div > <p>&#x0D05;</p>
</div>

The CSS:

.box display: flex; justify-content: center; align-items: center;

Taken from Quick Tip: The Simplest Way To Center Elements Vertically And Horizontally

share | improve this answer

answered Nov 10 ’15 at 3:25

Vinod

1,44453247

  • 2

    Careful: Won’t work in IE. You’ll need to add display: -ms-flexbox; -ms-flex-align: center; -ms-flex-pack: center.
    –  Dodekeract
    Dec 31 ’16 at 4:16


  • 3

    flexbox is just magical
    –  Qwertie
    Jul 4 ’17 at 23:56

add a comment  | 


up vote
24
down vote

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/

div height:90px; width:90px; text-align:center; border:1px solid silver; display: table-cell; // this says treat this element like a table cell vertical-align:middle; //now we can center vertically like in a TD

share | improve this answer

answered Apr 18 ’11 at 13:28

FatherStorm

6,61111324

  • 1

    does this solution work for IE 6 or IE 7?
    –  太極者無極而生
    Apr 18 ’11 at 13:37

  • @動靜能量: No, it only works in IE 8 and later.
    –  Guffa
    Apr 18 ’11 at 13:41

  • 14

    doesnt work in chrome
    –  Flash Thunder
    Jul 31 ’13 at 19:25

  • 2

    As of Jan ’16 this does work in Chrome.
    –  Rob
    Jan 12 ’16 at 13:57

  • 1

    This doesn’t work if position is absolute or fixed. See: jsfiddle.net/tH2cc/1636
    –  Aaron Mason
    Jan 15 at 1:11

add a comment  | 


up vote
12
down vote

I always use the following CSS for a container, to center it’s content horizontally and vertically.

display: -webkit-box;
display: -moz-box;
display: -ms-flexbox;
display: -webkit-flex;
display: flex;
-webkit-box-align: center;
-moz-box-align: center;
-ms-flex-align: center;
-webkit-align-items: center;
align-items: center;
-webkit-box-pack: center;
-moz-box-pack: center;
-ms-flex-pack: center;
-webkit-justify-content: center;
justify-content: center;

See it in action here: https://jsfiddle.net/yp1gusn7/

share | improve this answer

answered Aug 23 ’15 at 8:48

Kent Munthe Caspersen

1,8741519

  • 1

    ^ why standards are important
    –  Dodekeract
    Dec 31 ’16 at 4:17

add a comment  | 


up vote
11
down vote

you can try very easy code for this

 display:flex; align-items: center; justify-content:center;
.box height:90px; width:90px; background:green; display:flex; align-items: center; justify-content:center;
<div > Lorem
</div>

codpen link http://codepen.io/santoshkhalse/pen/xRmZwr

share | improve this answer

answered Dec 16 ’16 at 10:07

Santosh Khalse

4,21422428

  • Works even for text on multiple lines, +1!
    –  Jeroen Bellemans
    Dec 19 ’16 at 9:06

add a comment  | 


up vote
8
down vote

Give this css class to the targeted

.centered width: 150px; height: 150px; display: flex; align-items: center; justify-content: center; text-align: center; background: red; 
<div >This text is centered horizontally and vertically</div>

share | improve this answer

answered Sep 11 ’17 at 12:41

Aminu Kano

131125

  • 1

    I really should learn flexbox :s
    –  Josh Mc
    Jun 17 at 2:47

  • You really have to, and css grid as well 🙂 I recommend this curriculum .
    –  Aminu Kano
    Jun 17 at 13:06


add a comment  | 


up vote
7
down vote

Approach 1

div height: 90px; line-height: 90px; text-align: center; border: 2px dashed #f69c55;
<div> Hello World!
</div>

Approach 2

div height: 200px; line-height: 200px; text-align: center; border: 2px dashed #f69c55;
span display: inline-block; vertical-align: middle; line-height: normal;
<div> <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Haec et tu ita posuisti, et verba vestra sunt. Non enim iam stirpis bonum quaeret, sed animalis. </span>
</div>

Approach 3

div display: table; height: 100px; width: 100%; text-align: center; border: 2px dashed #f69c55;
span display: table-cell; vertical-align: middle;
<div> <span>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</span>
</div>

share | improve this answer

answered Jul 5 ’17 at 12:54

shubham agrawal

1,4152724

add a comment  | 


up vote
4
down vote

#parent display:table;
#child display:table-cell; width:100%; //as large as its parent to center the text horizontally text-align: center; vertical-align:middle;//vertically align this element on its parent

share | improve this answer

edited Feb 22 ’15 at 3:47

answered Feb 22 ’15 at 3:34

Narcisse Doudieu Siewe

464312

  • 4

    How exactly is this different than the other answers posted?
    –  Josh Crozier
    Feb 23 ’15 at 0:58

add a comment  | 


up vote
3
down vote

<div > <span>Text centered</span>
</div>
<style>
.small-container width:250px; height:250px; border:1px green solid; text-align:center; position: absolute; top: 50%; left: 50%; -moz-transform: translateX(-50%) translateY(-50%); -webkit-transform: translateX(-50%) translateY(-50%); transform: translateX(-50%) translateY(-50%);
.small-container span position: absolute; top: 50%; left: 50%; -moz-transform: translateX(-50%) translateY(-50%); -webkit-transform: translateX(-50%) translateY(-50%); transform: translateX(-50%) translateY(-50%);
</style>

share | improve this answer

edited Dec 9 ’15 at 3:47

josliber

36.5k115795

answered Dec 9 ’15 at 2:00

Arunkumar Maha

296

add a comment  | 


up vote
1
down vote

This works for me (tested ok!)

html:

<div > <p>Item to be Centered!</p>
</div>

css:

.mydiv height: 100%; position: relative;
.mydiv p margin: 0; position: absolute; top: 50%; left: 50%; margin-right: -50%; transform: translate(-50%, -50%); 

you can choose other values then 50% fi. 25% to center at 25% of parent

share | improve this answer

answered Dec 5 ’16 at 20:45

XPloRR

9713

add a comment  | 


up vote
1
down vote

div height: 90px; line-height: 90px; text-align: center; border: 2px dashed #f69c55;
<div> Hello World!
</div>

share | improve this answer

answered Feb 20 at 3:33

Debasis Paul

113

  • 2

    While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
    –  Sudheesh Singanamalla
    Feb 20 at 3:39

add a comment  | 


up vote
1
down vote

One of The Best approach is use flex property at parent div and add margin:auto property to
element tag

.parent display: flex; flex-direction: column; flex: 1; height:100%;
p margin:auto;

share | improve this answer

edited Aug 6 at 6:31

step

6051521

answered Jun 26 at 10:33

user3021146

122

add a comment  | 


up vote
0
down vote

Adjusting line height to get the vertical alignment.

line-height: 90px;

share | improve this answer

edited Nov 19 ’13 at 10:11

CloudyMarble

29.4k2181116

answered Nov 19 ’13 at 9:53

Abhay

213

  • 2

    Not going to downvote because it can be a solution, however this only works if you can guarantee single-line text…
    –  dooleyo
    Apr 28 ’14 at 18:24


add a comment  | 


up vote
0
down vote

This should be the right answer. Cleanest and simplest:

.element position: relative; top: 50%; transform: translateY(-50%);

share | improve this answer

answered Sep 15 ’16 at 20:01

Mike Barwick

4,27333959

add a comment  | 


up vote
0
down vote

<!DOCTYPE html>
<html> <head> <style> .container height:450px; background:#f8f8f8; display: -ms-flexbox; display: -webkit-flex; display: flex; -ms-flex-align: center; align-items: center; -webkit-box-align: center; justify-content: center; width: 100%; p font-size:24px; </style> </head> <body> <div > <p>hello world</p> </div> </body>
</html>

share | improve this answer

answered Mar 17 ’17 at 5:21

Nikit Barochiya

383310

add a comment  | 


up vote
0
down vote

<!DOCTYPE html>
<html> <head> <style> .maindiv height:450px; background:#f8f8f8; display: -webkit-flex; align-items: center; justify-content: center; p font-size:24px; </style> </head> <body> <div > <h1>Title</h1> </div> </body>
</html>

share | improve this answer

answered Mar 17 ’17 at 5:25

Nikit Barochiya

383310

add a comment  | 


up vote
0
down vote

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.

id-name height: 90px; line-height: 90px; text-align: center; border: 2px dashed red;

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.

id-name
display: flex;
justify-content: center;
align-items: center;
border: 2px dashed red;

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.

share | improve this answer

answered Mar 22 ’17 at 6:15

Clinton Roy

11113

add a comment  | 


up vote
0
down vote

Apply style:

position: absolute;
top: 50%;
left: 0;
right: 0;

Your text would be centered irrespective of its length.

share | improve this answer

edited May 6 ’17 at 19:24

Martin Tournoij

16.9k125888

answered Apr 26 ’17 at 13:08

techloris_109

15117

add a comment  | 


up vote
0
down vote

.cell-row display: table; width: 100%; height: 100px; background-color: lightgrey; text-align: center
.cell display: table-cell
.cell-middle vertical-align: middle
<div > <div >Center</div>
</div>

share | improve this answer

edited Oct 12 ’17 at 9:32

answered Sep 27 ’17 at 16:17

antelove

54726

add a comment  | 


up vote
0
down vote

This worked for me

.center-stuff text-align: center; vertical-align: middle; line-height: 230px; 

share | improve this answer

answered Feb 2 at 11:15

Ger Mc

190317

add a comment  | 


up vote
-1
down vote

Try the following example. I have added examples for each category: horizontal and vertical

<!DOCTYPE html>
<html> <head> <style> #horizontal text-align: center; #vertical position: absolute; top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%); </style> </head> <body> <div id ="horizontal">Center horizontal text</div> <div id ="vertical">Center vertical text</div> </body>
</html> 

share | improve this answer

answered Jul 4 at 20:12

Amir Md Amiruzzaman

9651518

  • the question is about centring horizontally AND vertically also this solution is already provided before
    –  Temani Afif
    Jul 7 at 14:16


add a comment  | 


up vote
-2
down vote

<!DOCTYPE html>
<html> <head> </head> <body> <div style ="text-align: center;">Center horizontal text</div> <div style ="position: absolute; top: 50%; left: 50%; transform: translateX(-50%) translateY(-50%);">Center vertical text</div> </body>
</html> 

share | improve this answer

answered Jul 7 at 14:00
user8875528

  • This is good clean code. Although, you could add ID to your div tags, so that you can use them later on if necessary.
    –  Amir Md Amiruzzaman
    Jul 7 at 14:02

  • Thank you for your suggestion. Yes, I agree ID is helpful to manipulate.
    – user8875528
    Jul 7 at 14:03

  • 1

    did you read previous answers? can you tell what this answer add to the previous ones?
    –  Temani Afif
    Jul 7 at 14:08

  • @AmirMdAmiruzzaman can you tell us what is the cleanest part of this code 🙂 … I see inline styles which is not good … also adding ID is not a good idea too, we need Classes
    –  Temani Afif
    Jul 7 at 14:09

  • Duplicate of Approach 1 of Josh’s answer
    –  Munim Munna
    Jul 7 at 15:31

add a comment  | 


up vote
-12
down vote

footer width: 100%; height: 80px; position: absolute; bottom: 0; left: 0;

share | improve this answer

edited Sep 12 ’14 at 2:55

Zach Saucier

16k74390

answered Apr 4 ’13 at 2:46

Kyle Cabral

212

add a comment  | 

protected by Temani Afif Jul 7 at 14:12

Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count ).

Would you like to answer one of these unanswered questions instead?

Not the answer you’re looking for? Browse other questions tagged html css or ask your own question .

asked

7 years, 4 months ago

viewed

1,291,165 times

active

11 days ago

Linked

2

How to center text within a DIV vertically and horizontally

0

How do I get text to go in the middle of a div?

0

How do I align text vertically centre using CSS?

0

How to vertically and horizontally center text inside columns?

1

Centering text (horizontally and vertically) in nav

0

Can’t center h1 inside main tags

-1

Position text vertically central inside absolutely positioned div without using pixel units

0

How can I center text without it staying at the top it’s containing element?

1

Center aligning a text horizontally and vertically in a div element

0

margin: 0px auto; not centering div

see more linked questions…

Related

3647

How to horizontally center a <div> in another <div>?

1691

Vertically align text next to an image?

2002

How do I give text or an image a transparent background using CSS?

468

Expand a div to take the remaining width

877

How to center absolutely positioned element in div?

330

How to align 3 divs (left/center/right) inside another div?

442

Vertically centering a div inside another div

1825

How do I vertically center text with CSS?

290

Center align image within div horizontally

250

How to center align vertically the container in bootstrap

Hot Network Questions

  • Does Flame Blade count as a weapon for Green-Flame Blade?

  • Is Moon pinging by laser which was shown in the ‘Big Bang Theory’ possible?

  • Why do some games persistently have mostly one viable strategy, while others can have many?

  • How can I utilize overly-violent PCs for character development / progressing the story?

  • tikz: Jigsaw Puzzle Boundary Shape

  • Why do ex-government employees have security clearances?

  • Why are airlines so concerned with checked baggage weight?

  • What is the crime of lèse-Bourbaki?

  • Can I forbid a patented invention from use by the government?

  • Is preparing for a lawsuit a valid reason to defer an exam?

  • Listing a Stack Overflow user as co-author for having provided substantial programming support

  • Completion of measure spaces – uniqueness

  • Who is the little girl in Raising Steam who blocks the railway meant to be a reference to?

  • When to use "Meter" vs "Metre"?

  • Is a pig-mounted cavalry possible?

  • ‘Caramelization’ of tomato sauce in slow cooker

  • Why do airliners have to park so accurately?

  • Custom Number Base Converter

  • Why is storing passwords in version control a bad idea?

  • Can a Rutterkin’s Bite poison a Paladin?

  • Find files by pattern and copy to target location

  • \text vs \scalebox

  • IT will only give password over phone – but is that really more secure than email?

  • How to handle colleagues who are unwilling to write a bug report?

more hot questions


question feed

default

Stack Overflow works best with JavaScript enabled