기본 소스와 결과

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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        body{
            margin: 0px;
            padding: 0px;
        }
        div{
            width: 100px;
            height: 100px;
        }
        div:nth-child(1){
            background-color: lightpink;
        }
        div:nth-child(2){
            background-color: lightgreen;
        }
        div:nth-child(3){
            background-color: lightgray;
        }
        div:nth-child(4){
            background-color: lightyellow;
        }
        div:nth-child(5){
            background-color: lightblue;
        }
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</body>
</html>
cs


1번 : 왼쪽으로 300px 만큼 이동

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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        body{
            margin: 0px;
            padding: 0px;
        }
        div{
            width: 100px;
            height: 100px;
        }
        div:nth-child(1){
            background-color: lightpink;
            margin-left: 300px;
        }
        div:nth-child(2){
            background-color: lightgreen;
            position: static;    /*디폴트값*/
        }
        div:nth-child(3){
            background-color: lightgray;
        }
        div:nth-child(4){
            background-color: lightyellow;
        }
        div:nth-child(5){
            background-color: lightblue;
        }
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</body>
</html>
cs

결과

2번 : position 의 디폴트 값으로 설정(변화없음)

3번 : position 의 절대 값으로 설정

소스

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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        body{
            margin: 0px;
            padding: 0px;
        }
        div{
            width: 100px;
            height: 100px;
        }
        div:nth-child(1){
            background-color: lightpink;
            margin-left: 300px;
        }
        div:nth-child(2){
            background-color: lightgreen;
            position: static;    /*디폴트값*/
        }
        div:nth-child(3){
            background-color: lightgray;
            position: absolute;    /*절대값으로 위치를 설정*/
            left: 300px;    /* x축 */
            top: 400px;    /* y축 (작성하지 않는다면 원래의 위치로 설정됨 */
        }
        div:nth-child(4){
            background-color: lightyellow;
        }
        div:nth-child(5){
            background-color: lightblue;
        }
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</body>
</html>
cs

결과

4번 : 변경사항없음

5번 : position 의 상대 값으로 설정

소스

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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        body{
            margin: 0px;
            padding: 0px;
        }
        div{
            width: 100px;
            height: 100px;
        }
        div:nth-child(1){
            background-color: lightpink;
            margin-left: 300px;
        }
        div:nth-child(2){
            background-color: lightgreen;
            position: static;    /*디폴트값*/
        }
        div:nth-child(3){
            background-color: lightgray;
            position: absolute;    /*절대값으로 위치를 설정*/
            left: 300px;    /* x축 */
            top: 400px;    /* y축 (작성하지 않는다면 원래의 위치로 설정됨 */
        }
        div:nth-child(4){
            background-color: lightyellow;
        }
        div:nth-child(5){
            background-color: lightblue;
            position: relative;    /*상대좌표로서 원래위치에서 변경*/
            left: 50px;
            top: 50px;
        }
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</body>
</html>
cs

결과



제휴 사이트 연결하기

예시로 네이버/다음/서울시청/현재 블로그 사이트를 들 것이다.


소스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        #content a:after{
            content: '-' attr(href);
        }
    </style>
</head>
<body>
    <div id="content">
        <p><a href="http://naver.com">네이버</a></p>
        <p><a href="http://daum.net">다음</a></p>
        <p><a href="http://www.seoul.go.kr/">서울시청</a></p>
        <p><a href="http://programacion.tistory.com/">블로그</a></p>
    </div>
</body>
</html>
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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        div{
            width: 100px;
            height: 100px;
        }
        div:nth-child(1){
            background-color: lightpink;
        }
        div:nth-child(2){
            background-color: lightgreen;
        }
        div:nth-child(3){
            background-color: lightgray;
        }
        div:nth-child(4){
            background-color: lightyellow;
        }
        div:nth-child(5){
            background-color: lightblue;
        }
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</body>
</html>
cs

결과


1. 숨김기능

소스

* display: none;  공간도 제거되며 숨겨주는 기능

* visibility: hidden; 공간은 존재하며 숨겨주는 기능

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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        div{
            width: 100px;
            height: 100px;
        }
        div:nth-child(1){
            background-color: lightpink;
        }
        div:nth-child(2){
            background-color: lightgreen;
            display: none;    /*존재가 사라짐. 공간도 제거*/
        }
        div:nth-child(3){
            background-color: lightgray;
        }
        div:nth-child(4){
            background-color: lightyellow;
            visibility: hidden;    /*숨김. 공간은 남음*/
        }
        div:nth-child(5){
            background-color: lightblue;
        
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</body>
</html>
cs

2번의 공간이 제거됨을 볼수있다.

4번은 공간은 존재하지만 숫자4와 배경색이 보이지않는다.

결과


2. 선택지시자 (+와 ~의 차이)

소스1

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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        div{
            width: 100px;
            height: 100px;
        }
        div:nth-child(1){
            background-color: lightpink;
        }
        div:nth-child(2){
            background-color: lightgreen;
        }
        div:nth-child(3){
            background-color: lightgray;
        }
        div:nth-child(4){
            background-color: lightyellow;
        }
        div:nth-child(5){
            background-color: lightblue;
        }
        div:nth-child(2)+div{
            background-color: lightgreen;
        }
        /*div:nth-child(1)+div~div{
            background-color: lightgray;
        }*/  
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</body>
</html>
cs

nth-child(2) 는 2번의 위치를 말하는것이고

+div 는 다음 div 를 말하는것으로 결과적으로는 3번의 위치를 말한다.

        div:nth-child(2)+div{
            background-color: lightgreen;
        }

결과1


소스2

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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        div{
            width: 100px;
            height: 100px;
        }
        div:nth-child(1){
            background-color: lightpink;
        }
        div:nth-child(2){
            background-color: lightgreen;
        }
        div:nth-child(3){
            background-color: lightgray;
        }
        div:nth-child(4){
            background-color: lightyellow;
        }
        div:nth-child(5){
            background-color: lightblue;
        }
        div:nth-child(2)+div{
            background-color: lightgreen;
        }
        div:nth-child(1)+div~div{
            background-color: lightgray;
        }
    </style>
</head>
<body>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
    <div>5</div>
</body>
</html>
cs

nth-child(1) 는 1번의 위치를 말하는것이고

+div 는 다음 div 를 말하는것으로 2번의 위치이다. ~div란 뜻은 2번의 위치(해당위치)의 모든 자식을 뜻해서 결과적으로는 3,4,5번까지 모두 해당된다.

        div:nth-child(1)+div~div{
            background-color: lightgray;
        }

결과2

위의 기능을 덮어써 오버라이드 되었다.


소스

* position: fixed; 가 고정하는 기능을 한다.

* background-repeat: no-repeat; 는 이미지의 중복되는 것을 막아준다.

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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <style>
        body{
            margin: 0px;
            padding: 0px;
        }
        #bar{
            border: 1px red solid;
        }
        img{
            width: auto;
            height: 80px;
        }
        #img01{
            position: fixed;
            background-color: aquamarine;
            width: 100%;
            height: 250px;
            background-image: url(uuu.jpg);
            /*background-size: 50%;*/
            background-repeat: no-repeat;
            /*background-attachment: fixed;*/
        }
    </style>
</head>
<body>
<!--    <img alt="" src="googlelogo.png" />
    <div id="bar"> </div>-->
    <div id="img01">다음의 이미지는 로고</div>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/>
    <br/><br/><br/><br/><br/><br/><br/><br/><br/>
</body>
</html>
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
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
<!DOCTYPE html>
<html>
<head>
    <meta>
    <style>
        table{
            width: 100%;
        }
        tr:first-child{
            background-color: darkgray;
        }
        tr:nth-child(2n){    /*짝수*/
            background-color: beige;
        }
        tr>th:nth-child(1){
            width: 10%;
        }
        tr>th:nth-child(2){
            width: 60%;
        }
        tr>th:nth-child(3){
            width: 20%;
        }
        td>a{
            display: inline-block;
            width: 100%;
            text-decoration: none;
            color: black;
        }
        .line:hover{
            background-color: darksalmon;
        }
    </style>
</head>
<body>
    <table>
        <tr>
            <th>1열</th>
            <th>2열</th>
            <th>3열</th>
            <th>4열</th>
        </tr>
        <tr class="line">
            <td><a href="#">1줄</a></td>
            <td><a href="#">1</a></td>
            <td><a href="#">2</a></td>
            <td><a href="#">3</a></td>
        </tr>
        <tr class="line">
            <td>2줄</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr class="line">
            <td>3줄</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr class="line">
            <td>4줄</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr class="line">
            <td>5줄</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr class="line">
            <td>6줄</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr class="line">
            <td>7줄</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr class="line">
            <td>8줄</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
        <tr class="line">
            <td>9줄</td>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
    </table>
</body>
</html>
cs


결과

마우스를 올리고 있는 경우


+ Recent posts