CSS如何在输入类型=“复选框”中对齐标签和输入
我遇到一个问题,我的标签文本和相应的复选框不是彼此相邻,而是对角线。我尝试使用 float 来修复它,但它对我来说没有正确工作。
label {
font-weight: bold;
margin: 1em;
}
input {
padding: 1em;
margin: 1em;
width: 100%;
}
<label className="toppings">Toppings:
<br />
<label> Pepperoni
<input ype="checkbox" name="pepperoni" />
</label>
<label> Pineapple
<input type="checkbox" name="pineapple" />
</label>
</label>
使用上面的代码,标签文本(例如“pepperoni”)位于复选框的对角上方。对如何让它们并排有任何帮助吗?
I'm having an issue where my label text and the corresponding checkbox or not next to each other, but rather diagonal. I've tried to fix it using float, but it hasn't worked correctly for me.
label {
font-weight: bold;
margin: 1em;
}
input {
padding: 1em;
margin: 1em;
width: 100%;
}
<label className="toppings">Toppings:
<br />
<label> Pepperoni
<input ype="checkbox" name="pepperoni" />
</label>
<label> Pineapple
<input type="checkbox" name="pineapple" />
</label>
</label>
With the above code, the label text (for example "pepperoni") is diagonally above the checkbox. Any help on how I can get them side-by-side?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一些小事情:
首先,你不应该有一个包含其他标签和输入的标签标签(它是无效的)
其次,如果您想为特定输入添加标签,您可以使用允许的属性
for
您将标签链接到输入several little things :
first you should not have a label tag that contain other label and input in it (it's invalid)
secondly if you want to have a label for a specific input you can use the attributes
for
that allow you to link a label to an input这取决于您的最终结果在设计上应该是什么样子,但像这样更改标记是一种选择。
It depends on what your final result is supposed to look like design-wise, but changing your markup like this is one option.
一种方法如下,代码中带有解释性注释:
JS Fiddle 演示。
One approach is as follows, with explanatory comments in the code:
JS Fiddle demo.