Prevent line-break of span element

前端 未结 4 1535
一个人的身影
一个人的身影 2020-12-07 10:51

I have a element which I want to display without any line break. How can I do that?

相关标签:
4条回答
  • 2020-12-07 11:21

    Put this in your CSS:

    white-space:nowrap;
    

    Get more information here: http://www.w3.org/wiki/CSS/Properties/white-space

    white-space

    The white-space property declares how white space inside the element is handled.

    Values

    normal This value directs user agents to collapse sequences of white space, and break lines as necessary to fill line boxes.

    pre This value prevents user agents from collapsing sequences of white space. Lines are only broken at newlines in the source, or at occurrences of "\A" in generated content.

    nowrap This value collapses white space as for 'normal', but suppresses line breaks within text.

    pre-wrap This value prevents user agents from collapsing sequences of white space. Lines are broken at newlines in the source, at occurrences of "\A" in generated content, and as necessary to fill line boxes.

    pre-line This value directs user agents to collapse sequences of white space. Lines are broken at newlines in the source, at occurrences of "\A" in generated content, and as necessary to fill line boxes.

    inherit Takes the same specified value as the property for the element's parent.

    0 讨论(0)
  • 2020-12-07 11:22

    white-space: nowrap is the correct solution but it will prevent any break in a line. If you only want to prevent line breaks between two elements it gets a bit more complicated:

    <p>
        <span class="text">Some text</span>
        <span class="icon"></span>
    </p>
    

    To prevent breaks between the spans but to allow breaks between "Some" and "text" can be done by:

    p {
        white-space: nowrap;
    }
    
    .text {
        white-space: normal;
    }
    

    That's good enough for Firefox. In Chrome you additionally need to replace the whitespace between the spans with an &nbsp;. (Removing the whitespace doesn't work.)

    0 讨论(0)
  • 2020-12-07 11:40

    If you only need to prevent line-breaks on space characters, you can use &nbsp; entities between words:

    No&nbsp;line&nbsp;break
    

    instead of

    <span style="white-space:nowrap">No line break</span>
    
    0 讨论(0)
  • 2020-12-07 11:42

    With Bootstrap 4 Class:

    text-nowrap
    

    Ref: https://getbootstrap.com/docs/4.0/utilities/text/#text-wrapping-and-overflow

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