When I specify text-align:center
for an element with a width that is greater than the width of the text, the text is centered within the content box of the elem
Strange requirement. I would expand the boxes to the texts size.
One possible solution might involve a negative text-indent: text-indent: -50px;
, but that won't work for smaller texts (first DIV
in your example). No better idea here right now.
this one seems to work: add a wrapper block with position relative and left:50% margin-left:-100% you can see it here Here's the code:
<style>
div {
display: block;
position: relative;
left: 100px;
height:1.5em;
width: 100px;
text-align: center;
white-space: nowrap;
border: 1px solid black;
background-color: silver;
}
span{
display:block;
position:relative;
left:50%;
margin-left:-100%;
}
</style>
<div><span>some text</span></div>
<div><span>some text that will overflow</span></div>