Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

html - input styling not applied right

I designed an input and I want the input look this way: https://codepen.io/YanivA5/pen/MWjQGod

but when I remove to the input required it look like that: https://codepen.io/YanivA5/pen/BaLYxdx

input:focus and input:valid applied when page starts

html

<div class="inputContainer">
        <input type="text" id="firstName" required>
        <span class="highlight"></span>
        <span class="bar"></span>
        <label for="">?? ????</label>
        <small>????? ?????? ???</small>
    </div>

styling

.inputContainer{
    position: relative;
    width: 200px;
    margin: 45px 0;
}


.inputContainer input{
    background: none;
    color: rgb(0, 0, 0);
    font-size: 18px;
    padding: 10px 10px 10px 5px;
    display: block;
    width: 100%;
    text-align: right;
    border: none;
    border-radius: 0;
    border-bottom: 1px solid  rgb(218, 218, 218);
}
.inputContainer input:focus {
    outline: none;
  }

  .inputContainer input:focus ~ label,
  .inputContainer input:valid ~ label {
    top: -14px;
    font-size: 12px;
    color:  rgb(37, 129, 235);
  }

  .inputContainer input:focus ~ .bar:before, input:valid ~ .bar:before{
    width: 200px; transition: 0.4s; left: 0;
  }

  .inputContainer label{
    color: rgb(0, 0, 0);
    font-size: 16px;
    font-weight: normal;
    position: absolute;
    pointer-events: none;
    right: 5px;
    top: 10px;
    transition: 0.2s ease all;
  }

  .inputContainer .bar {
    position: relative;
    display: block;
    width: fit-content;
}

.inputContainer .bar::before{
    content: '';
    height: 2px;
    width: 0;
    bottom: 0px;
    position: absolute;
    background: rgb(37, 129, 235);
    transition: 0.3s ease all;
    position: absolute; bottom: 0; 
    left: 100px; 
    width: 0; 
    height: 2px; 
    transition: 0.4s;
    
}

small{
    color: rgb(243, 11, 11);
    width: 200px;
    margin-right: 5px;
    float: right;
    text-align: right;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In the HTML of code #1 you have the required atrribute but in the code #2 you don't have it. It works without the required atrribute.

Image of the diff checker:
Image of the diff checker


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...