오늘 배운 CSS Selector 부분이 헷갈려서 적어본다.
그리고 css가 안먹혀서 티스토리로 옮길까 했는데
계정만들기가 귀찮아서 그냥 하기로 했다.🤪
INDEX
1. Eric A. Meyer 의 reset.css
2. CSS 적용방식 (@import 와 link의 차이점)
3. 선택자 (CSS Selector)
Reset.css
말 그대로 css를 리셋시켜주는 역할을 한다. 브라우저 마다 다른 마진으로 인해 생기는 요소 불일치를 줄여주는것이 목적이며, 문자, 헤딩, 라인높이 값 등을 0으로 리셋시켜준다.
참고로 이분의 저서 중 'CSS 완벽 가이드'(CSS: The Definitive Guide: Visual Presentation for the Web)란 책도 있다고 한다. CSS 문법 뿐만 아니라 관련 히스토리도 같이 알 수 있다고 한다. 무려 1260페이지라고 한다! 디스코드 채널에 있던 분의 표현을 빌리자면...
<style>
/* http://meyerweb.com/eric/tools/css/reset/
v2.0 | 20110126
License: none (public domain)
*/
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
display: block;
}
body {
line-height: 1;
}
ol, ul {
list-style: none;
}
blockquote, q {
quotes: none;
}
blockquote:before, blockquote:after,
q:before, q:after {
content: '';
content: none;
}
table {
border-collapse: collapse;
border-spacing: 0;
}
</style>
<style>
p{
font-size: 16px;
font-weight: 400;
color: blue;
}
</style>
<head>
<link rel="stylesheet" href="#.css">
</head>
<style>
@import url("#.css");
</style>
<body>
<h1 style="color: red;">Hello World<h1>
<body>
link방식과 @import방식의 차이점
멘토님께서 이렇게 답변해주셨다.
즉,@import 방식은 비교적 최신문법이라 구형 브라우저나 엣지에서 버그가 발생할 수 있다고 한다.
스택 오버플로우에는 이런 답변이 있는데 나중에 따로 포스팅해도 재미있을 것 같다.
Stack Overflow | Difference between @import and link in CSS
*{
margin: 0;
padding: 0;
}
p{
text-size: 16px;
color: red;
}
<head>
<title>css class</title>
<style>
.one {
color: aqua;
}
.two {
border: solid 1px black;
border-top-color: red;
}
.three {
font-size: 48px;
}
</style>
</head>
<body>
<!--
1. calss는 중첩해서 사용할 수 있다. 반면 id는 중첩해서 사용할 수 없습니다.
2. page 내 class는 여러개 존재해도 됩니다. 반면 id는 유일해야 합니다.
-->
<h1>hello world</h1>
<h1>hello world</h1>
<h1 class="one">hello world</h1>
<h1 class="two three">hello world</h1>
<h1 class="two">hello world</h1>
</body>
실행화면
<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">
<title>css class</title>
<style>
#one {
color: aqua;
}
#two {
border: solid 1px black;
border-top-color: red;
}
#three {
font-size: 48px;
}
</style>
</head>
<body>
<!--
1. id는 중첩해서 사용할 수 없습니다. 반면 class는 중첩해서 사용할 수 있습니다.
2. page내 id는 유일해야 합니다. 반면 class는 여러개 존재해도 됩니다.
-->
<a href="#one">클릭하세요!</a>
<h1>hello world</h1>
<h1>hello world</h1>
<h1 id="one">hello world</h1>
<h1 id="two three">hello world</h1>
<h1 id="two">hello world</h1>
</body>
실행화면
![]()
<title>selector</title>
<style>
ul p { /*요소 안의 선택자*/
color: red;
}
li > p { /*li 바로밑의 p*/
color: blue;
}
</style>
<body>
<ul>
<li>
<p>hello world</p>
</li>
<li>
<div>
<div>
<div>
<p>hello world</p>
</div>
</div>
</div>
</li>
<li>hello world</li>
<li>hello world</li>
</ul>
</body>
</html>
<style>
h1+ ul { /* h1 태그 옆의 ul 태그에 적용 (형재태그)*/
color: red;
}
</style>
<body>
<h1>hello world</h1>
<ul>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
</ul>
<ul>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
</ul>
<ul>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
</ul>
</body>
<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">
<title></title>
<style>
h1 ~ ul { /* ~을 기준으로 앞요소 이후에 있는 모든 뒤 요소 선택 */
color: red;
}
</style>
</head>
<body>
<h1>hello world</h1>
<ul>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
</ul>
<ul>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
</ul>
<ul>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
<li>hello world</li>
</ul>
</body>
</html>
<!DOCTYPE html>
<html lang="ko">
<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">
<title>헷갈리는 선택자</title>
<style>
.one .two { /*one 바로 아래 있는(자손) two 선택*/
color: blue;
}
.one.two { /*one 안에 포함되어 있는 two 선택*/
color: red;
}
</style>
</head>
<body>
<!-- .one
.one.two
.one>.two -->
<div class="one">hello world</div>
<div class="one two">hellow world</div>
<div class="one">
<div class="two">hello world</div>
</div>
</body>
</html>
실행화면
<!DOCTYPE html>
<html lang="ko">
<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">
<title>속성 선택자</title>
<style>
a[href] {
font-size: 32px;
}
a[href="http://velog.io"] {
font-size: 48px;
}
</style>
</head>
<body>
<a href="#">어디로모실까요?</a>
<a href="http://velog.io">벨로그</a>
<a>벨로그로갑시다</a>
</body>
</html>
실행화면
<!DOCTYPE html>
<html lang="ko">
<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">
<title>속성 선택자</title>
<style>
a[href="http://www.paullab.co.kr"] {
font-size: 48px;
}
a[href~="paullab"] { /*href 안의 변수의 단어를 포함하는 태그를 선택*/
color: turquoise;
font-weight: normal;
}
a[href~="naver"] {
color: turquoise;
font-weight: normal;
}
a[href*="paullab"] { /*속성의 속성값이 변수의 문자열을 포함하는 태그를 선택*/
color: red;
font-weight: bold;
}
a[href$=".pdf"] { /* 속성값이 변수(여기서는 ".pdf") 로 끝나는 요소 연결 */
font-size: 100pt;
}
</style>
</head>
<body>
<a href="#">클릭해!</a>
<a href="http://www.paullab.co.kr">클릭해!</a>
<a href="asd.pd">클릭해!</a>
<a href="asd.pdf">클릭해!</a>
<a href="naver">클릭하란마리양</a>
<a href="www.naver.com">클릭하란마리양</a>
<a>클릭해!</a>
</body>
</html>
실행화면
가상 선택자 이해를 잘 못했는데 다음에 꼭 정리해야겠다!
하루만에 진도가 많이 나가서 정신이 없다...