Can overflow text be centered?

后端 未结 8 885
傲寒
傲寒 2020-12-23 14:14

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

相关标签:
8条回答
  • 2020-12-23 15:13

    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.

    0 讨论(0)
  • 2020-12-23 15:20

    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>
    
    0 讨论(0)
提交回复
热议问题