I need to horizontally (I don’t care vertically) center three boxes (div or span, I don’t care) such that the mid one is always centered and its width is the smallest needed for the text (a single word) it contains, in the following way:
with a short text (a sigle word):
--------------------------------------------------------------
| |
| ---------------------- ---------- ---------------------- |
| | | |xxxxxxxx| | | |
| | | | | | | |
| ---------------------- ---------- ---------------------- |
| |
--------------------------------------------------------------
with a long text (a single word, again):
--------------------------------------------------------------
| |
| ----------- ------------------------------ ------------- |
| | | |xxxxxxxxxxxxxxxxxxxxxxxxxxxx| | | |
| | | | | | | |
| ----------- ------------------------------ ------------- |
| |
--------------------------------------------------------------
Is this possible?
My last temptative is far from success (since the boxes overlap, and I don’t want this):
<div style="position:relative;";>
<span style="display:block;width:100px;margin:0 auto;text-align:center;border:1px solid red"><h1>mid_text</h1>
<span style="text-align:right;position:absolute;top:0em;left:0;display:block;width:50%;float:left;border:1px solid red">left text</span>
<span style="text-align:left;position:absolute;top:0em;left:50%;display:block;float:right;width:50%;border:1px solid red">right text</span>
</span>
</div>
CSS
Add this to your CSS file:
HTML
And this is what your HTML code should look like:
Example: http://jsfiddle.net/YVhLt/