简体   繁体   English

使用引导程序,在标签中包装输入文本元素会使输入文本变为粗体而不占用整个宽度

[英]Using bootstrap, wrapping input text element in label causes input text to bold and not take up full width

I am trying to wrap my text input with a label. 我试图用标签包装我的文本输入。 I have some pickers on my form, and wrapping them in a label enables me to click on the label and activate the picker. 我的表格上有一些拣货员,并将它们包裹在标签中,这样我就可以点击标签并激活拣货员。 However, when I try this with the text box it breaks the form. 但是,当我尝试使用文本框时,它会破坏表单。 If I choose not to wrap the text boxes, the spacing between the label and the text box is different than the wrapped elements. 如果我选择不包装文本框,则标签和文本框之间的间距与包装的元素不同。

This doesn't work: 这不起作用:

<div class="form-group">
    <label>
        Title:
        <input  type="text" class="form-control" data-bind="value: title" />
    </label>
</div>

This works: 这有效:

<div class="form-group">
    <label>
        Date:
        <div class="input-group">
            <input type="text" class="form-control" placeholder="mm/dd/yy" data-provide="datepicker" data-bind="value  :date" id="Date" />
            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
        </div>
    </label>
</div>    

Also, the text inside the text inputs should not be bold. 此外,文本输入内的文本不应为粗体。

形成

You want to update your css, changing your label's to either display as block, or set their width to 100%. 您想要更新您的css,将标签更改为显示为块,或将其宽度设置为100%。 Then you also want to have input's inside labels set to a font-weight of normal. 然后你还想让输入的内部标签设置为正常的字体权重。

label {
    display:block;
}
label input {
    font-weight:normal
}

https://jsfiddle.net/partypete25/95ygubwx/ https://jsfiddle.net/partypete25/95ygubwx/

As an alternative, don't need to wrap elements for activation, you can just use the for attribute: 作为替代方案,不需要包装元素进行激活,您只需使用for属性:

<label for="title">Title</label>
<input id="title" type="text" class="form-control" data-bind="value: title" />

If you don't want this, then you need to understand cascading styles. 如果您不想这样,那么您需要了解级联样式。

You don't need to wrap input inside label. 您不需要在标签内包装输入。 for attribute of label element is doing the same thing you want to achieve.The correct way to create form control in bootstrap is : 对于 label元素的属性正在做你想要实现的同样的事情。在bootstrap中创建表单控件的正确方法

<div class="form-group">
  <label for="title">Title:</label>
    <input type="text" class="form-control" data-bind="value: title" id="title" />      
</div>

Why input not taking full-width and text being bold : Following is the label css applied by bootstrap library. 为什么输入不采用全角和文本为粗体 :以下是bootstrap库应用的标签css。

label {
    display: inline-block;
    max-width: 100%;
    margin-bottom: 5px;
    font-weight: 700;
}

In your case, if you wrap input inside label, input is inhering the font-weight: 700 px; 在您的情况下,如果您将输入包装在label内,则输入将继承font-weight:700 px; from it's parent(label) and hence bold. 从它的父(标签),因此大胆。

And the parent label's max-width: 100% is restricting the input width. 父标签的max-width:100%限制了输入宽度。 If it could have width: 100% than, input would take full width. 如果它的宽度可以是100% ,则输入将采用全宽。 There is difference between max-width and width. 最大宽度和宽度之间存在差异。

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

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