Bootstrap4使所有输入组插件的宽度相同

Pau*_*lor 8 css twitter-bootstrap bootstrap-4

是否可以使所有prepend附加组件的宽度相同?

即在这个屏幕截图中,我希望电子邮件,许可证密钥1和许可证密钥2的长度相同,这是可能的.

如果我没有使用附加组件并且只使用常规标签和表单网格,那么它可能是前置插件看起来比常规标签好得多.

在此输入图像描述

相关的Html是

<main class="container-fluid">
  <form action="/license.process" name="process" id="process" method="post">
      <div class="input-group mb-2">
          <div class="input-group-prepend">
              <label class="input-group-text" id="licenseEmaillabel">
                  Email
              </label>
          </div>
          <input type="text" name="licenseEmail" value="paultaylor@jthink.net" class="form-control" aria-describedby="licenseEmaillabel">
      </div>
      <div class="input-group mb-2">
          <div class="input-group-prepend">
              <label class="input-group-text" id="licenseKey1label">
                  License Key 1
              </label>
          </div>
          <input type="text" name="licenseKey1" value="51302c021440d595c860233f136731865a12cfad2ce2cc2" class="form-control" aria-describedby="licenseKey1label">
      </div>
      <div class="input-group mb-2">
          <div class="input-group-prepend">
              <label class="input-group-text" id="licenseKey2label">
                  License Key 2
              </label>
          </div>
          <input type="text" name="licenseKey2" value="1e50214376557ba3e1dede6c490f33d07b738515c8c2a03" class="form-control" aria-describedby="licenseKey2label">
      </div>
      <h3 class="error" style="visibility:hidden;">
          &nbsp;
      </h3>
      <input type="submit" name="cancel" value="Cancel" class="btn btn-outline-secondary">
      <input type="submit" name="save" value="Save" class="btn btn-outline-secondary">
      <button onclick="j2html.tags.UnescapedText@d352d4f" type="button" class="btn btn-outline-secondary">
          Get License
      </button>
  </form>
</main>
Run Code Online (Sandbox Code Playgroud)

sou*_*ned 10

所以这实际上非常复杂,因为每个标签都在它自己的div中,而不是兄弟链的一部分.而afaik,Bootstrap不支持这种类型的大小调整,只支持相对大小调整的类(基本上只会使字体更大).这种消除了我可以想到使用flex/child属性的大多数可能性.但是,我认为在这种情况下硬编码不是正确的用法.但这个想法是一样的.

min-width在这种情况下使用的例子不正确,因为min-width!== width=xx.例如,如果设置min-width为50px,但是1个文本字符串比此长,则仍然存在与上述相同的问题.基本上,如果您不想将它们全部设置为一个特定值,那么您唯一的另一个选择是使用Javascript.

这是我的纯CSS解决方法:

.input-group-prepend {
  width : 35%; /*adjust as needed*/
}

.input-group-prepend label {
  width: 100%;
  overflow: hidden;
}
Run Code Online (Sandbox Code Playgroud)

此外,您可以使用Boostrap特定的内联样式类,但这是任意的和Bootstrap的问题之一(太多的内联类杂乱代码,然后您必须手动将其添加到每个元素).只是提供它作为替代

例如:

<div class="input-group-prepend w-50"> /* grows to 50% of parent*/
  <label class="input-group-text w-100" id="licenseEmaillabel"> /* grows to 100% of parent */
Run Code Online (Sandbox Code Playgroud)


小智 5

只需使用引导程序类:

<div class="input-group">
    <div class="input-group-prepend col-3">
        <span class="input-group-text col-12">Name:</span>
    </div>
    <input type="text" class="form-control" placeholder="Name">
</div>
<div class="input-group">
    <div class="input-group-prepend col-3">
        <span class="input-group-text col-12">Address 1:</span>
    </div>
    <input type="text" class="form-control" placeholder="Street Address">
</div>
Run Code Online (Sandbox Code Playgroud)

看起来像这样: 预置所有相同的宽度