简体   繁体   English

造型引导主题复选框与单行标签

[英]Styling bootstrap themed checkbox with label on single line

I am attempting to utilize the bootstrap "form-control" class on a checkbox within a label tag so that clicking the text also checks the textbox. 我试图在标签标签中的复选框上使用引导程序“form-control”类,以便单击文本也检查文本框。

Here is the JSFiddle: https://jsfiddle.net/vpm13m2b/ 这是JSFiddle: https ://jsfiddle.net/vpm13m2b/

The HTML for the control is: 控件的HTML是:

<div class="form-group">
    <div>
        Attempt #1
        <span class="red">*</span>
    </div>
    <label>
        <input type="checkbox" class="form-control" /> Yes
    </label>
</div>
<div class="form-group">
    <div>
        Attempt #2
        <span class="red">*</span>
    </div>
    <label class="checkbox-inline">
        <input type="checkbox" class="form-control" /> Yes
    </label>
</div>

With attempt 1, the "Yes" text is pushed to a separate line. 尝试1时,“是”文本被推送到单独的行。 With attempt 2, the checkbox and underlying controls are pulled the width of the page, which also pushes the "Yes" text to the second line. 在尝试2中,复选框和底层控件被拉到页面的宽度,这也将“是”文本推到第二行。 The screenshot of this is below: 截图如下:

截图

Here's what I am trying to do: 这是我想要做的:

  • The styled checkbox is displayed next to the "Yes" 样式复选框显示在“是”旁边
  • Selecting the text also selects the checkbox 选择文本也会选中该复选框
  • Keep the solution clean (trying to avoid dealing with float:left or jquery click events on the text to check the checkbox) 保持解决方案干净(试图避免处理float:left文本float:left或jquery点击事件以检查复选框)

It just seems that there has to be a vanilla way to do this. 似乎必须有一种香草的方式来做到这一点。 All the bootstrap docs just show standard checkboxes - nothing with the form-control class styling the checkbox for their nice inline examples. 所有的bootstrap文档只显示标准的复选框 - 没有任何form-control类样式化其漂亮的内联示例的复选框。

Remove the class="form-control" from your checkboxes. 从复选框中删除class="form-control" As the bootstrap docs state: 正如bootstrap文档所述:

All textual <input> , <textarea> , and <select> elements with .form-control are set to width: 100%; 具有.form-control的所有文本<input><textarea><select>元素都设置为width:100%; by default. 默认情况下。

jsFiddle example jsFiddle例子

The .form-control class has about a dozen properties being set that you most likely don't want or need. .form-control类有大约十几个你很可能不想要或不需要的属性。

Here's what I came up with to fix my issue. 这是我想出来解决我的问题。 I need to retain the .form-control class on the checkbox element so that the checkbox control is styled by the bootstrap theme. 我需要在checkbox元素上保留.form-control类,以便复选框控件由引导主题设置样式。 My busines requirement is to use the themed, not default browser, checkbox. 我的业务要求是使用主题,而不是默认浏览器,复选框。

See working JSFiddle here: https://jsfiddle.net/vpm13m2b/3/ 在这里看到工作JSFiddle: https ://jsfiddle.net/vpm13m2b/3/

input[type=checkbox].form-control {
    width: 25px;
    height: 25px;
    display: inline-block;
    vertical-align: -8px;
}

The css above was added into my site's stylesheet so that the form-control checkboxes retain the style, and I don't have to change the existing code that has a label containing the checkbox and caption to the right of the checkbox vertically centered. 上面的css被添加到我网站的样式表中,因此表单控件复选框保留了样式,我不必更改现有代码,该代码具有包含复选框和标题的标签,该复选框和标题位于垂直居中的复选框的右侧。

This works with my original attempt (#1) on the html below: 这适用于我在html下面的原始尝试(#1):

<div class="form-group">
    <div>
        Attempt #1
        <span class="red">*</span>
    </div>
    <label>
        <input type="checkbox" class="form-control" /> Yes
    </label>
</div>

The resulting output matches what I'm looking for in the original question: 结果输出符合我在原始问题中寻找的内容:

解决方案截图

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM