How can I prevent having just one hanging word on a new line in an HTML element?

前端 未结 3 1645
借酒劲吻你
借酒劲吻你 2021-02-05 04:37

I\'m building a simple webpage with some marketing content. One thing I don\'t like is that if a line of text is too long, it will wrap onto the next line, which is fine

3条回答
  •  情深已故
    2021-02-05 04:50

    EDIT: The best answer is much cleaner -- you should probably use that instead. I'm leaving my answer up because it does work and it has some value for weird cases (e.g. if you're using a dash instead of a space, if you don't want to use  , etc).


    Here's a neat little solution. Create a CSS class like this:

    .nobr { white-space:nowrap; }
    

    Any element with the class "nobr" will not be allowed to wrap white-space (spaces, tabs, etc) onto new lines. So just surround the last two words of your text with a span.nobr.

    Such and such doesn't have to be difficult. Such and such product makes it easy

提交回复
热议问题