【问题标题】:How to render no-table HTML form into two columns with CSS using classes?如何使用类使用 CSS 将无表格 HTML 表单呈现为两列?
【发布时间】:2015-09-05 19:11:46
【问题描述】:

我正在尝试格式化一个两列的表单,以便:

  • 标签位于每个输入字段或文本区域的上方
  • col1 中的所有内容都在左侧的列中
  • col2 中的所有内容都在右侧的列中
  • col12 类中的所有内容都跨越两列

这是我需要使用的 HTML 代码:

    <div id="contactForm">
      <form method="post" action="">
        <h3>Contact us</h3>
        <p>To send us a work order, fill in the form below.<br /><strong>All the (*) fields are mandatory.</strong></p>
        <fieldset>
          <div class="col1">
            <label for="form_firstname">Firstname <span class="required">*</span></label>
            <input type="text" id="form_firstname" name="form_firstname" value="" />
          </div>
          <div class="col2">
            <label for="form_lastname">Lastname <span class="required">*</span></label>
            <input type="text" id="form_lastname" name="form_lastname" value="" />
          </div>
          <div class="col1">
            <label for="form_address">Address</label>
            <input type="text" id="form_address" name="form_address" value="" />
          </div>
          <div class="col2">
            <label for="form_city">City</label>
            <input type="text" id="form_city" name="form_city" value="" />
          </div>
          <div class="col1">
            <label for="form_email">Email <span class="required">*</span></label>
            <input type="text" id="form_email" name="form_email" value="" />
          </div>
          <div class="col2">
            <label for="form_phone">Phone <span class="required">*</span></label>
            <input type="text" id="form_phone" name="form_phone" value="" />
          </div>
          <div class="col12">
            <label for="form_attachment">Add Attachment <span class="form_attachment">*</span></label>
            <textarea id="form_attachment" name="form_attachment" rows="5"></textarea>
          </div>
          <div class="col12">
            <label for="form_message">Message <span class="required">*</span></label>
            <textarea id="form_message" name="form_message" rows="5"></textarea>
          </div>
          <div class="col12">
            <label for="form_message">Message <span class="required">*</span></label>
            <input type="submit" value="Send" formnovalidate="formnovalidate" />
          </div>
        </fieldset>
      </form>
    </div>

如何编码 CSS 以根据需要设置此 HTML 的样式?

这是JSFiddle上的代码。

【问题讨论】:

  • 为什么要重新发明轮子,使用Bootstrap、Foundation等前端框架,直到你足够成熟
  • 好吧,我宁愿从头做起,也不愿过头。
  • 我猜你是对的,有时候这比用无用的样式让你的项目臃肿好..

标签: html css forms layout


【解决方案1】:

试试这个 CSS:

#contactForm{
    width: 80%;
}
label,.col1,.col2,.col12{display: block;}
.col1{ float:right;}
.col2{float:left;}
.col12{clear:both;}
textarea{
    display: block;
    width: 100%;
}

http://jsfiddle.net/rx2gjpdw/3/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 2013-02-06
    • 2019-10-19
    相关资源
    最近更新 更多