css - Bootstrap 中的 HTML 需要验证消息样式问题

标签 css html twitter-bootstrap

在我的带有日历图标的 Bootstrap 表单字段中,它是必需的。但是,当显示错误消息时,它会像下图一样一团糟。 enter image description here

HTML代码是

<div class="input-group">
     <div class="input-group-addon"><i class="fa fa-calendar"></i></div>
     <input type="text" value="" class="form-control error" name="from" id="from" required">
 </div>

下面的 Bootstrap CSS

 .input-group-addon, .input-group-btn, .input-group .form-control {
        display: table-cell;
    }
.input-group-addon, .input-group-btn {
    vertical-align: middle;
    white-space: nowrap;
    width: 1%;
}
.input-group-addon {
    background-color: #eeeeee;
    border: 1px solid #cacaca;
    border-radius: 3px;
    color: #333333;
    font-size: 14px;
    font-weight: normal;
    line-height: 1;
    padding: 6px 12px;
    text-align: center;
}
.input-group .form-control {
    float: left;
    margin-bottom: 0;
    position: relative;
    width: 100%;
    z-index: 2;
}

HTML5 要求错误

<label id="from-error" class="error" for="from">This field is required.</label>

因此发生错误时HTML代码如下所示

<div class="input-group">
     <div class="input-group-addon"><i class="fa fa-calendar"></i></div>
     <input type="text" value="" class="form-control error" name="from" id="from" required">
<label id="from-error" class="error" for="from">This field is required.</label>
 </div>

那么如何解决这个问题并在衬里字段中设置日历图标?

最佳答案

因为 input-group 中的所有元素都是 display: table-cell ,你必须提供 display: table-rowlabel 用于它出现在新行中(在与表格不同的行中)。

编辑

如果您希望错误正好出现在输入下方并紧接在日历图标之后,我建议您改用 position: absolute

检查编辑后的代码片段:

.input-group-addon,
.input-group-btn,
.input-group .form-control {
  display: table-cell;
}

.input-group-addon,
.input-group-btn {
  vertical-align: middle;
  white-space: nowrap;
  width: 1%;
}

.input-group-addon {
  background-color: #eeeeee;
  border: 1px solid #cacaca;
  border-radius: 3px;
  color: #333333;
  font-size: 14px;
  font-weight: normal;
  line-height: 1;
  padding: 6px 12px;
  text-align: center;
}

.input-group .form-control {
  float: left;
  margin-bottom: 0;
  position: relative;
  width: 100%;
  z-index: 2;
}

/* #from-error {
  display: table-row;
} */

#from-error {
    position: absolute;
    bottom: -32px;
    left: 40px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<div class="input-group">
  <div class="input-group-addon"><i class="fa fa-calendar"></i></div>
  <input type="text" value="" class="form-control error" name="from" id="from" required>
  <label id="from-error" class="error" for="from">This field is required.</label>
</div>

关于css - Bootstrap 中的 HTML 需要验证消息样式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43138564/

相关文章:

html - 是否可以仅通过修改 HTML 和 CSS 代码将文件上传到网站?

javascript - 如何在div上创建两个滚动条作为顶部和底部?

javascript - onclick 触发调整大小,highcharts

javascript - 轮播缩略图仍然不会自动更新

html - 如果您使用的是 WordPress 循环,如何修复 Bootstrap 中的不均匀行?

jquery - Chrome 无法处理大屏幕尺寸的动画

html - 组 'calc()' CSS 属性;使用 margin 速记?

javascript - cordova window.location.href 行为错误

html - 仅使用 css 交换 div 顺序

html - 通过下拉菜单链接到 HTML 中的其他页面