简体   繁体   English

如何并排获得两个表单字段,每个字段的 label 在字段上方,在 CSS 中?

[英]How can I get two form fields side-by-side, with each field’s label above the field, in CSS?

I am stuck on figuring out some css, I need a section of my form to look like the following,我坚持要找出一些 css,我需要我的表格的一部分如下所示,

CSS 向左浮动

I have tried every variation I can think of,我已经尝试了所有我能想到的变化,

I have given the labels a fixed width and floated them left, then given the inputs the same width and floated them left.我给标签一个固定的宽度并将它们向左浮动,然后给输入相同的宽度并将它们向左浮动。

I am all out of ideas, how can I achieve this please?我完全没有想法,请问我该如何实现?

<form>
  <label for="company">
    <span>Company Name</span>
    <input type="text" id="company" />
  </label>
  <label for="contact">
    <span>Contact Name</span>
    <input type="text" id="contact" />
  </label>
</form>

label { width: 200px; float: left; margin: 0 20px 0 0; }
span { display: block; margin: 0 0 3px; font-size: 1.2em; font-weight: bold; }
input { width: 200px; border: 1px solid #000; padding: 5px; }

Illustrated at http://jsfiddle.net/H3y8j/ 图为http://jsfiddle.net/H3y8j/

You need an HTML element for each column in your layout. 您需要为布局中的每个列添加一个HTML元素。

I'd suggest: 我建议:

HTML HTML

<div class="two-col">
    <div class="col1">
        <label for="field1">Field One:</label>
        <input id="field1" name="field1" type="text">
    </div>

    <div class="col2">
        <label for="field2">Field Two:</label>
        <input id="field2" name="field2" type="text">
    </div>
</div>

CSS CSS

.two-col {
    overflow: hidden;/* Makes this div contain its floats */
}

.two-col .col1,
.two-col .col2 {
    width: 49%;
}

.two-col .col1 {
    float: left;
}

.two-col .col2 {
    float: right;
}

.two-col label {
    display: block;
}

How about something like this? 这样的事怎么样?

<div id="leftContainer">
  <span>Company Name</span>
  <br><input type="text" value="John Lewis Partnership">
</div>
<div id="rightContainer">
  <span>Contact Name</span>
  <br><input type="text" value="Timothy Patten">
</div>

Then, you can align the 2 divs by floating them left and right:- 然后,您可以通过左右浮动来对齐2个div: -

#leftContainer {
   float:left;
}

#rightContainer {
   float:right;
}

This worked perfectly for me without css. 没有CSS,这对我来说非常合适。 I think css would put some icing on the cake though. 我认为css会给蛋糕锦上添花。

    <form>
        <label for="First Name" >First Name:</label>
            <input type="text" name="username" size="15" maxlength="30" />
        <label for="Last Name" >Last Name:</label>
            <input type="text" name="username" size="15" maxlength="30" />
    </form>
<div>
<div style="float:left; width:101px; height:auto;">
    <div style="width:200px; float:left;">
        LabelText
    </div>
    <div style="width:200px; float:left;">
        <input type="text" name="textfield" id="textfield" />
    </div>
</div>
    <div style="float:left; width:101px; height:auto;">
    <div style="width:200px; float:left;">
        LabelText
    </div>
    <div style="width:200px; float:left;">
        <input type="text" name="textfield" id="textfield" />
    </div>
</div>



</div>

Give this a try 试一试

<style type="text/css">

form {width:400px;}

#text1 {float:right;}

#text2 {float:left;}

</style>

then 然后

<form id="form1" name="form1" method="post" action="">


 <label id="text1">Company Name<br />
   <input type="text" name="textfield2" id="textfield2" />
 </label>

 <label id="text2">Contact Name<br />
   <input type="text" name="textfield" id="textfield" />
 </label>


</form>

Test Page: http://jsbin.com/ahelo4 测试页面: http //jsbin.com/ahelo4

Works for me in the latest versions of Firefox, Safari, Chrome, Opera. 适用于最新版本的Firefox,Safari,Chrome,Opera。 Not 100% sure about IE though as im on a Mac, but I cant see why it wouldn't :) 不是100%肯定IE虽然作为我在Mac上,但我不明白为什么它不会:)

Here is a simple way to do it这是一个简单的方法

 .style1 {display: inline-block; }
 <form> <div class="style1"> <label for="field1">Company Name</label> <br> <input type="text" id="field1" name="field1"> </div> <div class="style1"> <label for="field2">Contact Name</label> <br> <input type="text" id="field2" name="field2"> </div> </form>

