我有两个div:
<div><label for="1">Some really loooong text... Which breaks the second div. Which then appears shifted to the right.</label><input type="text" id="1" name"1"></div>
<div><label for="2">Shifted.</label><input type="text" id="2" name"2">
Run Code Online (Sandbox Code Playgroud)
我使用以下css浮动输入文本字段左侧的标签:
label{
width:200px;
float:left;
}?
Run Code Online (Sandbox Code Playgroud)
第二个div向左移动而不是出现在第一个div之下.它应该作为第一个对齐到左边.
这是一个例子:http://jsfiddle.net/qh37Y/
现在,如果我插入一个明确的:两个; div它有效,但这是最好的做法吗?
.clearer {
clear:both;
}
<div class="clearer"><label for="2">Shifted.</label><input type="text" id="2" name"2">?
Run Code Online (Sandbox Code Playgroud)
首先,你不能使用以数字开头的标识符 - 只是说';)
如果我理解你的问题,解决方案是添加clear: both
到你的 div 中:
div {
clear: both
}
将代码放在标签定义下。