how to make space within span show up

后端 未结 2 1819
感动是毒
感动是毒 2021-02-18 19:19

I\'m trying to set background color for a whitespace within a span. But it won\'t show up when the whitespace span happens to be at the point when line wraps.

HT

相关标签:
2条回答
  • 2021-02-18 19:48

    Alter your css for the span to show white spaces, just like in the <pre> tag. Take a look at the different white-space options

    span {
        background-color: black;
        white-space:pre;
    }
    

    From the mentioned resource here is a nice table what the different options for white-space will do:

                   New lines    Spaces and tabs   Text wrapping
    normal         Collapse     Collapse          Wrap
    pre            Preserve     Preserve          No wrap
    nowrap         Collapse     Collapse          No wrap
    pre-wrap       Preserve     Preserve          Wrap
    pre-line       Preserve     Collapse          Wrap
    

    If you add a &nbsp; to your span, the string will not break anymore on your space but instead 'glue' the two parts together, without wrapping the string on the space.

    0 讨论(0)
  • 2021-02-18 20:10

    Another option if you do not want to rely on CSS for any reason is using the non-breaking space: &nbsp;, &#160; or &nbsp;

    (see here for more information about it)

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