Cascade Style Sheet (CSS)
HTML : 가져다 놓고
CSS : 꾸미고
Javascript : 시킨다.
설계는 HTML로 & 디자인은 CSS로
<span style="font-weight: bold; color: blue;">이 부분</span>
<head>
<!-- 중략 -->
<style>
span {
font-weight: bold;
color: blue;
}
</style>
</head>
index.html
<head>
<!-- 중략 -->
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
style.css
span {
font-weight: bold;
color: blue;
}
VSCode - live server preview -> 설치 후 해당 파일에서 F1 누르고 -> show live server preview
문서 내 지명된 모든 태그에 적용
div {
margin: 24px;
padding: 48px;
background-color: pink;
}
문서 내 다수 요소에 동일하게 적용될 수 있는 class 속성에 따라 적용
.finished { color: skyblue; }
.important { text-decoration: underline; }
/* 특정 태그에 해당 클래스가 있을 때 */
li.important { background-color: yellow; }
/* 하나 이상의 클래스 */
.important.finished { font-style: italic; }
(단일 선택자)
문서 내 유일한 요소에 적용될 수 있는 id 속성에 따라 적용
input[type=text] { background-color: skyblue; }
label[for=ip-id] { color: blue; }
내부의 모든 요소들을 선택
.outer div {
background-color: yellow;
}
바로 자식 단계(1촌)의 요소들 선택
.outer > div {
background-color: yellow;
}
바로 안 단계의 요소들을 선택
div {
padding: 12px 24px;
border-top: 1px solid black;
}
.selected {
border-top: 0;
color: white;
background-color: dodgerblue;
}
.selected + div { border-top: 0; }
모든 태그에 적용
section * {
display: inline-block;
background-color: orange;
padding: 24px;
}
어떤것은 해당하고 어떤것은 해당하지 않는것에 CSS를 줄 때
.underline:not(.blue) { color: red; }
.underline:not(.blue):not(li) { color: inherit; }
요소 안의 순서에 따라 CSS를 주고 싶을때
// 처음에 적용
ul li:first-child { border-top: 2px solid black; }
// 마지막에 적용
ul li:last-child { border-bottom: 2px solid black; }
// 3번째에 적용
ul li:nth-child(3) { color: purple; }
// 짝수에만 적용
ul li:nth-child(even) { background-color: #eee; }
// 일정 주기로 적용
ul li:nth-child(3n+1) { text-decoration: underline; }
요소에 마우스를 올렸을 때 CSS 적용
.blue-button {
font-size: 1em;
padding: 16px 24px;
color: white;
background-color: dodgerblue;
border: 0;
cursor: pointer;
border-radius: 4px;
}
.blue-button:hover {
background-color: darkblue;
}
cusor: pointer 는 커서를 올렸을 때 커서 모양을 바꿔준다.
:hover 를 넣으면 커서를 올렸을 때 적용되는 CSS를 입력할 수 있다.
div ul {
display: none;
position: absolute;
width: 100px; left: 12px; top: 56px; margin: 0; padding: 0;
}
div:hover ul { display: block; }
div ul li {
background-color: #444;
height: 44px; line-height: 44px; text-align: center;
}
div ul li:hover { background-color: #222; }
인라인 스타일 > id 선택자 > class 선택자 > 태그 선택자
구체적일수록 높은 순위
같은 우선순위라면 다음에(더 아랫줄) 오는 것이 덮어씀
color picker 로 찾기!