강의노트
아래주소의 github에 업로드된 예제코드를 다운받아 실습 할 수 있습니다(B_CSS 참조).
https://github.com/iseohyun/html-tutorial.git
문법
용어
article {
width: 95%;
max-width: 860px;
margin: 0 auto;
padding: 0 5px;
counter-increment: table-no 0;
padding-bottom: 50vh;
}
선언
상황에 맞춰서 혼합해서 사용하면 됩니다.
방법1: 외부 파일
A_declare >> b_linkFile.html<link rel="stylesheet" href="style.css">
또는
<style type="text/css">
@import url("style.css");
</style>
- 장점: 사이트를 통일성 있게 구성할 수 있다.
- 단점: 세세한 설정이 어렵다.
방법2: 내부 파일
A_declare >> a_innerFile.html
<style>
span {
color: red;
}
</style>
이것은 <span>빨간색</span>입니다.
또는
이것은 <span style="color:red">빨간색</span>입니다.
- 장점: 세세한 설정이 가능하다.
- 단점: 통일성이 떨어질 수 있다. 유지보수가 어렵다.
우선순위
A_declare >> c_priority.html
속성이 중복 선언 된 경우, 우선순위에 따라 적용이 됩니다.
자세한 내용은 선택자select(다음단원)에 나옵니다.
상세내용: 링크 - 명시도@mozilla
우선순위 | 문법 | html | CSS |
---|---|---|---|
1 | !important | <p>text</p> |
p{ color: blue !important; } p{ color: red; } |
2 | 명시도 점수가 높은 (아래 참조) |
<div> <div> A </div> <div class="X"> B </div> </div> |
div { background-color: red; } div>div { background-color: blue; } div.X { background-color: green; } |
3 | 명시도 점수가 같다면, 나중에 호출 된 |
<p>text</p> |
p{ color: blue} p{ color: red; } |
예시 | 1순위 | 2순위 | 3순위 | 비고 | |
---|---|---|---|---|---|
#id { ... } | <element id="id"> | 1 | 0 | 0 | ID는 1순위 |
.class { ... } | <element class="class"> | 0 | 1 | 0 | Class는 2순위 |
table { ... } | <table> | 0 | 0 | 1 | element는 3순위 |
table#id { ... } | <table id="id"> | 1 | 0 | 1 | 함께 쓰면 각각 카운터 |
table.class{ ... } | <table class="class"> | 0 | 1 | 1 | |
table td{ ... } | <table> ... <td> |
0 | 0 | 2 | |
tr>td{ ... } | <tr> <td> |
0 | 0 | 2 | |
table#id tr.class{ ... } | <table id="id"> ... <tr class="class"> |
1 | 1 | 2 | |
a:hover { ... } | <a :hover> | 0 | 1 | 1 | pattern은 2순위 |
a:hover:after { ... } | <a :hover :after> | 0 | 1 | 2 | pattern은 중복적용(x) |
ol:hover>li:after { ... } | <ol :hover> <li :after> |
0 | 1 | 3 | |
intput[type=text] { ... } | <input type="text"> | 0 | 1 | 1 | 속성 선택자 2순위 |
선택자
B_Selector >> [No].htmlNo | 속성내용 | 문법 | 예시 | 결과 | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | 전체 선택자 | * | * { ... } | 모든 객체 선택 | ||||||||||||||||||||||||||
태그 선택자 | [TagName] | div { ... } | <div> | |||||||||||||||||||||||||||
복수 선택자 ,(콤마) |
[TagName], [TagName] |
p, div { ... } | <div> <p> |
|||||||||||||||||||||||||||
B | 클래스 선택자 .(마침표) |
.[ClassName] | .my-class { ... } | <... class="my-class"> |
||||||||||||||||||||||||||
p.my-class { ... } | <p class="my-class"> |
|||||||||||||||||||||||||||||
아이디 선택자
[*]
중복이 안되는 것은 아닌데, 특정지어서 ID를 부여해놓고, 다른 선택지를 중복선택한다는 것이 설정오류
|
#[ID Name] | #my-id { ... } | <... id="my-id"> |
|||||||||||||||||||||||||||
C | 하위 선택자 | ' '(띄어쓰기) | table td { ... } | <table> ... <td> |
||||||||||||||||||||||||||
> | table>td { ... } | <table> <td> |
||||||||||||||||||||||||||||
인접 선택자 | ~ | h1~ul { ... } | <h1> ... <ul> |
|||||||||||||||||||||||||||
+ | h1+ul { ... } | <h1> <td> |
||||||||||||||||||||||||||||
D | 속성 선택자 | [속성명] | p[attr] { ... } | <p attr="..." [*]
>
속성이 지정되지 않고 존재만 하더라도 해당된다.
예) checked |
||||||||||||||||||||||||||
[속성명="값"] | p[color="red"] | <p color="red"> | ||||||||||||||||||||||||||||
[속성명~="값"] | p[color~="red"] | <p color="... red ..."> | ||||||||||||||||||||||||||||
[속성명*="값"] | p[color*="red"] | <p color="...red..."> | ||||||||||||||||||||||||||||
[속성명^="값"] | p[color^="red"] | <p color="red..."> | ||||||||||||||||||||||||||||
[속성명$="값"] | p[color$="red"] | <p color="...red"> | ||||||||||||||||||||||||||||
[속성명|="값"] | p[color|="red"] | <p color="red-..."> | ||||||||||||||||||||||||||||
E ~J |
가상 클라스 선택자 |
:패턴명 ::패턴명 |
|
Tip: has선택자
<div id="A"> a
<div id="B"> b
위에서 B를 선택하는 방법은 A>div가 있지만, B에서 A를 선택하는 방법은 javascript를 사용해야 했습니다. div:has(#B)를 통해서 선택할 수 있습니다.
div:has(#B) { color: red; }
단위
디스플레이의 크기와 해상도에 따라서 보여지는 크기를 조절 할 수 있습니다. 같은 10픽셀pixel이어도 사용자의 디바이스 해상도에 따라 의도하지 않게 연출 될 수 있다는 의미입니다. (버전에 따라 px가 절대출력크기로 보여질 수 있습니다.) 사용자에게 보여지는 크기를 지정하고 싶다면, inch 나 cm 단위를 사용하는 것이 좋습니다.
C_size >> [No].htmlNo | 기준 | 분류 | 소분류 | 단위 | 이름 | 상세 |
---|---|---|---|---|---|---|
A | 절대 | 실측 | 미터법 | cm | 센티미터 | |
mm | 밀리미터 | 1cm = 10mm | ||||
Q | 4분의1 밀리미터 | 1cm = 40Q | ||||
인치법 | in | 인치 | 1in = 2.54cm | |||
pc | Picas | 1in = 6pc | ||||
pt | 포인트 | 1in = 72pt | ||||
화소 | px | 픽셀 | 1in = 96pt(*실측의 경우) | |||
상대 | 글꼴 | 상위 요소 | em | 상대크기 | ||
ex | x height | 상대높이 | ||||
ch | 상대너비 | |||||
lh | line height | 라인 높이(줄 간격 포함) | ||||
최상위 요소 (root) |
rem | 상대크기 | ||||
rlh | root line height | 라인 높이(줄 간격 포함) | ||||
B | 화면출력 각 단위 1 = 1% |
vh | view height | 현재 보여지는 화면의 높이 | ||
vw | view width | 현재 보여지는 화면의 가로 | ||||
vmin | view minimum | 보여지는 화면이 정사각형이 아닐 때, 짧거나 긴 축의 길이 | ||||
vmax | view maximum | |||||
vb | view block | 가로쓰기모드: block=가로, inline=세로 세로쓰기모드: block=세로, inline=가로 |
||||
vi | view inline | |||||
svw, svh | small view width/height | [모바일 환경에서] small: 주소창 등의 표시로 제한된 영역 large: 최대의 영역 dynamic: 동적으로 움직이 때 현재의 영역 |
||||
lvw, lvh | large view width/height | |||||
dvw, dvh | dynamic view width/height | |||||
높이/넓이 | 상위요소 | % | percent | 상위 요소로부터 할당받은 크기를 100%로 하여 |
상속(상위 요소 vs 최상위 요소)
아래와 같은 구조에서, 1.5em으로 설정한(A)의 경우 이전의 크기를 참조해서 계속 커지지만, 1.5rem으로 설정한(R)의 경우 기본 크기를 참조하기 때문에 더 커지지 않고 일정하게 표시됩니다.
<span> A1(1.5em), R1(1.5rem) <span> A2(1.5em), R2(1.5rem) <span> A3(1.5em), R3(1.5rem) <span> A4(1.5em), R4(1.5rem) </span> </span> </span> </span>
설정 폰트의 조절은 브라우저가 제공하는 확대/축소(ctrl + '+/-')와는 다릅니다. 브라우저의 확대/축소는 보여지는 크기 전체를 동일비율로 변경합니다. 반면, 설정 폰트의 조절은 디자인을 유지한 상태에서 글자의 크기만 조절합니다.
함수
D_function >> [No].html값value를 직접 입력할 수도 있지만, 참조할 상수들을 조합하여 작성할 수 있습니다.
No | 특성 | 함수명 | 설명 | 예시 |
---|---|---|---|---|
A | :root | var, env |
env()함수는 시스템에서 관리하는 변수에 접근할 수 있습니다. 예: env(safe-area-inset-top); env(safe-area-inset-right); env(safe-area-inset-bottom); env(safe-area-inset-left); |
:root{ --myColor:#FD7751; } span { color: var(--myColor) } |
범용 | calc |
괄호 안의 내용을 계산 후 반환합니다. * 주의: 띄어쓰기가 없으면 에러 |
width: calc(90% - 30px); | |
B | abs, exp, log, max, min, minmax, mod, pow, round, sign, sqrt, sin, cos, tan, acos, asin, atan, atan2 |
abs: 절대값(absolute) exp: 지수(exponent) log: 로그 max: 최대값(maximum) min: 최소값(minimum) minmax: 최대, 최소 범위 round: 반올림 sign: 부호 sqrt: 제곱근 sin, cos, tan: 삼각함수 acos, asin, atan, atan2: 역삼각함수 |
grid-template-columns: minmax(min-content, 300px) minmax(200px, 1fr) 150px; transform: rotate(atan2(높이, 밑변)); * atan의 리턴범위가 180도 단, 부호알면 360까지 커버 가능하게 변환된 함수 atan2 사용 |
|
transform | translate, scale, rotate, matrix, skew |
translate: 이동 scale: 확대/축소 rotate: 회전 matrix: 3차원 변형 skew: 2차원 변형 |
transform: scale(0.5) translate(-100%, -100%); * 확장된 함수 matrix3d, perspective, rotate3d, rotateX, rotateY, rotateZ, translate3d, translateX, translateY, translateZ, scale3d, scaleX, scaleY, scaleZ, skewX, skewY |
|
C | numbering | counter, counters | 표, 그림, 목차 등에 순번 부여 counter-increment: 해당 속성 증가 counter-reset: 해당 속성 초기화 |
ol { counter-reset: index; list-style-type: none; } li::before { counter-increment: index; content: counters(index, '.', decimal) ' '; } |
D | image, background-image | url |
url입력을 도와줌 background-image: url(/source/canvas_drawimage.jpg); background-size:cover;/* cover = 꽉 차게, contain =원래size) */ |
|
background-image | cross-fade |
두 이미지를 합침 background-image: -webkit-cross-fade(url(/source/canvas_drawimage.jpg), url(/source/canvas-grid.png), 20%); |
||
E | color |
rgb, rgba, hsl, hsla, hwb, hwba, xyz, lab, oklab |
RGB : Red(빨강) - Green(초록) - Blue(파랑) #000 또는 #000000 HSI : Hue(색상) - Saturation(채도) - Lightness(명도) HWB : Hew(생상) - Whihtness(하양비) - Blackness(검정비) +a:opacity(투명도) |
p { color: rgb(255, 0, 0); } p { color: rgb(255, 0, 0, 128); } p { color: #F00 } p { color: #F008 } p { color: hsl(0,100%,69.6%)} p { color: hsl(0,100%,69.6%, 50%)} |
F | filter | blur, brightness, contrast, drop-shadow, grayscale, hue-rotate, invert, opacity, saturate, sepia |
blur : 흐리게 brightness: 밝기 contrast: 대조 drop-shadow: 그림자 grayscale: 회색비 hue-rotate: 색 회전 invert: 반전 opacity: 투명도 saturate: 채조 sepia: 갈색조 |
filter: bightness(150%) contrast(200%) |
G | 범용 | attr |
속성값(attribute)를 가져옴 * 예시 코드 수행 결과 World (데이터를 가져와 동적으로 사용하는 것에는 한계가 있음 -> javascript) |
<style> [my-attr]::before{ content: attr(my-attr) " "; color:red; } </style> <p my-attr="Hello">World</p> |
H | font-size | clamp | (최소값, 적용값, 최대값) | font-size:clamp(0.8em, 1vm, 1.5em) |
I | Size 등 | repeat | 반복 입력을 수행 |
1) grid-template-columns: repeat(3, 50px); 2) grid-template-columns: 50px 50px 50px; 같은 동작 수행 |
J | svg d, offset-path | path |
svg컨트롤을 함수로 가능(SVG참조)#svg_css_ex1:hover path { d: path("M20,80 L50,20 L80,80"); } |
마우스를 올려보세요. |
@rules
E_rules >> [No].htmlNo | 규칙명 | 내용 | 예시 |
---|---|---|---|
@charset | 스타일 시트에 의해 사용되는 문자 집합을 정의함 | @charset "utf-8"; | |
@import | 외부 스타일 시트를 포함함 | @import 'styles2.css'; | |
@namespace | prefix(접두어)가 붙는 것을 고려해야한다고 css엔진에 알림 |
@namespace url(http://www.w3.org/1999/xhtml); @namespace svg url(http://www.w3.org/2000/svg); |
|
A | @media | media query를 사용해 조건부 규칙을 정의 |
body { <!-- 2 줄로 배열합니다. --> column-count: 2; } @media (min-width: 500px) { body { <!-- 화면이 (모바일이라) 작으면, 1 줄로 배열합니다. :적응형 --> column-count: 1; } } |
@supports | 브라우저가 주어진 조건의 기준을 만족할 때의 규칙 |
@supports not (display: grid) { div { float: right; } } |
|
@document | 주어진 조건의 기준을 만족할 때의 규칙 | (CSS Spec lv.4) | |
@page | 인쇄 할 때의 규칙 | @page { margin: 1cm; } |
|
@font-face |
온라인 글꼴 사용 (font 단원 상세) |
@font-face{ font-family: '강원교육모두'; src: url("/강원교육모두\ Light.ttf") format('OpenType'); }폰트 파일을 열어서 format을 확인합니다. ttf, ttc, otf-> true type, woff -> woff, woff2 -> font-woff2 |
|
B | @keyframe | 애니메이션에서 단계를 지정 |
@keyframes important1 { from { margin-top: 50px; } to { margin-top: 100px; } } |
@viewport | 작은 화면에서의 규칙 | Draft(논의중) | |
C | @counter-style | 리스트를 사용자 정의 스타일로 변경 할 수 있음 |
@counter-style thumbs { system: cyclic; symbols: "\1F44D"; suffix: " "; } ul { list-style: thumbs; }
|
@color-profile | 컬러 프로파일을 불러옵니다. |
지정된 색을 사용합니다.@color-profile --swop5c { src: url("https://example.org/SWOP2006_Coated5v2.icc"); } #myColor { background-color: color(--swop5c 0% 70% 20% 0%); }와 같은 예제가 소개되고 있지만 잘 동작하지 않습니다.:root{ --myColor:#FD7751;} #myColor { background-color: var(--myColor);}<span id="myColor">지정된 색을 사용합니다.</span> |
|
@container | 조건에 맞을 때, 규칙을 설정합니다. |
@container (width > 400px) { h2 { font-size: 1.5em; } } |
|
@layer | 레이어를 구분할 수 있습니다. 레이어를 on/off하는 방법에 대해서 알려져 있지 않습니다. |
<style> @layer ly1 { #layer { font-size: 2em; color : red; } } @layer ly2 { #layer { background-color : yellow; } } @layer ly3 { #layer { text-shadow : #8888 5px 5px 5px; } } </style>레이어 테스트 입니다. |
속기
속기shorthands란 여러개의 속성을 한 번에 지정하는 것을 말합니다.
/* 개별 속성 지정 */
padding-top: 10px;
padding-right: 15px;
padding-bottom: 15px;
padding-left: 5px;
/* 속기 사용 */
padding: 10px 15px 15px 5px;
속성명 | 순서 | 예시 |
---|---|---|
animation |
animation-duration, animation-timing-function, animation-delay, animation-iteration-count, animation-direction, animation-fill-mode, animation-play-stats, animation-timeline |
animation: 3s ease-in 1s 2 reverse both paused slidein; animation: 3s linear 1s slidein; |
background | background-color, background-image, background-position, background-repeat, background-attachment |
background: red url(bg-graphic.png) 10px 10px repeat-x fixed; |
border | border-width, border-style, border-color |
border: 1px solid black |
font | font-style, font-variant, font-weight, font-stretch, font-size, line-height, font-family |
font: italic small-caps bold expanded 2em /3em '맑은 고딕' test |
margin | margin-top, margin-right, margin-bottom, margin-left |
margin: 10px 15px 15px 5px; |
padding | padding-top, padding-right, padding-bottom, padding-left |
padding: 10px 15px 15px 5px; |
list-style | list-style-type, list-style-image, list-style-position |
list-style: lower-roman url("../source/shape.png") outside; |
transition |
transition-property, dransition-duration, transition-timing-function, transition-delay |
transition: margin-left 4s ease-in-out 1s; |