연결 선택자(Combination Selector)

공백, >, +, ~ 등 결합자(combinator)를 이용해 특정 요소를 선택

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		header span {
			color: blue;
		}
	</style>
</head>
<body>
	<header>
		<h1>header -> h1</h1>
		<span>header -> span</span>
		<br>
		<div>
			<span>header -> div -> span</span>
		</div>
	</header>
	<span>span</span>
</body>
</html>

Untitled

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		header > span {
			color: blue;
		}
	</style>
</head>
<body>
	<header>
		<h1>header -> h1</h1>
		<span>header -> span</span>
		<br>
		<div>
			<span>header -> div -> span</span>
		</div>
	</header>
	<span>span</span>
</body>
</html>

Untitled

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		header + span {
			color: blue;
		}
	</style>
</head>
<body>
	<header>
		<h1>header -> h1</h1>
		<span>header -> span</span>
		<br>
		<div>
			<span>header -> div -> span</span>
		</div>
	</header>
	<span>span1</span>
	<div>
		<span>div -> span2</span>
	</div>
	<span>span3</span>
</body>
</html>

Untitled

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		header ~ span {
			color: blue;
		}
	</style>
</head>
<body>
	<header>
		<h1>header -> h1</h1>
		<span>header -> span</span>
		<br>
		<div>
			<span>header -> div -> span</span>
		</div>
	</header>
	<span>span1</span>
	<div>
		<span>div -> span2</span>
	</div>
	<span>span3</span>
</body>
</html>

Untitled

속성 선택자(Attribute Selector)

HTML 요소의 속성을 이용하여 특정 요소를 선택

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		[target] {
			color: green;
		}
	</style>
</head>
<body>
	<a href="" target="_blank">네이버</a>
	<br>
	<a href="">구글</a>
</body>
</html>

target 속성을 가진 요소만 적용

Untitled

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		[target="_self"] {
			color: green;
		}
	</style>
</head>
<body>
	<a href="" target="_blank">네이버</a>
	<br>
	<a href="" target="_self">구글</a>
</body>
</html>

target="_self" 인 요소만 적용

Untitled

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		[class~=att_class] {
			color: green;
		}
	</style>
</head>
<body>
	<h3 class="att_class">att_class</h3>
	<h3 class="att">att</h3>
	<h3 class="att_class2 att_class">att_class2 att_class</h3>
	<h3 class="att-1">att-1</h3>
</body>
</html>

class 속성의 값이 att_class가 포함된 요소만 적용

Untitled

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<style>
		[class~=att_class] {
			color: green;
		}
	</style>
</head>
<body>
	<h3 class="att_class">att_class</h3>
	<h3 class="att">att</h3>
	<h3 class="att_class2 att_class">att_class2 att_class</h3>
	<h3 class="att-1">att-1</h3>
</body>
</html>

class 속성의 값이 att 또는 att- 인 요소만 적용

Untitled