在 li
标签中,我放置了一个复选框和标签输入。
如果标签文本比标签大,文本会移到下一行。
我包装了标签文本,但如果标签文本太长,它没有对齐复选框和标签在同一行。
<li>
<input id="checkid" type="checkbox" value="test" />
<label style="word-wrap:break-word">testdata</label>
</li>
<li>
<label for="checkid" style="word-wrap:break-word">
<input id="checkid" type="checkbox" value="test" />testdata
</label>
</li>
CSS:
[type="checkbox"]
{
vertical-align:middle;
}
<label for="">
来处理复选框或单选框,并且要对齐复选框,vertical-align
就足够了。<li>
<input id="checkid" type="checkbox" value="test" />
<label for="checkid">testdata</label>
</li>
<li>
<input id="checkid2" type="checkbox" value="test" />
<label for="checkid2">testdata 2</label>
</li>
并设置 CSS 如下
input[type="checkbox"]
{
vertical-align:middle;
}
如果有较长的文本内容
label,input{
display: inline-block;
vertical-align: middle;
}
小贴士:在
以上建议对我来说都不起作用。我必须使用以下方法将复选框居中,并将标签文本显示在框的右侧:
<style>
.checkboxes {
display: flex;
justify-content: center;
align-items: center;
vertical-align: middle;
word-wrap: break-word;
}
</style>
<label for="checkbox1" class="checkboxes"><input type="checkbox" id="checkbox1" name="checked" value="yes" class="checkboxes"/>
Check the box.</label>
我也遇到了同样的问题,但是没有一个答案对我有用。 我正在使用 Bootstrap,下面的 CSS 代码帮助了我:
label {
display: contents!important;
}
这里还有另一种方法:
.checkbox-wrapper {
white-space: nowrap
}
.checkbox {
vertical-align: top;
display:inline-block
}
.checkbox-label {
white-space: normal
display:inline-block
}
<div class="text-left checkbox-wrapper">
<input type="checkbox" id="terms" class="checkbox">
<label class="checkbox-label" for="terms">I accept whatever you want!</label>
</div>
如果您正在使用Bootstrap,请使用“checkbox”或“checkbox-inline”类将标签和输入框包装在一个div中。
<li>
<div class="checkbox">
<label><input type="checkbox" value="">Option 1</label>
</div>
</li>
Reference: https://www.w3schools.com/bootstrap/bootstrap_forms_inputs.asp
.chkbox label
{
position: relative;
top: -2px;
}
<div class="chkbox">
<asp:CheckBox ID="Ckbox" runat="server" Text="Check Box Alignment"/>
</div>
这对我有用
CSS
input[type="checkbox"] {
display: inline-block;
width: auto;
}
HTML
<div class="checkbox">
<input type="checkbox">
<label>test data</label>
</div>
<li>
<input id="checkid" type="checkbox" value="test">
<label>testdata</label>
</li>
li input {float: left;}
li label {word-wrap: break-word; line-height: 16px; float: left;}
<li>
<div>
<input id="checkid" type="checkbox" value="test" />
</div>
<div>
<label style="word-wrap:break-word">testdata</label>
</div>
</li>