지원이 중단된 브라우저를 사용하고 있습니다.
브라우저 업그레이드를 통해 최적화된 화면을 볼 수 있습니다.

Home form - floating labels
Post
Cancel

form - floating labels

view

html

test page

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<div class="c-form">
    <!-- 기본 -->
    <div class="c-form__field">
        <label class="c-form__label" for="form-1">default</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-1">
        </div>
    </div>

    <!-- has-focus -->
    <div class="c-form__field has-focus">
        <label class="c-form__label" for="form-2">has-focus</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-2" value="has-focus">
        </div>
    </div>

    <!-- has-value -->
    <div class="c-form__field has-value">
        <label class="c-form__label" for="form-3">has-value</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-3" value="has-value">
        </div>
    </div>

    <!-- has-error -->
    <div class="c-form__field has-error has-value">
        <label class="c-form__label" for="form-4">has-error</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-4" value="has-error">
        </div>
    </div>

    <!-- is-disabled -->
    <div class="c-form__field is-disabled">
        <label class="c-form__label" for="form-5">is-disabled</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-5" value="is-disabled" disabled>
        </div>
    </div>

    <!-- 에러메시지 -->
    <div class="c-form__field has-focus has-error">
        <label class="c-form__label" for="form-5">에러메세지</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-5" aria-invalid="true" aria-describedby="error-message" value="에러">
        </div>
    </div>
    <p class="message-error" id="error-message">에러메세지가 출력됩니다.</p>

    <!-- 기본 + 필수입력 -->
    <div class="c-form__field">
        <label class="c-form__label" for="form-6">이름 <span class="sr-only">필수입력</span><span class="require" aria-hidden="true">*</span></label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-6" placeholder="이름 입력">
        </div>
    </div>
    <div class="c-form__field has-focus">
        <label class="c-form__label" for="form-6">이름 <span class="sr-only">필수입력</span><span class="require" aria-hidden="true">*</span></label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-6" placeholder="이름 입력">
        </div>
    </div>

    <!-- password -->
    <div class="c-form__field">
        <label class="c-form__label" for="form-13">password</label>
        <div class="c-form__row">
            <input type="password" class="o-input o-input--text" id="form-13" placeholder="8자~15자 영문/숫자/특수기호조합" maxlength="15">
        </div>
    </div>
    <div class="c-form__field has-focus">
        <label class="c-form__label" for="form-14">password</label>
        <div class="c-form__row">
            <input type="password" class="o-input o-input--text" id="form-14" placeholder="8자~15자 영문/숫자/특수기호조합" maxlength="15">
        </div>
    </div>

    <!-- 기본 + 텍스트 -->
    <div class="c-form__field">
        <label class="c-form__label" for="form-7">금액</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-7" placeholder="금액 입력" pattern="[0-9]*">
            <span class="c-form__text"></span>
        </div>
    </div>
    <div class="c-form__field has-focus">
        <label class="c-form__label" for="form-8">금액</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-8" placeholder="금액 입력" pattern="[0-9]*">
            <span class="c-form__text"></span>
        </div>
    </div>

    <!-- date -->
    <div class="c-form__field">
        <label class="c-form__label" for="form-19">date</label>
        <div class="c-form__row c-form__row--gutter">
            <input type="date" class="o-input o-input--date" title="시작일" id="form-19">
            <input type="date" class="o-input o-input--date" title="종료일">
        </div>
    </div>
    <div class="c-form__field has-focus">
        <label class="c-form__label" for="form-20">date</label>
        <div class="c-form__row c-form__row--gutter">
            <input type="date" class="o-input o-input--date" title="시작일" id="form-20">
            <input type="date" class="o-input o-input--date" title="종료일">
        </div>
    </div>

    <!-- textarea -->
    <div class="c-form__field">
        <label class="c-form__label" for="form-21">textarea</label>
        <div class="c-form__row">
            <textarea class="o-input o-input--textarea c-form__textarea" id="form-21" placeholder="댓글을 입력하세요." title="댓글" cols="5" rows="10" maxlength="300"></textarea>
        </div>
    </div>
    <div class="c-form__field has-focus">
        <label class="c-form__label" for="form-22">textarea</label>
        <div class="c-form__row">
            <textarea class="o-input o-input--textarea c-form__textarea" id="form-22" placeholder="댓글을 입력하세요." title="댓글" cols="5" rows="10" maxlength="300"></textarea>
        </div>
    </div>

    <!-- 한줄에 들어가는 요소가 한개 이상인 경우 -->
    <div class="c-form__field">
        <label class="c-form__label" for="form-9">생년월일</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-9" readonly value="1988년" title="년">
            <input type="text" class="o-input o-input--text" readonly value="09월" title="월">
            <input type="text" class="o-input o-input--text" readonly value="24일" title="일">
        </div>
    </div>
    <div class="c-form__field has-focus">
        <label class="c-form__label" for="form-10">생년월일</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-10" readonly value="1988년" title="년">
            <input type="text" class="o-input o-input--text" readonly value="09월" title="월">
            <input type="text" class="o-input o-input--text" readonly value="24일" title="일">
        </div>
    </div>

    <!-- 여러줄인 경우 + 한줄에 들어가는 요소가 한개 이상인 경우 (row 태그가 추가됨) -->
    <div class="c-form__field">
        <label class="c-form__label" for="form-11">이메일</label>
        <div class="c-form__row">
            <select class="o-input o-input--select" id="form-11" title="도메인">
                <option>직접입력</option>
                <option>naver.com</option>
            </select>
        </div>
        <!-- 도메인 선택 시, 활성화시 display: none 제거 -->
        <div class="c-form__row" style="display: none">
            <input type="email" class="o-input o-input--text" title="이메일 아이디">
            <span class="c-form__text">@gmail.com</span>
        </div>
        <!-- 직접입력 선택 시, 활성화시 display: none 제거 -->
        <div class="c-form__row" style="display: none">
            <input type="email" class="o-input o-input--text" title="이메일 주소">
        </div>
    </div>
    <div class="c-form__field has-focus">
        <label class="c-form__label" for="form-12">이메일</label>
        <div class="c-form__row">
            <select class="o-input o-input--select" id="form-12" title="도메인">
                <option>직접입력</option>
                <option>naver.com</option>
            </select>
        </div>
        <!-- 도메인 선택 시, 활성화시 display: none 제거 -->
        <div class="c-form__row">
            <input type="email" class="o-input o-input--text" title="이메일 아이디" placeholder="메일ID 입력">
            <span class="c-form__text">@gmail.com</span>
        </div>
        <!-- 직접입력 선택 시, 활성화시 display: none 제거 -->
        <div class="c-form__row">
            <input type="email" class="o-input o-input--text" title="이메일 주소" placeholder="메일주소 입력">
        </div>
    </div>

    <!-- input + button  -->
    <div class="c-form__field">
        <label class="c-form__label" for="form-15">아이디</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-15" placeholder="아이디 입력">
            <button type="button" class="button button--gray button--1x c-form__button">중복확인</button>
        </div>
    </div>
    <div class="c-form__field has-focus">
        <label class="c-form__label" for="form-16">아이디</label>
        <div class="c-form__row">
            <input type="text" class="o-input o-input--text" id="form-16" placeholder="아이디 입력">
            <button type="button" class="button button--gray button--1x c-form__button">중복확인</button>
        </div>
    </div>

    <!-- select + input -->
    <div class="c-form__field">
        <label class="c-form__label" for="form-17">휴대폰 번호</label>
        <div class="c-form__row">
           <select class="o-input o-input--select" id="form-17" title="통신사">
                <option>통신사 선택</option>
                <option>SKT</option>
                <option>KT</option>
                <option>U+</option>
            </select>
           <input type="tel"  class="o-input o-input--text" title="휴대폰 번호" maxlength="11" placeholder="'-' 없이 입력" pattern="[0-9]*">
        </div>
    </div>
    <div class="c-form__field has-focus">
        <label class="c-form__label" for="form-18">휴대폰 번호</label>
        <div class="c-form__row">
           <select class="o-input o-input--select" id="form-18" title="통신사">
                <option>통신사 선택</option>
                <option>SKT</option>
                <option>KT</option>
                <option>U+</option>
            </select>
          <input type="tel"  class="o-input o-input--text" title="휴대폰 번호" maxlength="11" placeholder="'-' 없이 입력" pattern="[0-9]*">
        </div>
    </div>
