Learn!
- CSS 기본문법
- 선언 방식
- 기본 선택자
- 복합 선택자
- 가상 클래스 선택자
- 가상 요소 선택자
- 속성 선택자
- 스타일 상속
- 선택자의 우선순위
<html lang="ko">
<head>
<style>
/* 주석 작성 */
div {
margin : 30px;
color : blue;
}
</style>
</head>
<body>
<div> hello </div>
</body>
</html>
<style> div{ color: red; margin: 20px; } </style>
<div style="color: red; magin: 20px;"></div>
스타일 속성에 직접 작성하는 방식(선택자 없음)<link rel="stylesheet" href="./css/main.css">
@import url("파일경로")- CSS의 @import 규칙으로 CSS 문서 안에서 또 다른 CSS 문서를 가져와 연결하는 방식.
- 직렬 연결 방식
- html에 메인으로 연결되어있는 css링크가 우선 적용되고, 그 css 링크에서 import된 css파일이 적용이 됨.
* { color: red; } abc { color: red; } .class { color: red; } #id { color: red; }
*(에스터리스크) : 모든 요소를 선택하는 선택자태그 선택자 : 태그 이름을 입력하는 선택자 클래스 선택자 : 클래스 속성의 값이 .클래스이름인 요소 선택아이디 선택자 : id 속성의 값이 #id이름인 요소 선택<div> <ul> <li>사과</li> <li>딸기</li> <li class='orange'>오렌지</li> <li>망고</li> <li>사과</li> </ul> <div>당근</div> <p>토마토</p> </div> <span class="orange">오렌지</span>
일치 선택자 : 선택자 두개를 동시에 만족하는 요소 선택자식 선택자 : 하위 선택자 : '띄어쓰기'가 선택자의 기호!인접 형제 선택자 : li 하나를 선택일반 형제 선택자 : li 모두를 선택.box { width: 100px; height: 100px; background-color: orange; transition: 1s; } .box:hover { width:300px; background-color: royalblue; }
- 선택자 ABC요소에 마우스 커서가 올라가 있는 동안 적용됨.
a : active { color: red; }
- 마우스를 클릭하고 있는동안 적용됨
- 클릭 버튼을 누르지 않으면 원래 상태로 돌아옴
input:focus { background-color: orange; }
- focus가 될 수 있는 요소 : HTML 대화형 콘텐츠
- input, a, button, label, select ...
- tabindex 속성에 -1값을 넣으면 focus가 안되는 요소에도 적용 가능함
<div class="fruits"> <span>딸기</span> <span>수박</span> <div>오렌지</div> <p>망고</p> <h3>사과</h3> </div>
first-child
last-child
nth-child
* 전체선택자가 붙어있기때문에 .fruits 클래스의 모든 하위요소를 뜻함 :not(부정선택자)
span 태그가 아닌것을 선택 .box::before { content: "앞!"; } .box::after { content: "뒤!"; } .box::after { content: ""; display: block; width: 30px; height: 30px; background-color: royalblue; }
ABC::before : 선택자 ABC 요소의 내부 앞에 내용을 삽입ABC::after : 선택자 ABC 요소의 내부 뒤에 내용을 삽입::before, ::after: 가상의 인라인 요소 <input type="text" value="HEROPY"> <input type='password' value='1234'> <input type='text' value='ABCD' disabled> <span data-fruit-name="apple">사과</span>[type] { color: red; } [type="password"]{ color: red; } [data-fruit-name]{ color: red; }
스타일 상속
<div class='animal'>동물 <div class='tiger'>호랑이</div> <div class='lion'>사자</div> <div class='elephant'>코끼리</div> </div>.animal { color: blue; }
부모요소에 적용된 스타일은 자식요소에도 상속되어 적용된다.
상속되는 CSS속성은 보통 글자/문자 관련 속성 (BUT 모든 글자/문자 속성은 아님!)
강제 상속
<div class="parent"> <div class="child"></div> </div>.parent { width:300px; height:200px; background-color: red; } .child { width:100px; height:inherit; background-color:inherit; position: fixed top:100px; right:10px; }
inherit이라는 값을 사용하여 강제 상속 시킬 수 있다. 우선순위란?
- !important
- 인라인 선언
- ID 선택자
- Class 선택자
- 태그 선택자
- 전체 선택자