What is the best way to style a list of checkboxes

前端 未结 4 1318
北海茫月
北海茫月 2020-12-29 05:19

What I\'d like to achieve is a layout like this

some label  [ ] checkbox 1
            [ ] checkbox 2
            [ ] checkbox 3
            [ ] checkbox 4
相关标签:
4条回答
  • 2020-12-29 05:45

    This very semantic HTML:

    <fieldset class="checkboxgroup">
        <p>some label</p>
        <label><input type="checkbox"> checkbox 1</label>
        <label><input type="checkbox"> checkbox 2</label>
        <label><input type="checkbox"> checkbox 3</label>
        <label><input type="checkbox"> checkbox 4</label>
    </fieldset>
    

    And this fairly simple CSS:

    .checkboxgroup{
        width: 20em;
        overflow: auto;
    }
    .checkboxgroup p{
        width: 7em;
        text-align: right;
    }
    .checkboxgroup label{
        width: 12em;
        float: right;
    }
    

    Adjust widths as needed.

    The proper way to do this really is to replace the p element in my HTML with a legend element, but this won't style the way you want it to without some pretty ugly CSS.

    0 讨论(0)
  • 2020-12-29 05:53

    In my opinion its more some kind of list than a table (but You did not list the whole picture). To me it looks like a definition list so I would use it (if not I would stick to a unordered list example the Magnar solution, adding labels.

    The definition list version:

     <dl id="checkboxes">
            <dt>same label or term</dt>
            <dd><input type="checkbox" id="chk1" /><label for="chk1">checkbox 1</label></dd>
            <dd><input type="checkbox" id="chk2" /><label for="chk2">checkbox 2</label></dd>
            <dd><input type="checkbox" id="chk3" /><label for="chk3">checkbox 3</label></dd>
            <dd><input type="checkbox" id="chk4" /><label for="chk4">checkbox 4</label></dd>
      </dl>
    
    0 讨论(0)
  • 2020-12-29 05:58

    I would use this markup:

    <div id="checkboxes">
      <label>some label</label>
      <ul>
        <li><input type="checkbox"> checkbox 1</li>
        <li><input type="checkbox"> checkbox 2</li>
        <li><input type="checkbox"> checkbox 3</li>
        <li><input type="checkbox"> checkbox 4</li>
      </ul>
    </div>
    

    and these styles:

    #checkboxes label {
      float: left;
    }
    #checkboxes ul {
      margin: 0;
      list-style: none;
      float: left;
    }
    

    Tables aren't evil, but they're used for the wrong reasons more often than not. They make for bigger html-files (bad for performance and bandwidth), usually with a more cluttered html-structure (bad for maintainability). As for tabular data however, they are excellent.

    0 讨论(0)
  • 2020-12-29 05:59
    <div style="float: left;">
        some label
    </div>
    
    <div style="float: left;">
        <input type="checkbox" />&#160;checkbox 1<br />
        <input type="checkbox" />&#160;checkbox 2<br />
        <input type="checkbox" />&#160;checkbox 3<br />
        <input type="checkbox" />&#160;checkbox 4
    </div>
    
    0 讨论(0)
提交回复
热议问题