【问题标题】:Strange behaviour when absolute positioning span around an input绝对定位跨越输入时的奇怪行为
【发布时间】:2015-08-30 19:09:31
【问题描述】:

我正在尝试为我的输入框创建验证错误消息,但我遇到了奇怪的行为以及 Chrome 和其他浏览器之间的差异。

我需要将错误标签设置为显示在输入旁边,并且它应该显示在其他元素上。 我的方法在 FF 中运行良好,但在 chrome 中不行。

问题在于,在 Chrome 中,错误消息位于输入框下方。

示例: http://jsfiddle.net/ewuq44xg/3/

调整 html 预览区域的大小以查看效果。

.editor-group {
    display: inline-block;
    vertical-align: top;
    margin-left: 5px;
    margin-right: 10px;
}
.editor-label {
    min-width: 120px;
    display: inline-block;
    vertical-align: middle;
}
.editor-field {
    min-width: 150px;
    display: inline-block;
    margin: 3px 0;
    vertical-align: middle;
}

.field-validation-error {
    background-repeat: repeat-x;
    min-width: 20px;
    position: absolute !important;
    text-align: center;
    z-index: 1000;
    outline: 0 none;
    background-position: center center;
    background-color: #FFF4C9;
    border: solid 1px #FFE79E;
    color: #635145;
    margin-right: 6px;
    display: inline-block;
    padding: 2px 5px 1px 6px;
    border-radius: 4px 4px 4px 4px;
    box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.3);
    font-weight: normal;
    font-size: .85em;
}
<div class="editor-group">
    <div class="editor-label editor-required">
        <label  for="NameOfReceiptNo1">Name</label>
    </div>
    <div class="editor-field">
        <input type="text" value="">
        <span class="field-validation-error">
            <span class="k-icon k-warning"></span>
            Name is required
        </span>
    </div>
</div>

【问题讨论】:

    标签: html css google-chrome firefox css-position


    【解决方案1】:

    将float: left添加到.editor-field input和float: right到.field-validation-error,您也可以从.field-validation-error中删除display: inline-block

    Fiddle

    【讨论】:

      【解决方案2】:

      当您使用position:absolute 时,最好指定元素应该存在的确切位置。 在您的示例中,我将right: -116px; top: 0; CSS 添加到.field-validation-error 和position:relative 到.editor-group field-validation-error 的第一个父级,所以现在field-validation-error 将知道留在editor-group 的右侧

      plunker

      .editor-field {
       min-width: 150px;
       display: inline-block;
       margin: 3px 0;
       vertical-align: middle;
       position: relative;
      }
      
      .field-validation-error {
       background-repeat: repeat-x;
       min-width: 20px;
       position: absolute !important;
       text-align: center;
       z-index: 1000;
       outline: 0 none;
       background-position: center center;
       background-color: #FFF4C9;
       border: solid 1px #FFE79E;
       color: #635145;
       margin-right: 6px;
       display: inline-block;
       padding: 2px 5px 1px 6px;
       border-radius: 4px 4px 4px 4px;
       box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.3);
       font-weight: normal;
       font-size: .85em;
       right: -116px; top: 0;
       }
      

      【讨论】:

      • 我认为很明显错误消息是可变的,并且会从一个输入变为另一个,因此我无法像您在right 中那样指定确切的数字。
      • 所以改用left。你知道editor-filed的宽度
      • left 与 white-space:nowrap 的组合看起来不错,但仍不如 FF 结果。
      • 而editor-filed 的宽度并不总是知道的,因为它有min-width。此解决方案并非无处不在。
      • 或者如果您不需要使用position abdolute,您可以将其删除并将float:left 添加到.field-validation-error 和.editor-field input,这样就可以彼此相伴了,
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      • 2013-04-23
      • 1970-01-01
      • 2010-10-28
      • 2013-07-01
      • 2022-11-30
      • 1970-01-01
      相关资源
      最近更新 更多