How to change a span to look like a pre with CSS?

后端 未结 8 1689
你的背包
你的背包 2020-12-01 04:05

Is it possible to to change a tag (or

) to preformat its contents like a
 tag would using only CSS?         


        
相关标签:
8条回答
  • 2020-12-01 04:27

    Specifically, the property you're looking at is:

    white-space: pre
    

    http://www.quirksmode.org/css/whitespace.html
    http://www.w3.org/TR/CSS21/text.html#white-space-prop

    0 讨论(0)
  • 2020-12-01 04:33

    Look at the W3C CSS2.1 Default Style Sheet or the CSS2.2 Working Draft. Copy all the settings for PRE and put them into your own class.

    pre {
        display: block;
        unicode-bidi: embed;
        font-family: monospace;
        white-space: pre;
    }
    
    0 讨论(0)
  • 2020-12-01 04:35

    See the white-space CSS property.

    .like-pre { white-space: pre; }
    
    0 讨论(0)
  • 2020-12-01 04:38

    Why not just use the <pre> tag, instead of the span tag? Both are inline, so both should behave in the way you would like. If you have a problem overriding the entire definition of <pre>, just give it a class/id.

    0 讨论(0)
  • 2020-12-01 04:42

    This makes a SPAN look like a PRE:

    span {
      white-space: pre;
      font-family: monospace;
      display: block;
    }
    

    Remember to change the css selector as appropriate.

    0 讨论(0)
  • 2020-12-01 04:45

    while the accepted answer is indeed correct, if you want the text to wrap you should use:

    white-space: pre-wrap;
    
    0 讨论(0)
提交回复
热议问题