Hide text if it doesn't fit in one line with CSS

后端 未结 3 1694
清歌不尽
清歌不尽 2021-02-13 18:16

I have a div element which contains some text. This element has fixed width. If the text is so long that it cannot fit in one line, I want to hide that text (the wh

3条回答
  •  青春惊慌失措
    2021-02-13 18:27

    If you want to hide the extra text, you can do something like:

    div {
        white-space: nowrap;
        overflow: hidden;
        max-width: 50em;
        border: 1px dotted black;
    }



    this is a super long line of text that it is never ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever ever going to fit on a single line!

    If you want to hide the entire element completely, you will need to use JavaScript. CSS has no way to "detect" if the text is too long.

提交回复
热议问题