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
https://forums.phpfreaks.com/topic/69301-center-text-in-a-div/#findComment-348334
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.