【问题标题】:Use Fieldset Legend with bootstrap将 Fieldset Legend 与引导程序一起使用
【发布时间】:2013-05-26 23:47:56
【问题描述】:

我正在为我的JSP 页面使用 Bootstrap。

我想在我的表单中使用 <fieldset><legend>。这是我的代码。

<fieldset class="scheduler-border">
    <legend class="scheduler-border">Start Time</legend>
    <div class="control-group">
        <label class="control-label input-label" for="startTime">Start :</label>
        <div class="controls bootstrap-timepicker">
            <input type="text" class="datetime" id="startTime" name="startTime" placeholder="Start Time" />
            <i class="icon-time"></i>
        </div>
    </div>
</fieldset>

CSS 是

fieldset.scheduler-border {
    border: 1px groove #ddd !important;
    padding: 0 1.4em 1.4em 1.4em !important;
    margin: 0 0 1.5em 0 !important;
    -webkit-box-shadow:  0px 0px 0px 0px #000;
            box-shadow:  0px 0px 0px 0px #000;
}

legend.scheduler-border {
    font-size: 1.2em !important;
    font-weight: bold !important;
    text-align: left !important;
}

我得到这样的输出

我想通过以下方式输出

我尝试添加

border:none;
width:100px;

legend.scheduler-border 在 CSS 中。我得到了预期的输出。但问题是我想为另一个字段添加另一个 &lt;fieldset&gt;。那时,图例中文本的宽度是一个问题,因为它比 100px 更长。

那么我应该怎么做才能得到我提到的输出呢? (不打图例文字)

【问题讨论】:

  • 您共享的代码似乎运行良好。 Here's a Fiddle
  • @Aquillo 你的 Fiddle 没有使用 Bootstrap。
  • @JamesDonnelly 也注意到这是由 Bootstrap 引起的;)
  • 我认为你应该考虑使用 Bootstrap 的面板。它具有与 fieldset 和 legend 非常相似的行为,但更时尚。

标签: html css forms twitter-bootstrap


【解决方案1】:

这是因为 Bootstrap 默认将 legend 元素的宽度设置为 100%。您可以通过将 legend.scheduler-border 更改为也使用来解决此问题:

legend.scheduler-border {
    width:inherit; /* Or auto */
    padding:0 10px; /* To give a bit of padding on the left and right */
    border-bottom:none;
}

JSFiddle example.

您还需要确保在 Bootstrap 之后添加自定义样式表,以防止 Bootstrap 覆盖您的样式 - 尽管您的样式在这里应该具有更高的特异性。

您可能还想在其中添加margin-bottom:0; 以减少图例和分隔线之间的差距。

【讨论】:

  • Ftr:对我来说,Bootstrap 3.3.6 还在legend 上设置了一个margin-bottom: 20px;,这将其推到了字段集边界之上,而不是放在行上。在自定义legend.scheduler-border 上设置margin-bottom: 0; 很容易解决。
【解决方案2】:

我遇到了这个问题,我用这种方式解决了:

fieldset.scheduler-border {
    border: solid 1px #DDD !important;
    padding: 0 10px 10px 10px;
    border-bottom: none;
}

legend.scheduler-border {
    width: auto !important;
    border: none;
    font-size: 14px;
}

【讨论】:

    【解决方案3】:

    我有一个不同的方法,使用引导面板来显示它更丰富。只是为了帮助某人并改进答案。

    .text-on-pannel {
      background: #fff none repeat scroll 0 0;
      height: auto;
      margin-left: 20px;
      padding: 3px 5px;
      position: absolute;
      margin-top: -47px;
      border: 1px solid #337ab7;
      border-radius: 8px;
    }
    
    .panel {
      /* for text on pannel */
      margin-top: 27px !important;
    }
    
    .panel-body {
      padding-top: 30px !important;
    }
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
    <div class="container">
      <div class="panel panel-primary">
        <div class="panel-body">
          <h3 class="text-on-pannel text-primary"><strong class="text-uppercase"> Title </strong></h3>
          <p> Your Code </p>
        </div>
      </div>
      <div>

    这将给出下面的样子。

    注意:我们需要更改样式以使用不同的标题大小。

    【讨论】:

      【解决方案4】:

      只是想简单地总结一下上面所有的正确答案。因为我不得不花费大量时间来弄清楚哪个答案可以解决问题以及幕后发生了什么。

      bootstrap的fieldset似乎有两个问题:

      1. bootstrap 将宽度设置为legend 为 100%。这就是为什么它会覆盖fieldset 的顶部边框。
      2. legend 有一个 bottom border

      所以,我们需要解决的只是将图例宽度设置为自动,如下所示:

      legend.scheduler-border {
         width: auto; // fixes the problem 1
         border-bottom: none; // fixes the problem 2
      }
      

      【讨论】:

        【解决方案5】:

        在 bootstrap 4 中,在字段集上添加与图例混合的边框要容易得多。您不需要自定义 css 来实现它,可以这样完成:

        <fieldset class="border p-2">
           <legend  class="w-auto">Your Legend</legend>
        </fieldset>
        

        看起来像这样:

        【讨论】:

        • w-auto 在 Bootstrap 4.0 中不可用,但在 4.1+ 中可用
        • 这是这里最简单的解决方案。我会向任何人推荐这个。
        • 对于 bootstrap 5,您还需要添加此样式。传说{浮动:无;填充:继承; }
        【解决方案6】:
        <fieldset  class="border p-2 form-group">
           <legend class="control-label customer-legend pl-1">Your Legend</legend>
        </fieldset>
        

        使用css:

        .customer-legend:before {
            left: 100px;
        }
        

        【讨论】:

          【解决方案7】:

          fieldset.scheduler-border {
            border: 1px groove #ddd !important;
            padding: 0 1.4em 1.4em 1.4em !important;
            margin: 0 0 1.5em 0 !important;
            -webkit-box-shadow: 0px 0px 0px 0px #000;
            box-shadow: 0px 0px 0px 0px #000;
            margin-top: 30px !important;
          }
          
          legend.scheduler-border {
            font-size: 1.2em !important;
            font-weight: bold !important;
            text-align: left !important;
            width: auto;
            padding: 0 10px;
            border-bottom: none;
            margin-top: -15px;
            background-color: white;
            color: black;
          }
          <div class="container">
              <fieldset class="scheduler-border">
                  <legend class="scheduler-border">Technical Ameen</legend>
                  <h2>Your Work Here</h2>
              </fieldset>
          </div>

          如果您找不到在引导程序 5 中使用图例标签的任何适当解决方案

          您可以在图例标签中使用 CSS 属性作为替代解决方案,这是一个内联 CSS 示例。

          style="margin-top:-15px; background-color: white; color: black;"
          

          另外,将这一行添加到 margin-top 的 fieldset 标签中

           style="margin-top: 30px !important;"
          

          我希望这是一个使用 bootstrap 5 获取图例样式的非常简单的解决方案

          【讨论】:

            【解决方案8】:

            在 Bootstrap 5 中,将 modifications 应用到图例的默认样式会导致与使用 Bootstrap 4 获得的渲染不同,然后您必须添加 float-none 作为 &lt;legend&gt; 标签的类:

            <fieldset class="border p-2">
               <legend  class="float-none w-auto">Your Legend</legend>
            </fieldset>
            

            <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"/>
            <form>
            <fieldset class="border p-2">
               <legend  class="float-none w-auto p-2">Your Legend</legend>
               <input type="text">
            </fieldset>
            </form>

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-03-26
              • 2013-01-05
              • 2016-11-07
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-11-06
              相关资源
              最近更新 更多