CSS: limit element to 1 line

后端 未结 4 1015
清歌不尽
清歌不尽 2021-02-01 01:23

I want to limit a element to display only 1 line of text, with the remainder hidden (overflow:hidden), without setting a specific height. Is this possible with just CSS?

相关标签:
4条回答
  • 2021-02-01 01:31
    p#foo {
        white-space:nowrap;
        overflow:hidden;
    }
    
    0 讨论(0)
  • 2021-02-01 01:31

    If you are looking to disable text wrapping, use white-space: nowrap;.

    0 讨论(0)
  • 2021-02-01 01:35

    It is possible:

    element (white-space: nowrap; overflow: scroll;)
    

    Text parsed as HTML or XML is tokenized, which means all runs of whitespace are collapsed to a single space character. This means you will have a single line of text unless you specify something in addition to the overflow declaration that could cause your text to wrap.

    EDIT: I failed to include the necessary write-space: nowrap property, otherwise the default is that a container will scroll vertically opposed to horizontally.

    0 讨论(0)
  • 2021-02-01 01:56

    use display block so it won't go beyond your parent element.

    p#foo{
        white-space: nowrap;
        overflow: hidden;
        display: block;
        text-overflow: ellipsis;
    }
    
    0 讨论(0)
提交回复
热议问题