【问题标题】:Add CSS when input is valid - CSS Challenge输入有效时添加 CSS - CSS Challenge
【发布时间】:2021-10-26 15:34:47
【问题描述】:

上下文:我在 netsuite 中工作,它在其输入周围添加跨度,因此我不能使用引导程序浮动标签,因为它针对调用它的 div 之后的直接子输入。

我的目标:当输入聚焦时,浮动标签内的文本会浮动,但是当我在字段内输入内容时,它会下降。当我当前未点击该字段时,我需要保持应用样式。

    <div class="col-md-3">
    <div class="custom_floating mb-3">
        <span>
            <input type="text" class="" id="firstname" placeholder=""> 
        </span>
        <label for="floatingFirstName" class="form-control-placeholder">
            First Name <span style="color: red;">*</span>
        </label>
    </div>
</div>

CSS:

    .custom_floating > span:focus-within + label {
  opacity: 0.65;
  transform: scale(0.85) translateY(-0.5rem) translateX(0.15rem);
}  

感谢任何答案。

【问题讨论】:

  • 似乎没有办法仅通过CSS来实现这一点。您需要使用 Javascript 将新样式添加到标签中。

标签: html css frontend netsuite


【解决方案1】:

input:not(:placeholder-shown) + label{
  color:red;
}
input:placeholder-shown + label{
  border-color: black;
}
 <div class="col-md-3">
    <div class="custom_floating mb-3">
        <span>
            <input type="text" class="" id="firstname" placeholder="name"> 
       
        <label for="floatingFirstName" class="form-control-placeholder">
            First Name <span style="color: red;">*</span>
        </label>
           </span>
    </div>
</div>

输入应该有占位符

【讨论】:

  • 我添加了一个输入,它只是在 JavaScript 中,因为在 netsuite 中你必须这样做。这很好用,你能把标签移到跨度之外吗?感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-15
  • 1970-01-01
  • 2019-09-22
  • 2014-05-12
  • 1970-01-01
  • 2014-07-18
相关资源
最近更新 更多