Как изменить CSS плавающую метку для каждого входа - PullRequest
0 голосов
/ 07 декабря 2018

В моем проекте я использую некоторый код CSS для создания плавающей метки для ввода формы.Оригинальный код для плавающей метки можно найти здесь для справки.

Однако, поскольку этот код содержит несколько примеров плавающих меток, я извлек скомпилированный CSS для интересующего меня (шар) и создал приведенный ниже пример с моими модификациями.В частности, я добавил значение в поля ввода.

.balloon {
  display: inline-block;
  width: 600px;
  padding: 10px 0 10px 15px;
  font-family: "Open Sans", sans;
  font-weight: 400;
  color: #377D6A;
  background: #efefef;
  border: 0;
  border-radius: 3px;
  outline: 0;
  text-indent: 60px;
  transition: all .3s ease-in-out;
}
.balloon::-webkit-input-placeholder {
  color: #efefef;
  text-indent: 0;
  font-weight: 300;
}
.balloon + label {
  display: inline-block;
  position: absolute;
  top: 8px;
  left: 0;
  bottom: 8px;
  padding: 5px 15px;
  color: #032429;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  text-shadow: 0 1px 0 rgba(19, 74, 70, 0);
  transition: all .3s ease-in-out;
  border-radius: 3px;
  background: rgba(122, 184, 147, 0);
}
.balloon + label:after {
  position: absolute;
  content: "";
  width: 0;
  height: 0;
  top: 100%;
  left: 50%;
  margin-left: -3px;
  border-left: 3px solid transparent;
  border-right: 3px solid transparent;
  border-top: 3px solid rgba(122, 184, 147, 0);
  transition: all .3s ease-in-out;
}

.balloon:focus,
.balloon:active {
  color: #377D6A;
  text-indent: 0;
  background: #fff;
}
.balloon:focus::-webkit-input-placeholder,
.balloon:active::-webkit-input-placeholder {
  color: #aaa;
}
.balloon:focus + label,
.balloon:active + label {
  color: #fff;
  text-shadow: 0 1px 0 rgba(19, 74, 70, 0.4);
  background: #7ab893;
  transform: translateY(-40px);
}
.balloon:focus + label:after,
.balloon:active + label:after {
  border-top: 4px solid #7ab893;
}


@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700,600,300,800);
* {
  box-sizing: border-box;
}

html,
body {
  overflow-x: hidden;
  font-family: "Open Sans", sans-serif;
  font-weight: 300;
  color: #fff;
  background: #efefef;
}

.row {
  max-width: 800px;
  margin: 0 auto;
  padding: 60px 30px;
  background: #032429;
  position: relative;
  z-index: 1;
  text-align: center;
}


.row:before {
  position: absolute;
  content: "";
  display: block;
  top: 0;
  left: -5000px;
  height: 100%;
  width: 15000px;
  z-index: -1;
  background: inherit;
}
.row:first-child {
  padding: 40px 30px;
}

.row span {
  position: relative;
  display: inline-block;
  margin: 30px 10px;
}
<div class="row">
  <span>
    <input class="balloon" id="state" type="text" value="AR" placeholder="Liquid, solid, gaseous..." /><label for="state">State</label>
  </span>
  <span>
    <input class="balloon" id="planet"  value="Earth" type="text" placeholder="Probably Earth" /><label for="planet">Planet</label>
  </span>
  <span>
    <input class="balloon" id="galaxy" type="text" value="This is a test" placeholder="Milky Way?" /><label for="galaxy">Guardians of the Galaxy</label>
  </span>
</div>

У меня возник вопрос, если вы посмотрите на третий вход, как отрегулировать расстояние для метки, чтобы они не перекрывались.Я понимаю, что могу изменить отступ текста в классе выноски, но это изменит интервал для всех входных данных.

Есть ли способ на индивидуальной основе, чтобы я мог настроить интервал для учета различной длины вэтикетки?

Ответы [ 2 ]

0 голосов
/ 07 декабря 2018

Добавьте дополнительный класс к "Gardians" и добавьте стиль, чтобы иметь немного больше.

Затем (и я не могу поверить, что я собираюсь это сказать) используйте !important на активномtext-indent для преодоления дополнительной специфики нового класса.

.balloon {
  display: inline-block;
  width: 600px;
  padding: 10px 0 10px 15px;
  font-family: "Open Sans", sans;
  font-weight: 400;
  color: #377D6A;
  background: #efefef;
  border: 0;
  border-radius: 3px;
  outline: 0;
  text-indent: 60px;
  transition: all .3s ease-in-out;
}

/*Wide Version - can be applied to more elements*/
.balloon.wide {  
  text-indent: 200px;
}

.balloon::-webkit-input-placeholder {
  color: #efefef;
  text-indent: 0;
  font-weight: 300;
}
.balloon + label {
  display: inline-block;
  position: absolute;
  top: 8px;
  left: 0;
  bottom: 8px;
  padding: 5px 15px;
  color: #032429;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  text-shadow: 0 1px 0 rgba(19, 74, 70, 0);
  transition: all .3s ease-in-out;
  border-radius: 3px;
  background: rgba(122, 184, 147, 0);
}
.balloon + label:after {
  position: absolute;
  content: "";
  width: 0;
  height: 0;
  top: 100%;
  left: 50%;
  margin-left: -3px;
  border-left: 3px solid transparent;
  border-right: 3px solid transparent;
  border-top: 3px solid rgba(122, 184, 147, 0);
  transition: all .3s ease-in-out;
}

