Can overflow text be centered?

后端 未结 8 883
傲寒
傲寒 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 14:54

    Div magic to the rescue. In case anyone is interested, you can see the solution here.

    HTML:

    some text
    some text that will overflow

    CSS:

    #outer {
        display: block;
        position: relative;
        left: 100px;
        width: 100px;
        border: 1px solid black;
        background-color: silver;
    }
    #inner {
        /* shrink-to-fit width */
        display: inline-block;
        position: relative;
        /* shift left edge of text to center */
        left: 50%;
    }
    #text {
        /* shift left edge of text half distance to left */
        margin-left: -50%;
        /* text should all be on one line */
        white-space: nowrap;
    }
    

提交回复
热议问题