CSS독학1(CSS 선택자)
출처:www.pinkcoding.com
아주 유용한 사이트를 찾았다.
이걸 보면서 공부!
html문서에 적용하는 방법은 3가지가 있다.
1. 외부문서로 저장하여 연결하기
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>외부 CSS 파일 적용</title>
- <link rel="stylesheet" type="text/css" href="스타일시트 경로" />
- </head>
- <body>
- <div>
- </div>
- </body>
- </html>
2. 문서 내부에 저장하기
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>스타일 태그에 CSS 코드 작성</title>
- <style type="text/css">
- div{background:#fff}
- </style>
- </head>
- <body>
- <div>
- </div>
- </body>
- </html>
3.태그에 직접 지정하기
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>태그에 직접 CSS코드 작성</title>
- </head>
- <body>
- <div style="background:#fff">
- </div>
- </body>
- </html>
CSS선택자1(tag,class,id)
- 태그명을 선택자로 선택 (tag)
<head>
<style>
p{color:red}
</style>
</head>
- 태그에 클래스 속성을 지정하여 선택자 사용(class)
<head>
<style>
p{color:red}
.blue{color:blue}
</style>
<head>
<body>
<p class="blue">header</p>
</body>
- 자식 태그 선택자 사용
<html>
<head>
<meta charset="utf-8" />
<title>자식 태그 선택자 사용</title>
<style>
li p{color:red}
</style>
</head>
<body>
<ul>
<li><p>hello world in list </p></li>
</ul>
<p>hello world in h1</p>
</body>
</html>
- 태그에 아이디 속성을 지정하여 선택자 사용(id)
id는 고유한 값으로 오직 한번만 사용한다.
class는 여러번 가능
<head>
<style>
#hi{color:red}
</style>
</head>
<body>
<p id=hi>hello world</p>
</body>
CSS 선택자2(*,child)
- 모든 태그 선택하기(*)
<head>
<style>
*{color:pink}
</style>
</head>
자식 태그에 CSS적용
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>자식 태그 선택하기</title>
- <style>
- section h1{color:pink}
- </style>
- </head>
- <body>
- <section>
- <h1>h1 tag in section tag</h1>
- </section>
- <aside>
- <h1>h1 tag in aside tag</h1>
- </aside>
- </body>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>자식 클래스 선택하기</title>
- <style>
- section *{color:skyblue}
- // .parent2{color:blue} 추가적으로 해봄
- .parent2 .hOne{color:blue} //추가적으로 해봄
- //aside{color:blue} //추가적으로 해봄
- </style>
- </head>
- <body>
- <section class='parent'>
- <p>p 태그</p>
- <h1>h1 태그</h1>
- <h2>h2 태그</h2>
- <h3>h3 태그</h3>
- <h4>h4 태그</h4>
- <h5>h5 태그</h5>
- <h6>h6 태그</h6>
- <b>b 태그</b>
- <span>span 태그</span>
- <div>div 태그</div>
- <em>em 태그</em>
- </section>
- <aside class='parent2'>
- <h1 class='hOne'>hOne class in parent2 class</h1>
- </aside>
- </body>
- </html>
CSS선택자3(인접 형제 클래스)
- <head>
- <style>
- section aside h1 ~ p{color:pink}
- </style>
- </head>
- <body>
- <section>
- 1 gorup
- <b>b 태그</b>
- <span>span 태그</span>
- <div>div 태그</div>
- <em>em 태그</em>
- </section>
- <section>
- 2 group
- <h1>h1 태그</h1>
- <h2>h2 태그</h2>
- <h3>h3 태그</h3>
- <h4>h4 태그</h4>
- <h5>h5 태그</h5>
- <h6>h6 태그</h6>
- <aside>
- <h1>h1 태그</h1>
- <p>p 태그</p>
- <p>p 태그</p>
- </aside>
- </section>
- </body>
section aside h1 ~ p {color:pink}
section태그 안에 있는 aside태그 안에 있는 h1태그와 같은 선상에 있는 p태그에 적용
동일선상에 있는 태그 중 첫번째 태그만 적용(~ 대신 +)
<style>
section aside h1 + p{color:blue}
</style>
section aside h1 ~ p{color:blue} ->
section aside p ~ h1{color:blue}는 색이 안변하고 원래대로 됨
section aside h1{color:blue}는 h1태그만 색이 변함
section aside p{color:blue}는 p태그만 색이 변함
굳이 h1~p를 사용해야 되나 ...
CSS선택자(가상 클래스)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>가상 클래스</title>
<style>
a:link{color:skyblue}
a:visited{color:hotpink}
a:hover{color:orange}
a:active{color:green}
</style>
</head>
<body>
<a href='https://www.naver.com' target='_blank'>NAVER</a>
<a href='https://m.youtube.com' target='_blank'>YOUTUBE</a>
<a href='https://www.netfilix.com' target='_blank'>NETFILIX</a>
</body>
</html>
클릭을 하지 않은 상태 link skyblue
클릭을 이미 한 상태 visited hotpink
마우스를 올린 상태 hover orange
클릭을 하고 있는 순간 active green
a:hover{color:orange; text-shadow:4px 4px 10px deeppink} (추가)
naver를 올리는 순간 그림자가 적용 되었다.
CSS선택자5(위치 선택자)
- nth-child
- nth-last-child
- first-of-type
- last-of-type
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>위치 가상 클래스</title>
<style>
aside p:nth-child(1){color:skyblue} //aside에서 첫 요소
aside p:nth-last-child(2){color:blue} //aside에서 역순
section p:first-of-type{color:green} //section에서 처음 오는 요소
section p:last-of-type{color:orange} //section에서 마지막에 오는 요소
</style>
</head>
<body>
<section>
<p>pinkcoding</p>
<p>pinkcoding</p>
<p>pinkcoding</p>
</section>
<aside>
<p>tomodevel</p>
<p>tomodevel</p>
<p>tomodevel</p>
</aside>
</body>
</html>
CSS 선택자6(only 선택자)
- only-child
- only-of-type
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>only 가상 클래스</title>
- <style>
- p:only-child{color:skyblue}
- </style>
- </head>
- <body>
- <p>pinkcoding</p>
- //<b>pinkcoding</b> p태그 외에 다른 태그가 있으면 안됨
- </body>
- </html>
only-child 는 선택자가 오직 한개 일때 사용
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8" />
- <title>위치 가상 클래스</title>
- <style>
- p:only-of-type{color:skyblue}
- </style>
- </head>
- <body>
- <p>pinkcoding</p>
- //<p>pinkcoding</p> p태그가 2개이상 있으면 안됨
- <b>pinkcoding</b> //존재해도됨
- <em>pinkcoding</em> //존재해도됨
- </body>
- </html>
CSS선택자(속성선택자)
src,href,id,class,name,value 는 속성
ex1)
- <head>
- <meta charset="utf-8" />
- <title>속성 클래스</title>
- <style>
- p[id]{color:skyblue}
- [class]{color:hotpink}
- </style>
- </head>
- <body>
- <p id="myName">pinkcoding</p>
- <b class="myName2">pinkcoding</b>
- <em class="myName3">pinkcoding</em>
- <p class="myName4">pinkcoding</p>
- <span class="myName5">pinkcoding</span>
- <div >pinkcoding</div>
- </body>
ex2)
- <head>
- <meta charset="utf-8" />
- <title>속성 클래스</title>
- <style>
- [id]{color:skyblue}
- [class]{color:hotpink}
- </style>
- </head>
- <body>
- <p id="myName" class="myName2">pinkcoding</p>
- </body>
나중에 선언된 핑크색이 적용
- <head>
- <meta charset="utf-8" />
- <title>속성 클래스</title>
- <style>
- [class]{color:hotpink}
- [id]{color:skyblue}
- </style>
- </head>
- <body>
- <p id="myName" class="myName2">pinkcoding</p>
- </body>
나중에 선언된 하늘색이 적용
- <head>
- <meta charset="utf-8" />
- <title>속성 클래스</title>
- <style>
- p[class]{color:hotpink} //먼저 적용
- [id]{color:skyblue}
- </style>
- </head>
- <body>
- <p id="myName" class="myName2">pinkcoding</p>
- </body>
먼저 선언했더라도 속성과 함께 태그까지 지정하면 hotpink가 된다.
선택자[속성 = "값"]{CSS속성:값}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>속성 클래스</title>
<style>
a[href="https://www.naver.com"]{color:hotpink; text-decoration:none}
//text-decoration:none을 없애면 밑줄이 생긴다.
</style>
</head>
<body>
<a href="https://www.naver.com" target='_blank'>NAVER</p>
<a href="https://www.m.youtube.com" target='_blank'>YOUTUBE</p>
</body>
</html>
속성 선택차 값 체크
- ^=
- $=
- *=
^= 특정문구로 시작
- <style>
- a[href^="https://"]{color:hotpink;text-decoration:none}
- </style>
$= 특정문구로 끝남
- <style>
- a[href$=".com"]{color:hotpink;text-decoration:none}
- </style>
*= 특정문구로 포함
- <style>
- a[href*="naver"]{color:hotpink;text-decoration:none}
- </style>
- <style>
- a[href*="http"]{color:hotpink;text-decoration:none}
- </style>