</div>

script

  • c-form__field 에 상태에 따라 클래스 추가 필요
    • has-focus : 내부에 포커스가 들어가 있는 경우
    • has-value : value 값이 있는 경우
    • has-error : 에러가 있는 경우
    • is-disabled : disabled 상태인 경우

기타

  • script로 .has-focus 클래스를 추가하는 방법 대신 :focus-within css 속성을 사용하는 방법이 있음
  • 관련 포스팅 : :focus-within

css

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*---------------------------------------------
  Form
 ----------------------------------------------*/
.o-input {
  $root : ".o-input";

  border: 0;
  border-radius: 0;
  background-color: transparent;
  vertical-align: middle;
  color: #222;
  -webkit-appearance: none;
  appearance: none;
  -webkit-transition: border 200ms ease-in-out;
  transition: border 200ms ease-in-out;

  &::-webkit-input-placeholder,
  &:-ms-input-placeholder,
  &::-moz-placeholder {
    color: #767676;
  }

  &::-ms-clear {
    display: none;
  }

  &:-webkit-autofill,
  &:-webkit-autofill:focus {
    border-color: #d8d8d8;
    -webkit-text-fill-color: #000;
    -webkit-box-shadow: 0 0 0 1000px #fff inset;
    transition: background-color 5000s ease-in-out 0s;
  }

  &--text,
  &--date {
    display: inline-block;
    width: 100%;
    height: 48px;
    padding: 0 20px;
    border: 1px solid #d8d8d8;
    border-radius: 6px;
    background-color: #fff;
    font-weight: 600;
    font-size: 17px;
    text-align: left;

    &[aria-placeholder] {
      color: #666;
    }

    &[readonly],
    &:disabled {
      background-color: #f8f8f8;
      cursor: default;
    }

    &:focus {
      border-color: #0076c0;
    }
  }

  &--text {
    &::-webkit-search-cancel-button {
      display: none;
    }

    &[type="number"]::-webkit-inner-spin-button {
      display: none;
    }
  }

  &--textarea {
    display: block;
    width: 100%;
    height: 132px;
    padding: 18px 20px;
    border: 1px solid #d8d8d8;
    border-radius: 6px;
    background-color: #f8f8f8;
    font-weight: 600;
    font-size: 17px;
    line-height: 21px;
    color : #666;
    resize: none;
  }

  &--select {
    display: inline-block;
    position: relative;
    overflow: hidden;
    width: 100%;
    height: 48px;
    padding: 0 20px;
    border: 1px solid #d8d8d8;
    border-radius: 6px;
    background: #fff url("../img/com/bg_select.png") no-repeat right 20px top 50%;
    background-size: 10px auto;
    font-size: 17px;
    font-weight: 600;
    color: #222;
    text-align: left;
    text-overflow: ellipsis;
    white-space: nowrap;

    &:disabled,
    &.is-disabled {
      background-color: #f8f8f8;
      cursor: default;
    }

    &:focus {
      border-color: #0076c0;
    }

    &.is-disabled:focus {
      border-color: #ccc;
    }
  }
}

