html - 我的自动下划线脚本有什么问题?

标签 html css

我想使用 Tobias Ahlin's script设计我的输入框。但是,虽然它在段落上工作得很好,但在输入上却没有。此外,::before 不会出现在旁观者中。

这是我的代码:

.edit input {
    text-decoration: none;
    outline: none;
    border: none;
    position: relative;
    font-size: 1.125rem;
}

.edit input::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 2px;
    bottom: 0;
    left: 0;
    background-color: #000;
    visibility: hidden;
    transform: scaleX(0);
    transition: all 0.15s ease-in-out 0s;
}
.edit input:active::before, .edit input:focus::before {
    visibility: visible;
    transform: scaleX(1);
}
<div class="edit">
  <input type="text" maxlength="15" value="Some content here...">
</div>

我在 Angular 5 应用程序中使用它,但我认为它现在不相关。

最佳答案

问题是 input 和其他表单元素不呈现 :before:after 伪元素。

Authors specify the style and location of generated content with the :before and :after pseudo-elements. As their names indicate, the :before and :after pseudo-elements specify the location of content before and after an element's document tree content. The 'content' property, in conjunction with these pseudo-elements, specifies what is inserted.

W3 Specs

因此将 input 包装在 span 中将使它起作用:

<div class="edit">
  <span><input type="text" maxlength="15" value="Some content here..."></span>
</div>

CSS ...请注意 .edit span::before bottom: -2px 与您的代码不同。

span {
  position: relative;
}

.edit input {
    text-decoration: none;
    outline: none;
    border: none;
    position: relative;
    font-size: 1.125rem;
}

.edit span::before {
    content: "";
    position: absolute;
    width: 100%;
    height: 2px;
    bottom: -2px;
    left: 0;
    background-color: #000;
    visibility: hidden;
    transform: scaleX(0);
    transition: all 0.15s ease-in-out 0s;
}
.edit span:hover::before {
    visibility: visible;
    transform: scaleX(1);
}

关于html - 我的自动下划线脚本有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50048955/

相关文章:

javascript - serializeArray 找不到某些输入

html - 如何使一个元素在另一个元素和容器边缘之间居中

html - 将简单的菜单插入到 css 中以用作网页标题

jquery - 如何在激活弹出模式时停止主滚动条工作,这也需要一个滚动条

html - index.php 中的 Yii2 全 Angular 布局

html - 为什么我的标题标签没有出现在使用 Ruby on Rails 的搜索结果中?

css - 如何水平居中垂直旋转的文本?

CSS - linter 与美化器不一致

javascript - 仅在一台机器上出现 HTML 对齐问题(均为 IE8)

php - 单选按钮阵列 - 需要帮助