Jump to content

center text in a div


brown2005

Recommended Posts

Harder than it sounds.

 

Centering a block element within a block element is relatively easy:

 

.outerbox {position:relative; width:500px; height:500px; border:1px solid #000;}
.innerbox {position:absolute; top:50%; left:50%; display:block; width:400px; height:400px; border:1px solid #000; margin:-200px 0 0 -200px;}

<div class="outerbox">
   <div class="innerbox"></div>
</div>

 

Doing the same with an inline element is much harder, especially if you want it to remain centered when the user starts fiddling around with text-size, and I can't think of any satisfactory way to do it. If I wanted to put a word in the center of a div, until someone shows me a better way, I'd probably use a small image (because I could then use the centering technique above and avoid resizing problems).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.