.c-form {
  $root : ".c-form";

  margin-top: 25px;

  &__field {
    position: relative;
    padding: 0 20px;
    background-color: #fff;
    border: 1px solid #d8d8d8;
    border-radius: 6px;

    &:nth-child(n+2) {
      margin-top: 10px;
    }
  }

  &__label {
    position: absolute;
    top: 50%;
    left: 20px;
    font-size: 17px;
    color: #666;
    transform: translateY(-50%);
    transition: all 100ms;
  }

  &__row {
    display: flex;
    font-size: 0;

    #{$root}__label + & {
      #{$root}__text,
      .o-input {
        height: 64px;
        padding-top: 22px;
      }
    }

    & + & {
      border-top: 1px solid #eee;
    }

    & > *:nth-child(n+2) {
      display: none;
      margin-left: 10px;
    }
  }

  &__auto {
    flex: none !important;
  }

  &__text {
    flex: none !important;
    font-size: 17px;
    line-height: 42px;
    color: #000;
  }

  .o-input--text,
  .o-input--date,
  .o-input--textarea {
    padding: 0;
    border: none;
    background-color: transparent;
  }

  .o-input--date {
    line-height: 42px;

    & + #{$root}__text {
      margin: 0 10px 0 20px;
    }
  }

  .o-input--text {
    height: 42px;
    background-color: transparent !important;
  }

  .o-input--select {
    height: 42px;
    padding: 0;
    border: none;
    background-color: transparent;
    background-position: right 0 top 50%;
  }

  .o-input--text,
  .o-input--date,
  .o-input--textarea,
  .o-input--select {
    color: transparent;

    &::-webkit-input-placeholder {
      color: transparent;
    }

    &:-ms-input-placeholder {
      color: transparent;
    }

    &::-moz-placeholder {
      color: transparent;
    }
  }

  // 상태
  &__field {
    &.has-focus, // :focus-within
    &.has-value,
    &.has-value.is-disabled {
      #{$root}__row {
        & > * {
          display: block;
          flex: 1;
        }
      }

      #{$root}__label {
        top: 0;
        font-size: 13px;
        transform: translateY(10px);
      }

      #{$root}__label + #{$root}__row {
        .o-input--date,
        .o-input--select {
          background-position: right 0 top 40px;
        }
      }

      .o-input {
        color: #000;

        &::-webkit-input-placeholder {
          color: #666;
        }

        &:-ms-input-placeholder {
          color: #666;
        }

        &::-moz-placeholder {
          color: #666;
        }
      }

      .o-input--textarea {
        height: 108px;
        margin-top: 40px;
        padding-top: 0;
        font-size: 17px;
        line-height: 24px;
      }
    }

    &.has-focus { // :focus-within
      border-color: #0076c0;

      #{$root}__label {
        color: #0076c0;
      }
    }

    &.has-error {
      border-color: #ff3c50;

      #{$root}__label {
        color: #ff3c50;
      }
    }

    &.is-disabled {
      background-color: #f8f8f8;
    }
  }
}

/*---------------------------------------------
  기타
 ----------------------------------------------*/
// 에러 메시지
.message-error {
  display: block;
  width: 100%;
  margin-top: 9px;
  font-size: 15px;
  color: #ff3c50;
}

// 필수입력
.require {
  color: #ff3c50;
}
This post is licensed under CC BY 4.0 by the author.