How to insert line breaks in HTML documents using CSS

前端 未结 12 1650
情深已故
情深已故 2020-12-29 01:53

I\'m writing a web service, and I want to return the data as XHTML. Because it\'s data, not markup, I want to keep it very clean - no extra

s or
相关标签:
12条回答
  • 2020-12-29 02:27
    <style type="text/css">
    label, input { float: left; }
    label { clear:left; }
    </style>
    
    <form>
        <label>thing 1:</label><input />
        <label>thing 2:</label><input />
    </form>
    
    0 讨论(0)
  • 2020-12-29 02:38

    Form controls are treated specially by browsers, so a lot of things don't necessarily work as they should. One of these things is generated content - it doesn't work for form controls. Instead, wrap the labels in <label> and use label:before { content: '\a' ; white-space: pre; }. You can also do it by floating everything and adding clear: left to the <label> elements.

    0 讨论(0)
  • 2020-12-29 02:41

    The secret is to surround your whole thingie, label and widget, in a span whose class does the block and clear:

    CSS

    <style type="text/css">
      .lb {
        display:block;
        clear:both;
      }
    </style>
    

    HTML

    <form>
      <span class="lb">Thingy 1: <input class="a" type="text" name="one" /></span>
      <span class="lb">Thingy 2: <input class="a" type="text" name="two" /></span>
      <span class="lb">Thingy 3: <input class="b" type="checkbox" name="three" /></span>
      <span class="lb">Thingy 4: <input class="b" type="checkbox" name="four" /></span>
    </form>
    
    0 讨论(0)
  • 2020-12-29 02:43

    I agree with John Millikin. You can add in <span> tags or something around each line with a CSS class defined, then make them display:block if necessary. The only other way I can think to do this is to make the <input> an inline-block and make them emit "very large" padding-right, which would make the inline content wrap down.

    Even so, your best bet is to logically group the data up in <span> tags (or similar) to indicate that that data belongs together (and then let the CSS do the positioning).

    0 讨论(0)
  • 2020-12-29 02:44

    the following would give you the newlines. It would also put extra spaces out in front though... you'd have to mess up your source indentation by removing the tabbing.

    form { white-space: pre }
    
    0 讨论(0)
  • 2020-12-29 02:45

    The javascript options are all over complicating things. Do as Jon Galloway or daniels0xff suggested.

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