行内块元素的跨度显示不正常

4
我可以帮您翻译成中文。以下是内容:

我有一个文本框和一个与之关联的验证信息。我想将验证消息放在文本框内,但它只显示在下方。我尝试了不同的选项,但都没有生效。下面是相关代码:

HTML

<div class="col-lg-3 wrap">
  <span>
    <input class="mandatoryText vtooltips form-control textbox validationInput" style="width: 100%; vertical-align: top; border-radius: 4px;" maxlength="100" name="tradeName" type="text">
    <span class="vspan" style="display: inline-block;">Please enter Name</span> 
  </span>
</div>

CSS

input[type=text].vtooltips {
    position: relative;
    display: inline;
}
input[type=text].vtooltips + span.vspan {
    /*position: absolute;*/
    display:none;
    font-size:12px; 
    font-family: Arial; 
    color:white;
    border-radius:3px; 
    background: #DC000C;
    width:50%;
    border: 1px solid #6D6D6D;
    line-height: 0px;
    text-align: center;
    /*visibility: hidden;*/
    border-radius: 4px;
    box-shadow: 2px 2px 0px #AFB1B1;
    margin-left:5px;
    line-height:15px;

}
.validationInput,
.validationInput:focus,
.validationInput:hover {
    background-color: #FFFFE0!important;
    border: 1px solid red!important;
    height: 20px
}
.wrap span:first-child {
    display: inline-block;
    position: relative;
    overflow: hidden;
    width: 100%
}
.wrap span:first-child:after {
    content: '';
    position: absolute;
    top: -5px;
    right: -5px;
    width: 0;
    height: 0;
    border-width: 5px;
    border-color: red;
    border-style: solid;
    transform: rotate(45deg);
    box-shadow: 0 0 0 1px #FFFFE0
}

这里有一个与之相关的演示

期望的输出结果是:

enter image description here

如果需要帮助,请告诉我。谢谢。

3个回答

5
最简单的方法是使用CSS Flexbox。将您的<span>容器从inline-block改为flex,就像这样:
span {
  display: flex;
}

请看下面的代码片段:

/* CSS used here will be applied after bootstrap.css */
input[type=text].vtooltips {
        position: relative;
        display: inline;
        height: 20px;
    }
    .vspan {
        /*position: absolute;*/
        display:none;
        font-size:12px; 
        font-family: Arial; 
        color:white;
        border-radius:3px; 
        background: #DC000C;
        width:50%;
        height: 20px;
        border: 1px solid #6D6D6D;
        line-height: 0px;
        text-align: center;
        /*visibility: hidden;*/
        border-radius: 4px;
        box-shadow: 2px 2px 0px #AFB1B1;
        margin-left:5px;
        line-height:15px;
        
    }
.validationInput,
.validationInput:focus,
.validationInput:hover {
    background-color: #FFFFE0!important;
    border: 1px solid red!important;
    height: 20px
}

.wrap span:first-child {
    display: flex;
    position: relative;
    overflow: hidden;
    width: 100%
}

.wrap span:first-child .input-holder:after {
    content: '';
    position: absolute;
    top: -5px;
    right: -5px;
    width: 0;
    height: 0;
    border-width: 5px;
    border-color: red;
    border-style: solid;
    transform: rotate(45deg);
    box-shadow: 0 0 0 1px #FFFFE0
}

body {
  margin: 30px;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="col-lg-3 wrap">
  <span>
    <span class="input-holder">
    <input type="text" class="mandatoryText vtooltips form-control textbox validationInput" style="width: 100%; vertical-align: top; border-radius: 4px;" maxlength="100" name="tradeName"></span>
    <span class="vspan" style="display: inline-block;">Please enter Name</span> 
  </span>
</div>

希望这可以帮到你!

它在某种程度上确实起作用。但是在我提供的图像中,可以看到文本框的右上角有一个三角形。使用您的解决方案,三角形消失了。我也需要那个三角形。 - Senjuti Mahapatra
@SenjutiMahapatra 我已经更新了我的答案,请现在看一下,这将解决你的问题。只需要进行一个小的结构更改。 - Saurav Rastogi
@SenjutiMahapatra 很高兴为您服务! - Saurav Rastogi

1
请进行以下更改:

请做以下更改:

<div class="col-lg-3 wrap">
  <span>
    <input class="mandatoryText vtooltips form-control textbox validationInput" style="width: 70%; vertical-align: top; border-radius: 4px;" maxlength="100" name="tradeName" type="text">
    <span class="vspan" style="display: inline;">Please enter Name</span> 
  </span>
</div>

请从这里参考Demo

不,它不起作用。你能提供一个可运行的示例吗? - Senjuti Mahapatra
我参考了演示。但是正如您所看到的,右上角的三角形应该在文本框内而不是外面。您能提供相同的内容吗? - Senjuti Mahapatra

1
只需添加以下 css。。
span.vspan{
    margin-top: 5px;
    display: flex;
}
.wrap span:first-child {
       display: flex;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接