как выбрать щелкающий ввод в css - PullRequest
3 голосов
/ 04 марта 2020
#registeration_form input:nth-of-type(1):focus ~ #underline1,#registeration_form input:nth-of-type(2):focus  ~ #underline2,#registeration_form input:nth-of-type(3):focus  ~ #underline3,#registeration_form input:nth-of-type(4):focus  ~ #underline4,#registeration_form input:nth-of-type(5):focus  ~ #underline5,#registeration_form input:nth-of-type(6):focus  ~ #underline6{
    transition: 1000ms;
    width: 100%;
}

Есть ли простой способ написать этот код?

1 Ответ

2 голосов
/ 04 марта 2020

Я не совсем уверен, что вы строите, но вот предположение (основанное на наименовании), которое действительно упростит ваш CSS. По сути, я изменяю вещи, чтобы зависеть от разделяемых классов, а не от c id s.

.field .underline {
  width: 0;
  overflow: hidden;
  transition: 100ms;
  position: absolute;
  bottom: 0;
  left: 0;
  height: 1px;
  background-color: black;
}

#registeration_form input:focus + .underline {
 width: 100%; 
}

.field {
  display: inline-block;
  position: relative;
  margin-bottom: .5rem;
}

.field input {
  border: 1px solid #eee;
  padding: .5rem 1rem;
}
<form id="registeration_form">
  <div class="field">
    <input placeholder="Enter Name" aria-label="enter name" type="text" class="input" id="name" name="name">
    <span role="presentation" class="underline"></span>
  </div>

  <div class="field">
    <input placeholder="Enter Username" aria-label="enter username" type="text" class="input" id="username" name="username">
    <span role="presentation" class="underline"></span>
  </div>

  <div class="field">
    <input placeholder="Enter Email" aria-label="enter email" type="text" class="input" id="email" name="email">
    <span role="presentation" class="underline"></span>
  </div>
</form>

jsFiddle

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...