我怎样才能得到一个<div>漂浮在另外两个之上<div> s 如果空间太窄,三个人都不能并排?</div><div id="text_translate"><p> 我有一个由两个&lt;div&gt;并排组成的进度条,在&lt;dd&gt;内浮动到右侧。 在进度条的左侧,我在另一个&lt;div&gt;中有一些文本。 如果&lt;dd&gt;太窄,我希望&lt;div&gt;包含文本到进度条顶部的 go,而不是分成两行。</p><p> 我正在使用 bootstrap.css 没有修改: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> .col-6 { -ms-flex: 0 0 50%; flex: 0 0 50%; max-width: 50%; position: relative; width: 100%; padding-right: 15px; padding-left: 15px; }.my-0 { margin-top: 0;important. }:py-2 { padding-top. 0;5rem.important: };float-left { float. left:important; }.float-right { float: right;important. }:bg-success { background-color; #28a745 !important; } .bg-danger { background-color: #dc3545 !important; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;dd class="col-6 my-0"&gt; &lt;div class="float-left"&gt;Some text&lt;/div&gt; &lt;div class="py-2 float-right bg-success" style="width:10px;"&gt;&lt;/div&gt; &lt;div class="py-2 float-right bg-danger" style="width:90px;"&gt;&lt;/div&gt; &lt;/dd&gt;</pre></div></div><p></p><p> 我尝试将z-index添加到&lt;div&gt; ,但无法达到我想要的效果。</p><p> 怎么做?</p></div></div> - How can I get a <div> to float on top of two other <div>s if the space is too narrow for all three to stay side-by-side?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何并排获得两个表单域,每个图例位于该域上方? - How can I get two form fields side-by-side, with each legend above the field? 我怎样才能得到一个<div>漂浮在另外两个之上<div> s 如果空间太窄,三个人都不能并排?</div><div id="text_translate"><p> 我有一个由两个&lt;div&gt;并排组成的进度条,在&lt;dd&gt;内浮动到右侧。 在进度条的左侧,我在另一个&lt;div&gt;中有一些文本。 如果&lt;dd&gt;太窄,我希望&lt;div&gt;包含文本到进度条顶部的 go,而不是分成两行。</p><p> 我正在使用 bootstrap.css 没有修改: </p><p></p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false"><div class="snippet-code"><pre class="snippet-code-css lang-css prettyprint-override"> .col-6 { -ms-flex: 0 0 50%; flex: 0 0 50%; max-width: 50%; position: relative; width: 100%; padding-right: 15px; padding-left: 15px; }.my-0 { margin-top: 0;important. }:py-2 { padding-top. 0;5rem.important: };float-left { float. left:important; }.float-right { float: right;important. }:bg-success { background-color; #28a745 !important; } .bg-danger { background-color: #dc3545 !important; }</pre><pre class="snippet-code-html lang-html prettyprint-override"> &lt;dd class="col-6 my-0"&gt; &lt;div class="float-left"&gt;Some text&lt;/div&gt; &lt;div class="py-2 float-right bg-success" style="width:10px;"&gt;&lt;/div&gt; &lt;div class="py-2 float-right bg-danger" style="width:90px;"&gt;&lt;/div&gt; &lt;/dd&gt;</pre></div></div><p></p><p> 我尝试将z-index添加到&lt;div&gt; ,但无法达到我想要的效果。</p><p> 怎么做?</p></div></div> - How can I get a <div> to float on top of two other <div>s if the space is too narrow for all three to stay side-by-side? 我该如何显示? <ul> 作为使用CSS的两个并排列? - How can I display a <ul> as two side-by-side columns using CSS? 如何并排获得这两个div? - How to get these two divs side-by-side? HTML / CSS:无法使文本省略号与两个并排的文本字段一起使用 - HTML/CSS: Cannot get text-ellipsis to work with two side-by-side text fields 如何将标签强制到表单字段的左侧 - How to force label to left side of form field 我试图让表单的字段在移动设备上堆叠,但在桌面设备上并排放置(flexbox) - I'm trying to get the fields of a form to stack when on mobile but stay side-by-side when on desktop (flexbox) 如何并排放置两个输入字段 - How to Place Two Input Fields Side-by-Side 如何获得两个并排的html输入字段,使其看起来小于小于输入字段高度的中间分隔符? - How do I get two side by side html input fields to look combined with a middle separator that is less than the input field heights? CSS /表格:如何并排放置两个版本的文本? - CSS/tables: how to put two versions of a text side-by-side?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM