- radio 버튼

동일한 name값을 가져야 다중 선택이 가능함

 

- custom radio button : 배경이미지 이용 ( background: url(img/....jpg) )

백그라운드이미지를 왼쪽 오른쪽으로 이동시킴

1
2
3
4
5
6
7
8
9
10
11
    <div class="grade">
        <h2>Custom Radio</h2>
        <input type="radio" name="grade" id="grade-chk1"> 
        <label for="grade-chk1"><em></em>초등학생</label> 
        <input type="radio" name="grade" id="grade-chk2"> 
        <label for="grade-chk2"><em></em>중학생</label>  
        <input type="radio" name="grade" id="grade-chk3"> 
        <label for="grade-chk3"><em></em>고등학생</label>  
        <input type="radio" name="grade" id="grade-chk4"> 
        <label for="grade-chk4"><em></em>대학생</label>     
    </div>
cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
.grade input[name=grade] {
    display: none;
}
.grade label {
    cursor: pointer;
}
.grade label em {
    /* border: 1px solid red; */
    display: inline-block;
    width: 18px;
    height: 18px;
    vertical-align: middle;
    margin-right: 5px;
    background: url(../img/check/radio-01.png) no-repeat left center;
}
.grade input[type=radio]:checked + label em {
    background-position: right center;
}
.grade input[type=radio]:checked + label {
    color: #f156b6;
}
cs

 


 

- custom checkbox: 폰트어썸 4.7 이용

https://fontawesome.com/v4.7/

before after : input 요소에는 사용할 수 없음 

label:before 

active : 마우스를 눌렀다가 떼지 않은 상태 

 

 

1
2
3
4
5
6
7
8
9
10
    <form action="">
        <input type="checkbox" id="chk1" checked>
        <label for="chk1">HTML</label>
        <input type="checkbox" id="chk2">
        <label for="chk2">CSS</label>
        <input type="checkbox" id="chk3">
        <label for="chk3">jQuery</label>
        <input type="checkbox" id="chk4">
        <label for="chk4">UIKit</label>
    </form>
cs

 

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
/* 폰트어썸 4.7 */
 
@import url('https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css');
 
form input[type=checkbox] {
    display: none;
}
form label {
    display: block;
    width: 150px;
}
/*
아이콘 유니코드로 content에 넣기  
font-family를 폰트어썸으로 해야함(필수) 
fontsize로 아이콘 사이즈 조절
*/
form label:before {
    content: '\f00c';
    font-family: fontawesome;
    display: inline-block;
    width: 14px;
    height: 14px;
    line-height: 14px;
    border: 1px solid #333;
    border-radius: 3px;
    margin-right: 5px;
    text-align: center;
    vertical-align: middle;
    font-size: 13px;
 
    transition: 0.3s;
    color: transparent;
}
form input[type=checkbox]:checked + label:before {
    background-color: crimson;
    color: #fff;
    border-color: transparent;
}

/* scale(0) 으로 없어졌다가 나타나는 느낌 */
form input[type=checkbox] + label:active:before {
    transform: scale(0);
}
cs

 

- checkbox

<input type="checkbox"> 

체크박스를 이용할 때 텍스트를 눌러도 체크가 가능하게 만들어야한다.

 

input의 id == label의 for 일치시키는 방법 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    <div class="custom">
        <label>
            <input type="checkbox">
            약관을 충분히 이해하였으며 동의 합니다.
        </label>
    </div>

    <!-- 추천 방법  -->
    <div class="custom">
        <input type="checkbox" id="agree-chk">
        <label for="agree-chk">
            약관을 충분히 이해하였으며 동의 합니다.
        </label>
    </div>
cs

 

 


- custom checkbox 만들기

체크박스 이미지를 하나 준비한다. label안에 이미지를 넣을 태그 하나 만들어야한다.

이 예제에서는 em태그로 했지만 다른 태그도 가능

처음에 빈 체크박스 이미지를 보여주고 checked 되면 체크된 이미지를 보여준다. (백그라운드 포지션 right center)

- html

1
2
3
4
5
    <div class="custom">
        <input type="checkbox" id="chk">
        <label for="chk"><em></em>약관을 충분히 이해하였으며 동의 합니다.
        </label>
    </div>
cs

 

 

- css 

em은 인라인요소이다. 

백그라운드-포지션: left (수평), center (수직) // 디폴트값

:checked 눌렀을 때 

+ 인접선택자 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
.custom input[type=checkbox] {
    /* html에서 기능을 하지않는 것이 아니라 보이지 않는것임 */
    display: none;
}
.custom label em {
    display: inline-block;
    /* border: 1px solid red; */
    width: 18px;
    height: 18px;
 
    /* 인라인 끼리 중앙을 맞출때 */
    vertical-align: middle;
 
    margin-right: 5px;
    background: url(../img/check/checkbox-01.png) no-repeat;
   /* background-position: left center; */
}
/* type=checkbox체크 박스를 눌렀을 때 그 바로밑에 있는 label em 요소
+ 인접선택자 : 바로 밑에 요소 */
.custom input[type=checkbox]:checked + label em {
    background-position: right center;
}
cs

 

 

 

 

 

+ Recent posts