0

input::before element not showing. I'm not sure why:

input::before {
  content: "this is before";
  position: absolute;
}

body {
  background-color: Royalblue; /*#f0f0f0;*/
  margin: 0px;
}

form {
  position: relative;  
  top: 90px;
  margin: 0 auto;
  width: 280px;
  height: 340px; 
  border: 1px solid #B0C4DE;
  background: royalblue;   
  border-radius: 0px;
  border-radius: 10px 10px 10px 10px;
}

/* Main EFFECT ================================ */


input {
  position: relative;
  top: 0px;
  left: 0px;   
  font-family: 'Montserrat', sans-serif;
  border: 0; 
  border-bottom: 1px solid black;
  background: transparent;
  font-size: 15px;                     
  height: 25px; 
  width: 180px;
  outline: 0; 
  z-index: 1; 
  color: black;
}


span {
  position: absolute;
  top: 7px;
  left: 0px;
  font-family: 'Montserrat', sans-serif;
  font-size: 13px;
/*  z-index: 1; */
  color: white; 
  transition: top .5s ease, font-size .5s ease;     
}


input::before {
  content: "this is before";
  position: absolute;
}

.child {
    position: relative;
    width: 65%;
    top: 30px;
    margin: 0 auto;
/*    margin-bottom: 30px;*/
}
<body>
  <form class="parent">     
    <div class="child">
      <input type="text" id="username" required /> 
        <span>Username</span>
    </div>
   </form>     
</body>
Ferrmolina
  • 2,737
  • 2
  • 30
  • 46
yalpsid
  • 235
  • 1
  • 13
  • 2
    Pseudo element can only be rendered in container, please read this: https://stackoverflow.com/questions/2587669/can-i-use-a-before-or-after-pseudo-element-on-an-input-field/14019758 – Chaska Sep 19 '18 at 03:20

1 Answers1

1

My understanding: input is a replaced element. By replaced I mean contents are replaced by the browser's default widget (here text box). You can workaround this problem by using an wrapping element <input/>. Here is an example and its fiddle.

HTML

<span class="input-container">
    <input type="text" id="username" required /> 
</span>

CSS

span.input-container:before {
  content: "|";
  border: 1px solid blue;
}

Please change values to suit your styles.

Fiddle: https://jsfiddle.net/4csrwy0f/7/

trk
  • 2,106
  • 14
  • 20
  • I think its also well summarized in the SO link share by @Chaska in the question's comments. – trk Sep 19 '18 at 03:46