3

I want to use Tobias Ahlin's script to style up my input-box. But while it's perfectly working on paragraphs, on inputs doesn't. Also, the ::before doesn't show up in the spectator.

Here's my code:

.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>

I'm using it in an Angular 5 application, but I don't think it's relevant right now.

Community
  • 1
  • 1
Drobesz
  • 384
  • 1
  • 7
  • 17

3 Answers3

2

The problem is that input and other form elements don't render :before and :after pseudo elements.

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

So wrapping the input in a span would make this to work:

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

CSS ... please note for .edit span::before bottom: -2px difference than your code.

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);
}
August
  • 2,045
  • 10
  • 23
2

Here's a way to do it.

Just wrap the input element with a span tag and style that one instead.

$(document).ready(function() {
  $(document).on("focus blur", "input", function() {
    $("span").toggleClass("underline");
  });
});
.edit {
  position: relative;
  display: inline-block;
}

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

.edit span::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 span.underline::before,
.edit span.underline::before {
  visibility: visible;
  transform: scaleX(1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="edit">
  <span>
    <input type="text" maxlength="15" value="Some content here...">
  </span>
</div>
dokgu
  • 4,957
  • 3
  • 39
  • 77
  • 1
    Thank you, this is exactly what I want. But I've improved it. I'll post as an answer, works as the same, but doesn't use JS. – Drobesz Apr 26 '18 at 18:27
2

Thank you, @uom-pgregprio for the solution, this works as the same as yours, but doesn't require JS. (For anybody who's stuck with this problem.)

.edit {
  position: relative;
  display: inline-block;
}

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

.edit span::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 span:focus-within::before {
  visibility: visible;
  transform: scaleX(1);
}
<div class="edit">
  <span>
    <input type="text" maxlength="15" value="Some content here...">
  </span>
</div>
Drobesz
  • 384
  • 1
  • 7
  • 17
  • Oh cool, I wasn't aware that something like `:focus-within` exists. I learned something from you too. That deserves a . – dokgu Apr 27 '18 at 15:41
  • As an additional info just in case you really don't want a JS approach. [`:focus-within`](https://caniuse.com/#feat=css-focus-within) still has a few red boxes I see in this page. If you're worried about cross-browser compatibility you might want to take this into account. – dokgu May 15 '18 at 16:22