<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
height: 1000vh;
margin: 50px;
}
div{
width: 150px;
height: 150px;
background-color: wheat;
position: relative;
}
div:first-child{
background-color: tomato;
}
div:last-child{
background-color: teal;
}
</style>
</head>
<body>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</body>
</html>
첫번째 div는 tomato색으로, 마지막 div는 teal색으로 변했다. class나 id를 사용하지 않고 div:first-class, div:last-class를 이용하는 것이 훨씬 좋은 방법이다.
even 혹은 odd
로 짝수와 홀수번째 span을 가리킬 수 있다.<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
body{
height: 1000vh;
margin: 50px;
}
span{
background-color: violet;
}
span:nth-child(2),
span:nth-child(4){
background-color: yellow;
}
</style>
</head>
<body>
<span>hello</span>
<span>hello</span>
<span>hello</span>
<span>hello</span>
<span>hello</span>
<span>hello</span>
<span>hello</span>
</body>
</html>
:과 nth
을 붙여써야 된다!)span:nth-child(4n+1){
background-color: yellow;
}