💡 CSS의 Cascading원칙과 Selector에 대해 공부해보자
이번 포스트에서는 CSS의 기초인 Selector가 핵심이다.
CSS의 꾸미는 요소들을 학습하기 전에 Cascading 원칙과 CSS에 다양한 Selector들을 예제를 통해 학습해보는 시간을 가질 것이다.
CSS를 적용하기 전에 알아야하는 원칙들이다.
상속되지 않는 경우(상속받지 않는 요소 또는 상속되지 않는 프로퍼티),
inherit
키워드를 사용하여 명시적으로 상속받게 할 수 있다.
CSS Selector의 종류를 알아보고 예제를 통해 어떻게 적용되는지 확인해보겠다.
HTML :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="main.css">
<title>CSS Selector</title>
</head>
<body>
<h1 id="welcome-title">Welcome CSS</h1>
<h2>List 1</h2>
<ul>
<li class="color-blue">TEST LI 1</li>
<li>TEST LI 2</li>
<li class="color-blue">TEST LI 3</li>
</ul>
<h2>List 2</h2>
<ol>
<li>FIRST</li>
<li>SECOND</li>
</ol>
<h3>LOREM IPSUM</h3>
<p>Lorem ipsum dolor sit amet, </p>
<p class="color-blue">consectetur adipisicing elit.</p>
<p> Laborum ratione aliquid debitis error blanditiis fugiat quam cumque eos vel, </p>
<p>enim commodi deleniti, ea placeat nemo natus nobis provident in? Sit!</p>
</body>
</html>
특정 태그 전체에 적용되는 선택자로 사용에 주의가 필요하다.
CSS:
/* Type Selector */
h2 {
color: purple;
}
h3{
color: red;
}
Result:
태그에 입력한 id 값에만 적용된다.
CSS:
/* ID Selector */
#welcome-title {
color: aqua;
}
Result:
태그에 입력한 class 값들에 적용된다.
CSS:
/* Class Selector */
.color-blue {
color: blue;
}
Result:
태그 내부에 속성 값 (Attribute)가 있는 태그들만 스타일을 적용시킨다.
Attribute Selector 적용에는 다섯 가지 방식이 있다.
HTML :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="attributeSelector.css">
<title>Attribute Selector</title>
</head>
<body>
<ul>
<li>
<a href="http://example.com" target="_blank">
Exapmle Linke (com/http)
</a>
</li>
<li>
<a href="http://example.org" target="_blank">
Exapmle Linke (org/http)
</a>
</li>
<li>
<a href="https://example.com">
Exapmle Linke (com/http)
</a>
</li>
<li>
<a href="https://example.org">
Exapmle Linke (org/http)
</a>
</li>
</ul>
<input type="text">
<input type="submit">
<input type="reset">
</body>
</html>
CSS:
/* Attribute Selector */
/* 1. [attr] */
a[target] {
color: red;
}
/* 2. [attr=value] */
a[href="https://example.org"] {
color: indianred;
}
input[type="submit"] {
background-color:yellow;
}
/* 3. [attr^=value] */
a[href^="https://"]{
font-style: italic;
}
/* 4. [attr$=value] */
a[href$=".com"]{
font-size: large;
}
/* 5. [attr*=value] */
a[href*="example"]{
font-weight: bold;
}
Result:
selector로 가져온 요소에서 추가로 범위를 좁힐 수 있는 선택자이다.
selector:first-child
: selector로 가져온 요소의 부모 태그 기준으로 첫 번째 요소에만 적용
selector:last-child
: selector로 가져온 요소의 부모 태그 기준으로 마지막 요소에만 적용
selector:nth-child(n)
: selector로 가져온 요소의 부모 태그 기준으로 n 번째 요소에 적용
selector:first-of-type
: selector로 가져온 요소 중 첫 번째 태그의 요소에만 적용
selector:last-of-type
: selector로 가져온 요소 중 마지막 태그의 요소에만 적용
selector:nth-of-type(n)
: selector로 가져온 요소 중 n 번째 태그 요소에 적용
selector1:not(selector2)
: selector1으로 가져온 요소들 중에 selector2로 가져온 요소를 제외하고 적용
a:link
: 하이퍼 링크의 상태가 방문하기 이전일 때의 스타일을 적용
a:visited
: 하이퍼 링크의 상태가 방문한 이후일 때의 스타일을 적용
selector:hover
: selector로 가져온 요소에 마우스를 올리면 스타일이 적용
selector:active
: selector로 가져온 요소(버튼 등)의 활성화 시 나타남.
selector:focus
: selector로 가져온 요소(버튼 등)의 양식의 입력 칸 등 포커스를 받은 요소
HTML :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="pseudoClassSelector.css">
<title>Pseudo-class Selector</title>
</head>
<body>
<h2>first-child</h2>
<ul>
<li class="movie">first-child</li>
<li class="movie">Zootopia</li>
<li class="movie">Inside Out</li>
<li class="movie">nth-child(4)</li>
<li class="movie">last-child</li>
</ul>
<hr />
<h2>first-of-type</h2>
<section>
<div>Go to gim</div>
<p class="fot-class">.fot-class:first-of-type</p>
<p>nth-of-type</p>
<p>last-of-type</p>
<div>NetNet</div>
</section>
<hr />
<h2>not()</h2>
<input type="text" placeholder="name">
<input type="email" placeholder="email">
<input class="passwd" type="password" placeholder="password">
<input type="submit">
<hr />
<h2>link, visited</h2>
<a href="http://example.com">Example Link</a>
<hr />
<h2>hover, active, focus</h2>
<input type="button" value="click me!">
<input type="button" value="click me!">
<input type="button" value="click me!">
<input type="button" value="click me!">
<input type="text">
<hr />
<h2>enabled, disabled, checked</h2>
<input type="text" placeholder="1">
<input type="text" placeholder="2">
<input type="text" placeholder="3" disabled>
<input type="radio" name="my-input" id="yes">
<label for="yes">Yes</label>
<input type="radio" name="my-input" id="no">
<label for="no">No</label>
<input type="checkbox" name="check-me" id="check-me">
<label for="check-me">Check me!</label>
</body>
</html>
CSS:
/* Pseudo-class Selector */
/* 1. first-child */
li:first-child {
color: green;
}
.movie:first-child {
font-size: 32px;
}
/* 2. last-child */
li:last-child {
color: darkgreen;
}
/* 3. nth-child */
li:nth-child(4){
color: red;
}
/* 4. first-of-type */
.fot-class:first-of-type {
color: yellow;
}
/* 5. last-of-type */
p:last-of-type {
color: blue;
}
/* 6. nth-of-type */
p:nth-of-type(2n){
color: aqua;
}
/* 7. not() */
input:not([type="text"]) {
background-color: indigo;
}
/* 8. link */
a:link {
color: red;
}
/* 9. visited */
a:visited {
color: brown;
}
/* 10. hover */
input[value="click me!"] {
background-color: skyblue;
border: none;
}
input[value="click me!"]:hover {
background-color: teal;
color: white;
}
/* 11. active */
input[value="click me!"]:active {
background-color: yellow;
}
/* 12. focus */
input[type="text"]:focus {
background-color: brown;
}
/* 13. enabled */
input[type="text"]:enabled {
background-color: bisque;
}
/* 14. disabled */
input[type="text"]:disabled {
background-color: red;
}
/* 15. checked */
input[type="radio"]:checked {
outline: 3px solid red;
}
input[type="checkbox"]:checked {
outline: 3px solid blue;
}
Result:
selector로 가져온 속성에서 추가로 범위를 좁힐 수 있는 선택자이다.
selector::before
: 선택한 요소의 앞에 의사 요소를 하나 생성함.
selector::after
: 선택한 요소의 뒤에 의사 요소를 하나 생성함.
selector::first-letter
: 선택한 요소의 첫 번째 글자의 스타일을 변경해줌
before
과 함께 사용하게 되면 before에서 지정해준 content 속성 값의 첫 번째 글자의 스타일이 변경됨.selector::first-line
: 선택한 요소의 첫 줄의 스타일을 변경해줌.
selector::selection
: 선택한 요소의 드래그 스타일을 변경해줌.
HTML :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="pseudoElementSelector.css">
<title>Pseudo-element Selector</title>
</head>
<body>
<h2>before, after</h2>
<div class="before-after focus"> text1 </div>
<div class="before-after"> text2 </div>
<div class="before-after focus"> text3 </div>
<div class="before-after"> text4 </div>
<div class="before-after"> text5 </div>
<hr />
<h2>first-letter, first-line, selection</h2>
<p class="lorem">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eius modi animi tempora ea nulla impedit,
vero quisquam
exercitationem consequatur porro facere ex harum corporis cupiditate! Velit voluptatum repellendus unde consectetur.
</p>
<p class="lorem">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eius modi animi tempora ea nulla impedit,
vero quisquam
exercitationem consequatur porro facere ex harum corporis cupiditate! Velit voluptatum repellendus unde consectetur.
</p>
<p class="lorem">Lorem ipsum, dolor sit amet consectetur adipisicing elit. Eius modi animi tempora ea nulla impedit,
vero quisquam
exercitationem consequatur porro facere ex harum corporis cupiditate! Velit voluptatum repellendus unde consectetur.
</p>
</body>
</html>
CSS :
/* 1. before */
.focus::before {
content: '-';
color: aqua;
}
/* 2. after */
.before-after:not(.focus)::after {
content: '-';
color: aqua;
}
/* 3. first-letter */
.lorem::first-letter{
font-size: large;
color: red;
}
.lorem::before{
content: 'Before-';
}
/* 4. first-line */
.lorem::first-line{
color: blue;
}
/* 5. selection */
.lorem::selection {
background-color: aqua;
color: wheat;
}
Result :
CSS를 적재적소에 효과적으로 사용하기 위해서는 기초를 다져야한다.
프로젝트를 진행하면서 CSS 디자인에 큰 신경을 쓰지 못했고 알고 있는 기초 지식(class, id selector)들로만 요소를 가져와서 퍼플리싱을 했는데, 훨씬 다양한 셀렉터들이 있어 다양한 경우에 유연하게 사용할 수 있을 것 같다.