span { color :red; }
css는 위와 같이 구성된다고 할 수 있다.
style을 html에 적용하는 방법은 3가지가 있다.
style="color:red;"
라고 되어 있는 것을 볼 수 있다.<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>World !</title>
</head>
<body>
<span style ="color:red;">HELLO World!</span>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>World !</title>
<style>
span{
color :red;
}
</style>
</head>
<body>
<span>HELLO World!</span>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>World !</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<span>HELLO World!</span>
</body>
</html>
color:green;
과 font-size : 30px;
의 속성이 상속되어 적용됨.border
나 padding
과 같은 배치와 관련된 속성은 상속이 되지 않고 body태그 바로 밑의 div태그에만 적용되었다.body > div
는 div
로도 표현이 가능하긴 하지만, body태그 밑의 div인지 li태그 밑의 div인지 불명확하기 때문에 body > div
로 해주어야 한다.<html>
<head>
<style>
body > div{
color : green;
font-size : 30px;
border:2px solid slategray;
padding : 30px;
}
</style>
</head>
<body>
<div>
<span>my text is upper</span>
<ul>
<li><span>my text is dummy</span>
<div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptas repudiandae minima excepturi repellendus itaque facere maxime mollitia laborum placeat minus possimus a ullam doloremque, officiis eveniet quas, hic sequi quod?</p>
</div>
</li>
<li></li>
</ul>
</div>
</body>
</html>
div> p
와 p
)선언방식에 따른 차이
inline >internal >external
body> span
이 더 구체적으로 표현되었으므로 우선해서 적용된다.두가지 코드가 같은 속성을 주고 있지만 id값에 더 높은 점수를 준다.
id > class > element
이때 element는 div와 같은 selector를 의미한다.
css specificity
를 검색하면 된다.