[英]Placing form elements on new lines without <br>
I'm trying to create a form to use for my work, I guess my question is more of a why does this happen. 我正在尝试创建用于工作的表单,我想我的问题更多是为什么会发生这种情况。
<div class="checkbox">
<input type="radio" name="transport_method" >Delivery
<input type="radio" name="transport_method">Store Pick-Up
<input type="radio" name="transport_method" >Day Trip
</div>
my css class of "checkbox" looks like this 我的“复选框”的CSS类看起来像这样
.checkbox {
float: left;
display: inline;
}
now my code at the next element 现在我的代码在下一个元素
<div>First name:<br>
<input type="text" name="firstname"><br>
</div><br><br><br>
I have to add 3 <br>
's to get the "First name:" to be on a new line. 我必须添加3 <br>
来获得“名字:”以换行。 I started with only 2 radio buttons and then I only needed 2 <br>
's. 我最初只有2个单选按钮,然后只需要2个<br>
。 Is there a way to format my css to not need any <br>
's? 有没有一种方法可以格式化我的CSS而不需要任何<br>
?
I think I need the <br>
's (correct me if I'm wrong) due to the fact that html file is reading the radio buttons as new lines and displaying them on one line, therefore the <br>
's fix that issue, but I don't like using them nor do I think it is semantically correct. 我认为我需要<br>
的(如果我错了,请纠正我),因为html文件正在将单选按钮读取为新行并将其显示在一行上,因此<br>
解决此问题问题,但我不喜欢使用它们,也不认为它在语义上是正确的。
<div class="checkbox">
<input type="radio" name="transport_method">Delivery
<input type="radio" name="transport_method">Store Pick-Up
<input type="radio" name="transport_method">Day Trip</div>
<div class="clear"></div>
<div>First name:
<input type="text" name="firstname">
</div>
.clear{clear:both}
instead of <br/>
.clear{clear:both}
而不是<br/>
EDIT: If you dont want to create new class you can use like this too : 编辑:如果您不想创建新类,也可以这样使用:
.checkbox::after {
display:block;
clear:both;
content:"";
}
The radio buttons can be wrapped in a <fieldset>
element 单选按钮可以包装在<fieldset>
元素中
The labels can all be marked up with <label>
elements . 标签都可以用<label>
元素<label>
。 The for
attribute links to its input via the matching id
attribute . for
属性通过匹配的id
属性链接到其输入。 One benefit of this is that users can click/touch on the label. 这样的好处之一是用户可以单击/触摸标签。
That gives us this: 这给了我们这个:
<form>
<fieldset class="checkbox">
<input type="radio" name="transport_method" id="delivery">
<label for="delivery">Delivery</label>
<input type="radio" name="transport_method" id="pick-up">
<label for="pick-up">Store Pick-Up</label>
<input type="radio" name="transport_method" id="day-trip">
<label for="day-trip">Day Trip</label>
</fieldset>
<fieldset class="names">
<label for="firstname">First name:</label>
<input type="text" name="firstname" id="firstname">
<label for="lastname">Last name:</label>
<input type="text" name="lastname" id="lastname">
</fieldset>
</form>
The default display value for inputs is display: inline
which brings them all onto one line. 输入的默认显示值为display: inline
,可将所有输入都display: inline
在一行上。 Use display: block
on text inputs to knock them down: 使用display: block
文本输入以将其击倒:
input[type=text] {
display: block;
}
We want the radio buttons to remain on the one line, so they can be left at their default display: inline
. 我们希望单选按钮保持在一行上,因此可以将其保留在默认display: inline
。 More information on display
. display
更多信息。
Bring it all together with a little bit more CSS: 结合使用更多的CSS:
input[type=text] { display: block; margin: 5px 0; } input[type=radio] + label { margin-right: 10px; } label, input[type=radio] { cursor: pointer; } fieldset { border: none; } form { background: #FFF9C4; width: 500px; font-family: sans-serif; }
<form> <fieldset class="checkbox"> <input type="radio" name="transport_method" id="delivery"> <label for="delivery">Delivery</label> <input type="radio" name="transport_method" id="pick-up"> <label for="pick-up">Store Pick-Up</label> <input type="radio" name="transport_method" id="day-trip"> <label for="day-trip">Day Trip</label> </fieldset> <fieldset class="names"> <label for="firstname">First name:</label> <input type="text" name="firstname" id="firstname"> <label for="lastname">Last name:</label> <input type="text" name="lastname" id="lastname"> </fieldset> </form>
<div class="checkbox">
<input type="radio" name="transport_method" >Delivery
<input type="radio" name="transport_method">Store Pick-Up
<input type="radio" name="transport_method" >Day Trip
</div>
<div class="clear"></div>
<div>
First name:
<br>
<input type="text" name="firstname"><br>
</div>
<div class="clear"></div>
in css 在CSS
.clear{
clear:both
}
It's as simple as this: 就这么简单:
.checkbox{display:block}
And if you mean to have those checbox inputs floated to left, then use 而且,如果您要使这些复选框的输入浮动到左侧,请使用
.checkbox input{display:inline-block}
And there you go, no floats, no br
tags, nothing weird 然后就可以了,没有浮点数,没有br
标签,没有任何奇怪的东西
Using the new class amit made 使用新的课程完成
use .clear{clear:both} instead of 使用.clear {clear:both}而不是
on the following element, in my case 在以下情况下,就我而言
<div >First name:<br>
<input type="text" name="firstname"><br>
</div>
turned into 转换成
<div class="clear">First name:<br>
<input type="text" name="firstname"><br>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.