HTML+CSS: How to force div contents to stay in one line?

后端 未结 10 1859
小蘑菇
小蘑菇 2020-11-30 17:52

I have a long text inside a div with defined width:

HTML:

Stack Overflow is the BEST !!!

CSS:



        
相关标签:
10条回答
  • 2020-11-30 18:23

    Give this a try. It uses pre rather than nowrap as I would assume you would want this to run similarly to <pre> but either will work just fine:

    div {
        border: 1px solid black;
        max-width: 70px;
        white-space:pre;
    }
    

    http://jsfiddle.net/NXchy/11/

    0 讨论(0)
  • 2020-11-30 18:25

    Everybody jumped on this one!!! I too made a fiddle:

    http://jsfiddle.net/audetwebdesign/kh4aR/

    RobAgar gets a point for pointing out white-space:nowrap first.

    Couple of things here, you need overflow: hidden if you don't want to see the extra characters poking out into your layout.

    Also, as mentioned, you could use white-space: pre (see EnderMB) keeping in mind that pre will not collapse white space whereas white-space: nowrap will.

    0 讨论(0)
  • 2020-11-30 18:26

    Try this:

    div {
        border: 1px solid black;
        width: 70px;
        overflow: hidden;
        white-space: nowrap;
    }
    
    0 讨论(0)
  • 2020-11-30 18:26

    Use white-space:nowrap and overflow:hidden

    http://jsfiddle.net/NXchy/8/

    0 讨论(0)
提交回复
热议问题