.balloon:focus,
.balloon:active {
  color: #377D6A;
  /*Note !important*/
  text-indent: 0 !important;
  background: #fff;
}
.balloon:focus::-webkit-input-placeholder,
.balloon:active::-webkit-input-placeholder {
  color: #aaa;
}
.balloon:focus + label,
.balloon:active + label {
  color: #fff;
  text-shadow: 0 1px 0 rgba(19, 74, 70, 0.4);
  background: #7ab893;
  transform: translateY(-40px);
}
.balloon:focus + label:after,
.balloon:active + label:after {
  border-top: 4px solid #7ab893;
}


@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700,600,300,800);
* {
  box-sizing: border-box;
}

html,
body {
  overflow-x: hidden;
  font-family: "Open Sans", sans-serif;
  font-weight: 300;
  color: #fff;
  background: #efefef;
}

.row {
  max-width: 800px;
  margin: 0 auto;
  padding: 60px 30px;
  background: #032429;
  position: relative;
  z-index: 1;
  text-align: center;
}


.row:before {
  position: absolute;
  content: "";
  display: block;
  top: 0;
  left: -5000px;
  height: 100%;
  width: 15000px;
  z-index: -1;
  background: inherit;
}
.row:first-child {
  padding: 40px 30px;
}

.row span {
  position: relative;
  display: inline-block;
  margin: 30px 10px;
}
<div class="row">
  <span>
    <input class="balloon" id="state" type="text" value="AR" placeholder="Liquid, solid, gaseous..." /><label for="state">State</label>
  </span>
  <span>
    <input class="balloon" id="planet"  value="Earth" type="text" placeholder="Probably Earth" /><label for="planet">Planet</label>
  </span>
  <span>
    <input class="balloon wide" id="galaxy" type="text" value="This is a test" placeholder="Milky Way?" /><label for="galaxy">Guardians of the Galaxy</label>
  </span>
</div>

Обычно я избегаю !important, как чума , но в этом случае это имеет смысл.Вы можете избежать этого, используя более конкретные селекторы для вашего активного состояния

.balloon:focus,
.balloon:active,
.balloon.wide:focus,
.balloon.wide:active, {
  color: #377D6A;
  text-indent: 0;
  background: #fff;
}
0 голосов
/ 07 декабря 2018

целевое значение ввода

input[value="This is a test"] {text-indent:170px; }
.balloon {
  display: inline-block;
  width: 600px;
  padding: 10px 0 10px 15px;
  font-family: "Open Sans", sans;
  font-weight: 400;
  color: #377D6A;
  background: #efefef;
  border: 0;
  border-radius: 3px;
  outline: 0;
  text-indent: 60px;
  transition: all .3s ease-in-out;
}
.balloon::-webkit-input-placeholder {
  color: #efefef;
  text-indent: 0;
  font-weight: 300;
}
.balloon + label {
  display: inline-block;
  position: absolute;
  top: 8px;
  left: 0;
  bottom: 8px;
  padding: 5px 15px;
  color: #032429;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  text-shadow: 0 1px 0 rgba(19, 74, 70, 0);
  transition: all .3s ease-in-out;
  border-radius: 3px;
  background: rgba(122, 184, 147, 0);
}
.balloon + label:after {
  position: absolute;
  content: "";
  width: 0;
  height: 0;
  top: 100%;
  left: 50%;
  margin-left: -3px;
  border-left: 3px solid transparent;
  border-right: 3px solid transparent;
  border-top: 3px solid rgba(122, 184, 147, 0);
  transition: all .3s ease-in-out;
}

.balloon:focus,
.balloon:active {
  color: #377D6A;
  text-indent: 0;
  background: #fff;
}
.balloon:focus::-webkit-input-placeholder,
.balloon:active::-webkit-input-placeholder {
  color: #aaa;
}
.balloon:focus + label,
.balloon:active + label {
  color: #fff;
  text-shadow: 0 1px 0 rgba(19, 74, 70, 0.4);
  background: #7ab893;
  transform: translateY(-40px);
}
.balloon:focus + label:after,
.balloon:active + label:after {
  border-top: 4px solid #7ab893;
}


@import url(https://fonts.googleapis.com/css?family=Open+Sans:400,700,600,300,800);
* {
  box-sizing: border-box;
}

html,
body {
  overflow-x: hidden;
  font-family: "Open Sans", sans-serif;
  font-weight: 300;
  color: #fff;
  background: #efefef;
}

.row {
  max-width: 800px;
  margin: 0 auto;
  padding: 60px 30px;
  background: #032429;
  position: relative;
  z-index: 1;
  text-align: center;
}


.row:before {
  position: absolute;
  content: "";
  display: block;
  top: 0;
  left: -5000px;
  height: 100%;
  width: 15000px;
  z-index: -1;
  background: inherit;
}
.row:first-child {
  padding: 40px 30px;
}

.row span {
  position: relative;
  display: inline-block;
  margin: 30px 10px;
}
<div class="row">
  <span>
    <input class="balloon" id="state" type="text" value="AR" placeholder="Liquid, solid, gaseous..." /><label for="state">State</label>
  </span>
  <span>
    <input class="balloon" id="planet"  value="Earth" type="text" placeholder="Probably Earth" /><label for="planet">Planet</label>
  </span>
  <span>
    <input class="balloon" id="galaxy" type="text" value="This is a test" placeholder="Milky Way?" /><label for="galaxy">Guardians of the Galaxy</label>
  </span>
</div>
...