[Special Summer Sale] 40% OFF All Magento 2 Themes

Cart

How to prevent line breaks in list items using CSS

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #10094
    rajasekar
    Participant

    I’m trying to put a link called Submit resume in a menu using a li tag. Because of the whitespace between the two words it wraps to two lines. How to prevent this wrapping with CSS?

    #10101
    n1313
    Participant

    Use white-space: nowrap;[1] [2] or give that link more space by setting li‘s width to greater values.


    [1] § 3. White Space and Wrapping: the white-space property – W3 CSS Text Module Level 3
    [2] white-space – CSS: Cascading Style Sheets | MDN

    #10099
    ptim
    Participant

    If you want to achieve this selectively (ie: only to that particular link), you can use a non-breaking space instead of a normal space:

    <li>submit&nbsp;resume</li>
    

    https://en.wikipedia.org/wiki/Non-breaking_space#Encodings

    edit: I understand that this is HTML, not CSS as requested by the OP, but some may find it helpful…

    #10100
    jimmyrare
    Participant

    You could add this little snippet of code to add a nice “…” to the ending of the line if the content is to large to fit on one line:

    li {
      overflow: hidden; 
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    
    #10098
    nadeesh-peiris
    Participant

    display: inline-block; will prevent break between the words in a list item:

    li {
      display: inline-block;
    }
    
    #10097
    yevgeniy-afanasyev
    Participant

    Bootstrap 4 has a class named text-nowrap. It is just what you need.

    #10096
    sergio-mcfly-pyk
    Participant

    In your CSS, white-space can do the job

    possible values:

    white-space: nowrap
    white-space: pre
    white-space: pre-wrap
    white-space: pre-line
    white-space: break-spaces
    white-space: normal
    
    #10095
    michaelloop
    Participant

    <nobr>your text</nobr> (not css but easier in some cases)

Viewing 8 posts - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.