Arranging fieldset elements like a typical table-design

后端 未结 2 2018
余生分开走
余生分开走 2020-12-19 22:45

I\'m trying to arrange the titles for 3 fieldset elements the same way a typical table looks, but I can\'t get it the way I want. This comes pretty close, however...

相关标签:
2条回答
  • 2020-12-19 22:59

    what you could do is remove the label's from the flow so they don't get vertically aligned with the inputs/text.. do this by absolutely positioning them.. this will require a parent element to have position: relative; on it - I presume the overall code above is in a form element but for the sake a demo I've just wrapped all your code in a div.

    Working Example

    HTML:

    <div id="form"> 
    
      <label>Title1</label>
      <fieldset>
        <input value="Lorem Ipsum" /><br />
        <input value="Lorem Ipsum" /><br />
        <input value="Lorem Ipsum" />
      </fieldset>
    
      <label>Title2</label>
      <fieldset>
        <input value="Lorem Ipsum" />
      </fieldset>
    
      <label>Title3</label>
      <fieldset>
      Lorem Ipsum
      </fieldset>
    
    </div>
    

    CSS:

    #form {
      position: relative; /* labels need this on the their parent element */
    }
    
    fieldset {
      margin: 0;
      padding: 0;
      border: 0;
      padding-top: 30px; /* leave a space to position for the labels */
    }
    
    fieldset {display: inline-block; vertical-align: middle;}
    fieldset {display: inline !ie7; /* IE6/7 need display inline after the inline-block rule */}
    
    label {
       position: absolute; 
       top: 5px; 
       left: auto; 
       margin-left: 5px; 
       font-weight: bold;
    }
    

    added per comments

    because there's not enough room in comments, here's the code I was thinking which doesn't position the label, to do this the label would need to go inside the vertically aligned fieldset

    #form {
      position: relative; /* labels need this on the their parent element */
    }
    
    fieldset {
      margin: 0;
      padding: 0;
      border: 0;
    }
    
    fieldset {display: inline-block; vertical-align: middle; background: #eee;}
    fieldset {display: inline !ie7;}
    
    
    label {
       display: block;
       font-weight: bold;
    }
    

    HTML:

    <fieldset>
      <label>Title1</label>
      <input value="Lorem Ipsum" /><br />
      <input value="Lorem Ipsum" /><br />
      <input value="Lorem Ipsum" />
    </fieldset>
    
    
    <fieldset>
      <label>Title2</label>
      <input value="Lorem Ipsum" />
    </fieldset>
    
    
    <fieldset>
      <label>Title3</label>
      Lorem Ipsum
    </fieldset>
    
    0 讨论(0)
  • 2020-12-19 23:00

    Like this

        .divTable {
          display: table;
          width: 50%;
        }
        .divRow {
          display: table-row;
        }
        .cellOne,
        .cellTwo {
          display: table-cell;
          padding: 20px;
          width: 30%;
        }
    <fieldset id="fileConnSet">
      <legend>File:</legend>
      <div class="divTable">
        <!--Table start-->
        <div class="divRow">
          <!--1st row, like <tr>-->
          <div class="cellOne">
            <!--1st column, like <td>-->
            <label>Name:</label>
          </div>
          <div class="cellTwo">
            <!--2nd column-->
            <input type="text" />
          </div>
        </div>
    
        <div class="divRow">
          <!--2nd row, like <tr>-->
          <div class="cellOne">
            <label>Age:</label>
          </div>
          <div class="cellTwo">
            <input type="text" />
          </div>
        </div>
    
        <div class="divRow">
          <!--3rd row, like <tr>-->
          <div class="cellOne">
            <label>Company:</label>
          </div>
          <div class="cellTwo">
            <input type="text" />
          </div>
        </div>
      </div>
      <!--Table end-->
    </fieldset>

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