input :: before元素未显示

时间:2018-09-19 03:15:57

标签: html css

input :: before元素未显示。我不确定为什么:

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>

1 个答案:

答案 0 :(得分:0)

我的理解:输入是replaced元素。 “替换”是指用浏览器的默认小部件(在此为文本框)替换内容。 您可以使用包装元素<input/>解决此问题。这是一个例子及其提琴。

HTML

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

CSS

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

请更改值以适合您的样式。

提琴:https://jsfiddle.net/4csrwy0f/7/