user2014429
user2014429

Reputation: 2577

center text in div like an img element

I can center fixed images easily on the page, but when it comes to text it seems to be more complex. this jsFiddle demonstrates how the image is centered but the text is not. How can I center the text in the same way? Thanks for reading.

.center{
         display:table;
         position:fixed;
         left:0;
         right:0;
         margin:auto;
         border:1px solid grey;
}

<div class = "center">text</div>

Upvotes: 0

Views: 61

Answers (2)

roemel
roemel

Reputation: 3297

Just add text-align:center; and width:100%; to your css. Is this what you are looking for?

Updated Fiddle

EDIT: You can replace width:100%; with width:yourWidthYouWant; and it will work as well.

Upvotes: 4

Akshay
Akshay

Reputation: 14348

Give a width and use text-align:center http://jsfiddle.net/Rc9fa/358/

.center{
width:100px;
text-align:center;
display:table;
position:fixed;
left:0;
right:0;
margin:auto;
border:1px solid grey;

}

Upvotes: 3

Related Questions