Bookmark

Pure CSS Float Label

Code collected from the Internet and saved here so I do not have to search for it again later 😊

DEMO:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<fieldset>
  <legend>Sign up</legend>

  <div class="input-group">
    <div class="has-float-label">
      <input id="first" type="text" placeholder="Name"/>
      <label for="first">First</label>
    </div>

    <div class="has-float-label">
      <input id="last" type="text" placeholder="Surname"/>
      <label for="last">Last</label>
    </div>
  </div>

  <label class="has-float-label">
    <input type="email" placeholder="[email protected]"/>
    <span>Email</span>
  </label>

  <label class="has-float-label">
    <input type="password" placeholder="••••••••"/>
    <span>Password</span>
  </label>

  <button>Sign up</button>
</fieldset>
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* Powered by: https://github.com/tonystar/float-label-css
 */

/* Re-usable mixins
 * https://github.com/tonystar/float-label-css/blob/master/scss/_mixins.scss
 */
@mixin float-label-container {
  display: block;
  position: relative;
}

@mixin float-label {
  label, > span {
    position: absolute;
    left: 0;
    top: 0;
    cursor: text;
    font-size: 75%;
    opacity: 1;
    transition: all .2s;
    @content;
  }
}

@mixin float-label-input {
  &::placeholder {
    opacity: 1;
    transition: all .2s;
  }
  &:placeholder-shown:not(:focus)::placeholder {
      opacity: 0;
  }
}

@mixin float-label-scaled {
  &:placeholder-shown:not(:focus) + * {
      font-size: 150%;
      opacity: .5;
      @content;
  }
}

/* Default styling
 * https://github.com/tonystar/float-label-css/blob/master/scss/float-label.scss
 */
.has-float-label {
  @include float-label-container;

  @include float-label;

  select {
    -webkit-appearance: none;
       -moz-appearance: none;
            appearance: none;
  }

  input, select {
    @include float-label-input;
    font-size: inherit;
    padding-top: 1em;
    margin-bottom: 2px;
    border: 0;
    border-radius: 0;
    border-bottom: 2px solid rgba(0,0,0,.1);

    @include float-label-scaled {
      top: .25em;
    }

    &:focus {
      outline: none;
      border-color: rgba(0,0,0,.5);
    }
  }

  select {
    padding-right: 1em;
    background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 4 5'%3E%3Cpath fill='%23333' d='M2 0L0 2h4zm0 5L0 3h4z'/%3E%3C/svg%3E") no-repeat right .5em bottom .25em;
    background-size: 8px 10px;
  }
}

// Custom styling

* {
  box-sizing: border-box;
}
body {
  position: relative;
  max-width: 20rem;
  background: #eee;
  margin: 2rem auto;
}
fieldset {
  padding: 1.5rem;
  background: white;
}
input, select {
  width: 100%;
  margin-bottom: .5rem;
}
button {
  background: #333;
  color: white;
  border: 0;
  padding: .5em 1em;
  margin-top: 1rem;
}
.input-group {
  display: table;
  width: 100%;
}
.input-group > * {
  display: table-cell;
  width: 50%;
}
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* Powered by: https://github.com/tonystar/float-label-css
 */
/* Re-usable mixins
 * https://github.com/tonystar/float-label-css/blob/master/scss/_mixins.scss
 */
/* Default styling
 * https://github.com/tonystar/float-label-css/blob/master/scss/float-label.scss
 */
.has-float-label {
  display: block;
  position: relative;
}
.has-float-label label, .has-float-label > span {
  position: absolute;
  left: 0;
  top: 0;
  cursor: text;
  font-size: 75%;
  opacity: 1;
  transition: all 0.2s;
}
.has-float-label select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
}
.has-float-label input, .has-float-label select {
  font-size: inherit;
  padding-top: 1em;
  margin-bottom: 2px;
  border: 0;
  border-radius: 0;
  border-bottom: 2px solid rgba(0, 0, 0, 0.1);
}
.has-float-label input::-moz-placeholder, .has-float-label select::-moz-placeholder {
  opacity: 1;
  -moz-transition: all 0.2s;
  transition: all 0.2s;
}
.has-float-label input:-ms-input-placeholder, .has-float-label select:-ms-input-placeholder {
  opacity: 1;
  -ms-transition: all 0.2s;
  transition: all 0.2s;
}
.has-float-label input::placeholder, .has-float-label select::placeholder {
  opacity: 1;
  transition: all 0.2s;
}
.has-float-label input:placeholder-shown:not(:focus)::-moz-placeholder, .has-float-label select:placeholder-shown:not(:focus)::-moz-placeholder {
  opacity: 0;
}
.has-float-label input:placeholder-shown:not(:focus):-ms-input-placeholder, .has-float-label select:placeholder-shown:not(:focus):-ms-input-placeholder {
  opacity: 0;
}
.has-float-label input:-moz-placeholder-shown:not(:focus)::placeholder, .has-float-label select:-moz-placeholder-shown:not(:focus)::placeholder {
  opacity: 0;
}
.has-float-label input:-ms-input-placeholder:not(:focus)::placeholder, .has-float-label select:-ms-input-placeholder:not(:focus)::placeholder {
  opacity: 0;
}
.has-float-label input:placeholder-shown:not(:focus)::placeholder, .has-float-label select:placeholder-shown:not(:focus)::placeholder {
  opacity: 0;
}
.has-float-label input:-moz-placeholder-shown:not(:focus) + *, .has-float-label select:-moz-placeholder-shown:not(:focus) + * {
  font-size: 150%;
  opacity: 0.5;
  top: 0.25em;
}
.has-float-label input:-ms-input-placeholder:not(:focus) + *, .has-float-label select:-ms-input-placeholder:not(:focus) + * {
  font-size: 150%;
  opacity: 0.5;
  top: 0.25em;
}
.has-float-label input:placeholder-shown:not(:focus) + *, .has-float-label select:placeholder-shown:not(:focus) + * {
  font-size: 150%;
  opacity: 0.5;
  top: 0.25em;
}
.has-float-label input:focus, .has-float-label select:focus {
  outline: none;
  border-color: rgba(0, 0, 0, 0.5);
}
.has-float-label select {
  padding-right: 1em;
  background: url("data:image/svg+xml;charset=utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 4 5'%3E%3Cpath fill='%23333' d='M2 0L0 2h4zm0 5L0 3h4z'/%3E%3C/svg%3E") no-repeat right 0.5em bottom 0.25em;
  background-size: 8px 10px;
}

* {
  box-sizing: border-box;
}

body {
  position: relative;
  max-width: 20rem;
  background: #eee;
  margin: 2rem auto;
}

fieldset {
  padding: 1.5rem;
  background: white;
}

input, select {
  width: 100%;
  margin-bottom: 0.5rem;
}

button {
  background: #333;
  color: white;
  border: 0;
  padding: 0.5em 1em;
  margin-top: 1rem;
}

.input-group {
  display: table;
  width: 100%;
}

.input-group > * {
  display: table-cell;
  width: 50%;
}

Source: github://tonystar/float-label-css

0 Bình luận

Góp Ý / Bình Luận / Đánh giá