Html 将 Fieldset Legend 与 bootstrap 结合使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16852484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 08:57:28  来源:igfitidea点击:

Use Fieldset Legend with bootstrap

htmlcssformstwitter-bootstrap

提问by Shiju K Babu

I'm using Bootstrap for my JSPpage.

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

I want to use <fieldset>and <legend>for my form. This is my code.

我想使用<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 is

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;
}

I am getting output like this enter image description here

我得到这样的输出 在此处输入图片说明

I want output in the following way

我想以下列方式输出

enter image description here

在此处输入图片说明

I tried adding

我尝试添加

border:none;
width:100px;

to legend.scheduler-borderin CSS. And I'm getting the expected output. But the problem is I would like to add another <fieldset>for another fields. That time the width of text in legend is a problem as it is lengthier than than 100px.

legend.scheduler-border在CSS。我得到了预期的输出。但问题是我想<fieldset>为另一个领域添加另一个。那个时候图例中文本的宽度是一个问题,因为它比100px.

So what shall I do to get output like I have mentioned? (Without striking the legend text)

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

回答by James Donnelly

That's because Bootstrap by default sets the width of the legendelement to 100%. You can fix this by changing your legend.scheduler-borderto also use:

这是因为 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.

JSFiddle 示例

You'll also need to ensure your custom stylesheet is being added afterBootstrap to prevent Bootstrap overriding your styling - although your styles here should have higher specificity.

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

You may also want to add margin-bottom:0;to it as well to reduce the gap between the legend and the divider.

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

回答by Joe Audette

In bootstrap 4 it is much easier to have a border on the fieldset that blends with the legend. You don't need custom css to achieve it, it can be done like this:

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

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

which looks like this: bootstrap 4 fieldset and legend

看起来像这样: bootstrap 4 字段集和图例

回答by Mohammad Aghayari

I had this problem and I solved with this way:

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

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;
}

回答by Jajikanth pydimarla

I had a different approach , used bootstrap panel to show it little more rich. Just to help someone and improve the answer.

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

.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>

This will give below look. enter image description here

这将给出以下外观。 在此处输入图片说明

Note: We need to change the styles in order to use different header size.

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

回答by Manoj Shrestha

Just wanted to summarize all the correct answers above in short. Because I had to spend lot of time to figure out which answer resolves the issue and what's going on behind the scenes.

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

There seems to be two problems of fieldset with bootstrap:

fieldset with bootstrap 好像有两个问题:

  1. The bootstrapsets the width to the legendas 100%. That is why it overlays the top border of the fieldset.
  2. There's a bottom borderfor the legend.
  1. bootstrap设置宽度到legend为100%。这就是为什么它覆盖fieldset.
  2. 有一个bottom borderlegend.

So, all we need to fix this is set the legend width to auto as follows:

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

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