๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ปSTUDY/CSS

[CSS] @media query

๋ฐ˜์‘ํ˜•

[CSS] @media query

๋ฏธ๋””์–ด ์ฟผ๋ฆฌ, @media query ์ดํ•ดํ•˜๊ธฐ

  • ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๋Š” ๋‹จ๋ง๊ธฐ ์œ ํ˜•(์ถœ๋ ฅ๋ฌผ vs ํ™”๋ฉด)๊ณผ, ์–ด๋–ค ํŠน์„ฑ์ด๋‚˜ ์ˆ˜์น˜(ํ™”๋ฉด ํ•ด์ƒ๋„, *๋ทฐํฌํŠธ ๋„ˆ๋น„ ๋“ฑ)์— ๋”ฐ๋ผ ์›น์‚ฌ์ดํŠธ๋‚˜ ์•ฑ์˜ ์Šคํƒ€์ผ์„ ์ˆ˜์ •ํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

    *๋ทฐํฌํŠธ(viewport) :: ํ˜„์žฌ ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๊ณ  ์žˆ๋Š” ๋‹ค๊ฐํ˜•(๋ณดํ†ต ์ง์‚ฌ๊ฐํ˜•)์˜ ์˜์—ญ. ์›น๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” ํ˜„์žฌ ์ฐฝ์—์„œ ๋ฌธ์„œ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„(์ „์ฒดํ™”๋ฉด์ด๋ผ๋ฉด ํ™”๋ฉด์ „์ฒด)์„ ๋งํ•œ๋‹ค. ๋ทฐํฌํŠธ ์ค‘์—์„œ๋„ ์ง€๊ธˆ ๋ณผ ์ˆ˜ ์žˆ๋Š” ๋ถ€๋ถ„์„ ์‹œ๊ฐ์ ๋ทฐํฌํŠธ(visual viewport)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์Šค๋งˆํŠธํฐ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ํ™”๋ฉด์„ ํ™•๋Œ€ํ–ˆ์„ ๋•Œ์™€ ๊ฐ™์€ ํŠน์ • ์ƒํ™ฉ์—์„œ ๋ ˆ์ด์•„์›ƒ ๋ทฐํฌํŠธ์˜ ํฌ๊ธฐ๋Š” ๋ณ€ํ•˜์ง€ ์•Š์ง€๋งŒ ์‹œ๊ฐ์  ๋ทฐํฌํŠธ๋Š” ๋” ์ž‘์•„์ง„๋‹ค.

 

1. ๋ฏธ๋””์–ด์ฟผ๋ฆฌ ์‚ฌ์šฉ๋ฒ•

1-1) CSS ์ฝ”๋“œ ๋‚ด๋ถ€์—์„œ ๋ถ„๊ธฐํ•˜๋Š” ๋ฐฉ๋ฒ•.

@media only all and (์กฐ๊ฑด๋ฌธ) { ์‹คํ–‰๋ฌธ }

 

1-2) CSS ์ฝ”๋“œ ์™ธ๋ถ€์—์„œ ๋ถ„๊ธฐํ•˜๋Š” ๋ฐฉ๋ฒ•.

  • ์กฐ๊ฑด๋ฌธ์— ๋”ฐ๋ผ ๋ณ„๋„์˜ ์™ธ๋ถ€ CSS ํŒŒ์ผ์„ ์ฐธ์กฐํ•˜์—ฌ ๋ถ„๊ธฐํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์ด ๋ฐฉ์‹์€ ์„ฑ๋Šฅ ์ตœ์ ํ™” ์ธก๋ฉด์—์„œ ๊ถŒ์žฅํ•˜์ง€ ์•Š๋Š”๋‹ค.

    ๋ฐ์Šคํฌํƒ‘ ๋ธŒ๋ผ์šฐ์ € ์‚ฌ์šฉ์ž๊ฐ€ ์–ธ์ œ๋“  ์กฐ๊ฑด์„ ๋ณ€๊ฒฝ(์˜ˆ๋ฅผ ๋“ค๋ฉด ์ฐฝํฌ๊ธฐ๋ฅผ ์กฐ์ ˆํ•ด์„œ ํ•ด์ƒ๋„๋ฅผ ๋ฐ”๊ฟˆ)ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์›น ๋ธŒ๋ผ์šฐ์ €๋Š” ์กฐ๊ฑด์— ๊ด€๊ณ„์—†์ด A.css ํŒŒ์ผ๊ณผ B.css ํŒŒ์ผ์„ ํ•ญ์ƒ ์š”์ฒญํ•œ๋‹ค. HTTP ์š”์ฒญ์„ ๋ถˆํ•„์š”ํ•˜๊ฒŒ ๋‘ ๋ฒˆ ๋ฐœ์ƒ์‹œ์ผœ ์ด ํŽ˜์ด์ง€๋ฅผ ์ฒ˜์Œ ๋กœ๋”ฉํ•˜๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ๋Š” ์„ฑ๋Šฅ ์ €ํ•˜์˜ ์›์ธ์ด ๋œ๋‹ค. CSSsํŒŒ์ผ์€ ํ•˜๋‚˜๋กœ ๋ณ‘ํ•ฉํ•˜๊ณ , CSS ์ฝ”๋“œ ๋‚ด๋ถ€์—์„œ ์กฐ๊ฑด ๋ถ„๊ธฐํ•˜๋Š” ๋ฐฉ์‹์„ ๊ถŒ์žฅํ•œ๋‹ค.

<link rel="stylesheet" media="all and (์กฐ๊ฑดA)" href="A.css" />
<link rel="stylesheet" media="all and (์กฐ๊ฑดB)" href="B.css" />

 

2. ๋ฏธ๋””์–ด์ฟผ๋ฆฌ ์‚ดํŽด๋ณด๊ธฐ.

  • @media :: ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๊ฐ€ ์‹œ์ž‘๋จ์„ ์„ ์–ธ.

2-1) ์—ฐ์‚ฐ์ž ํƒ€์ž…

and | not | only | ,

  • and :: ์—ฌ๋Ÿฌ ๋ฏธ๋””์–ด ํŠน์ง•๋“ค์„ ํ•˜๋‚˜๋กœ ๊ฒฐํ•ฉ.
  • not :: ์ „์ฒด ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๋ฅผ ๋ถ€์ •ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
  • only :: ๋ฏธ๋””์–ด์ฟผ๋ฆฌ๋ฅผ ์ธ์‹ํ•˜์ง€ ๋ชปํ•˜๋Š” ๋ธŒ๋ผ์šฐ์ €(์ •ํ™•ํžˆ legacy user agents)์—์„œ ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๋ฅผ ์ˆจ๊ธฐ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ.
  • , (or) :: ์—ฌ๋Ÿฌ ๋ฏธ๋””์–ด ํŠน์ง•๋“ค์„ ํ•˜๋‚˜๋กœ ๊ฒฐํ•ฉ.

 

not์ด๋‚˜ only๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ฏธ๋””์–ด ํƒ€์ž…์„ ๊ทœ์ •ํ•ด์•ผ ํ•œ๋‹ค.

๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ณ„ํ•˜์ง€ ์•Š๋Š”๋‹ค.

 

[์˜ˆ์ œ]
  1. ๊ฐ€์žฅ ๋‹จ์ˆœํ•œ ํ˜•ํƒœ์˜ ๋ฏธ๋””์–ด์ฟผ๋ฆฌ
@media (min-width: 700px) {}

์œ„ ๊ตฌ๋ฌธ์—์„œ ๋ฏธ๋””์–ด ํƒ€์ž…์ด ์ƒ๋žต๋˜์—ˆ์ง€๋งŒ, ๋ฏธ๋””์–ด ํƒ€์ž…์˜ ๊ธฐ๋ณธ๊ฐ’์€ all์ด๋‹ค.

์•„๋ž˜์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์€ ๋ช…๋ น์ด๋‹ค.

@media only and (min-width: 700px) {}
/* ๋ชจ๋“  ์œ ํ˜•์˜ ์žฅ์น˜์ด๋ฉฐ ์ตœ์†Œ๋„ˆ๋น„ 700px์ผ ๋–„ ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜๊ฒ ๋‹ค๋Š” ์˜๋ฏธ. */

 

  1. and ์‚ฌ์šฉ.

    ์ƒˆ๋กœ์šด ๋ฏธ๋””์–ด ํŠน์ง•๋“ค์„ ์ถ”๊ฐ€ํ•  ๋•Œ๋งˆ๋‹ค and์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

@media (min-width: 700px) and (orientation: portrait) {}
/* ๋ชจ๋“  ์œ ํ˜•์˜ ์žฅ์น˜์ด๋ฉฐ ์ตœ์†Œ ๋„ˆ๋น„ 700px์ด์ƒ์ด๋ฉฐ, ๋ฐฉํ–ฅ์ด ์„ธ๋กœ๋ชจ๋“œ์ผ ๋•Œ๋งŒ ์ ์šฉ. */
@media print and (min-width: 700px) and (orientation: landscape) {}
/* ํ”„๋ฆฐํŠธ ์žฅ์น˜์ด๋ฉฐ ์ตœ์†Œ๋„ˆ๋น„๊ฐ€ 700px์ด์ƒ์ด๋ฉฐ, ๋ฐฉํ–ฅ์ด ๊ฐ€๋กœ์ผ ๋•Œ ์ น์šฉ. */

 

  1. , ์‰ผํ‘œ ์‚ฌ์šฉ. , ์‰ผํ‘œ๋Š” ๊ฐ๊ฐ ๊ฐœ๋ณ„ ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๋กœ ์ธ์‹ํ•œ๋‹ค.
@media (min-width: 700px), print and (orientation: landscape) {}
/* ๋ชจ๋“  ์žฅ์น˜์—์„œ ์ตœ์†Œ๋„ˆ๋น„ 700px์ด์ƒ์ผ ๋•Œ ์ ์šฉํ•˜๊ฑฐ๋‚˜, ํ”„๋ฆฐํŠธ ์žฅ์น˜์—์„œ๋Š” ๊ฐ€๋กœ๋ฐฉํ–ฅ์ผ ๋•Œ๋งŒ ์ ์šฉ. */

 

  1. not ์‚ฌ์šฉ. not์€ ์ „์ฒด ๋ฏธ๋””์–ด์ฟผ๋ฆฌ๋ฅผ ์ˆ˜์‹ํ•œ๋‹ค. ์ฆ‰, not์€ all and (color)๋ฅผ ํฌํ•จํ•˜์—ฌ ๋ถ€์ •ํ•จ.
@media not all (color) {}
/* === @media not (all and (color)){} ์™€ ๊ฐ™์€ ์˜๋ฏธ. */
/* ๋ชจ๋“  ์ƒ‰์ƒ ์žฅ์น˜์—์„œ ์ด ์Šคํƒ€์ผ์„ ์ ์šฉํ•˜์ง€ ์•Š๊ฒ ๋‹ค. */

์‰ผํ‘œ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ์‚ฌ์šฉํ•  ๋•Œ, ๋ฏธ๋””์–ด ๊ตฌ๋ฌธ์€ ๊ฐœ๋ณ„ ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๋กœ ์ธ์‹ํ•˜๋ฏ€๋กœ not์€ ์‰ผํ‘œ ์ดํ›„์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๋Š”๋‹ค.

@media not screen and (color), print and (color) {}
/* ๋ชจ๋“  ์Šคํฌ๋ฆฐ ์ƒ‰์ƒ ์žฅ์น˜์—์„œ ์ ์šฉํ•˜์ง€ ์•Š๊ฑฐ๋‚˜, ํ”„๋ฆฐํŠธ ์ƒ‰์ƒ์žฅ์น˜์—์„œ ์ ์šฉ. */

 

  1. only ์‚ฌ์šฉ.

    ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ ์ง€์›ํ•˜๋Š” ์‚ฌ์šฉ์ž ์—์ด์ „ํŠธ๋งŒ ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ ๊ตฌ๋ฌธ์„ ํ•ด์„ํ•˜๋ผ๋Š” ๋ช…๋ น. ์ƒ๋žต๊ฐ€๋Šฅํ•˜๋‹ค.

     

2-2) media type(๋ฏธ๋””์–ด์ข…๋ฅ˜).

all | print | speech | screen

  • all :: ๊ธฐ๋ณธ๊ฐ’. ๋ชจ๋“  ๋ฏธ๋””์–ด ์žฅ์น˜์— ์‚ฌ์šฉ.
  • print :: ํ”„๋ฆฐํ„ฐ์— ์‚ฌ์šฉ.
  • screen :: ์ปดํ“จํ„ฐ ์Šคํฌ๋ฆฐ, ํ…Œ๋ธ”๋ฆฟ, ์Šค๋งˆํ‹ฐํฐ ๋“ฑ.
  • speech :: ํŽ˜์ด์ง€๋ฅผ ์ฝ์–ด์ฃผ๋Š” ํ™”๋ฉด ๋‚ญ๋…๊ธฐ.
  • ๋ฏธ๋””์–ด ์ข…๋ฅ˜ ์ค‘ ํ‡ดํ™”๋œ ๊ฒƒ :: aural , baille, embossed, handheld, projection, tty, tv

 

 

2-3) media feature(๋ฏธ๋””์–ด ํŠน์ง•).

width | height | min-width | min-height | max-width | max-height | device-width | device-height | min-device-width | min-device-height | max-device-width | max-device-height | aspect-ratio(min- / max- ์ ‘๋‘์–ด ์‚ฌ์šฉ๊ฐ€๋Šฅ) | divice-aspect-ratio (min- / max- ์ ‘๋‘์–ด ์‚ฌ์šฉ๊ฐ€๋Šฅ) | color(min- / max- ์ ‘๋‘์–ด ์‚ฌ์šฉ๊ฐ€๋Šฅ) | color-index (min- / max- ์ ‘๋‘์–ด ์‚ฌ์šฉ๊ฐ€๋Šฅ) | monochrome (min- / max- ์ ‘๋‘์–ด ์‚ฌ์šฉ๊ฐ€๋Šฅ) | resoluction (min- / max- ์ ‘๋‘์–ด ์‚ฌ์šฉ๊ฐ€๋Šฅ) | scan | grid

 

  • width :: ํ™”๋ฉด ์˜์—ญ ๋„ˆ๋น„(๋ธŒ๋ผ์šฐ์ € ์ฐฝ ๊ฐ™์€)
@media screen and (min-width: 500px) and (max-width: 800px) {}
/* ์Šคํฌ๋ฆฐ ์žฅ์น˜ ์ตœ์†Œ ํ™”๋ฉด์ด 500px๋ณด๋‹ค ํฌ๊ณ  800px๋ณด๋‹ค ์ž‘์„ ๋•Œ ์Šคํƒ€์ผ ์ ์šฉ. */
  • height :: ํ™”๋ฉด ์˜์—ญ ๋†’์ด
  • device-width :: ์ถœ๋ ฅ ์žฅ์น˜์˜ ๋„ˆ๋น„. ( ์ปดํ“จํ„ฐ ์Šคํฌ๋ฆฐ ๊ฐ™์€)
@media screen and (max-device-width: 450px) {}
/* ์žฅ์น˜ ๋„ˆ๋น„๊ฐ€ 450px๋ณด๋‹ค ์ž‘์„ ๋•Œ ์Šคํƒ€์ผ ์ ์šฉ. */
  • device-height :: ์ถœ๋ ฅ ์žฅ์น˜์˜ ๋†’์ด
  • aspect-ratio :: ํ™”๋ฉด ์˜์—ญ์˜ ๊ฐ€๋กœ ์„ธ๋กœ ๋น„ ๊ธฐํ˜ธ /๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ์•ž์—๋Š” ์ˆ˜ํ‰ ํ”ฝ์…€๋น„์œจ, ๋’ค์—๋Š” ์ˆ˜์ง ํ”ฝ์…€๋น„์œจ (์–‘์ˆ˜์ด๋ฉฐ ์ •์ˆ˜).
@media screen and (min-aspect-ratio: 1/1) {}
/* 
 * ๊ฐ€๋กœ ํ™”๋ฉด ๋น„๊ฐ€ 1:1 ์ด์ƒ์ผ ๋•Œ ์ ์šฉ. 
	 ์ฆ‰, ํ™”๋ฉด์ด ์ง์‚ฌ๊ฐ์ด๋‚˜ ์„ธ๋กœ์ผ ๋•Œ๋งŒ ์ ์šฉ.
*/
  • device-aspect-ratio :: ์ถœ๋ ฅ ์žฅ์น˜์˜ ๊ฐ€๋กœ์„ธ๋กœ ๋น„.(์ˆ˜ํ‰ํ”ฝ์…€ ๋น„ / ์ˆ˜์งํ”ฝ์…€ ๋น„)
@media screen and (device-aspect-ratio: 16/9) {}
/*
 * ์žฅ์น˜ ๊ฐ€๋กœ ์„ธ๋กœ๋น„๊ฐ€ 16:9์ผ ๋•Œ ์ ์šฉ.
*/
  • color :: ์ถœ๋ ฅ ์žฅใ…‡์น˜์˜ ์ƒ‰์ƒ ๊ตฌ์„ฑ์š”์†Œ ๋‹น ๋น„ํŠธ ์ˆ˜ (์žฅ์น˜๊ฐ€ ์ƒ๊น” ์žฅ์น˜๊ฐ€ ์•„๋‹ˆ๋ฉด ๊ฐ’์€ 0)`
@media all and (color) {}
/* ๋ชจ๋“  ์ƒ‰์ƒ ์žฅ์น˜์— ์ ์šฉ. */

@media all and (min-color:4) {}
/* ์ƒ‰์ƒ ๊ตฌ์„ฑ์š”์†Œ๋‹น ์ตœ์†Œ 4๋น„ํŠธ๋ฅผ ์ง€๋‹Œ ์žฅ์น˜์— ์ ์šฉ. */ 
  • color-index :: ์žฅ์น˜๊ฐ€ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๋Š” ์ƒ‰์ƒ ์ˆ˜.
@media all and (min-color-index: 256) {}
/* ์ตœ์†Œ 256 ์ƒ‰์ƒ์„ ์ง€๋‹Œ ๋ชจ๋“  ์žฅ์น˜์— ์ ์šฉ. */
  • grid :: ์ถœ๋ ฅ ์žฅ์น˜๊ฐ€ ๊ทธ๋ฆฌ๋“œ ์žฅ์น˜ ๋˜๋Š” ๋น„ํŠธ๋ฑ ์žฅ์น˜๋ƒ์— ๋”ฐ๋ผ ๊ฒฐ์ •.

    ์žฅ์น˜๊ฐ€ ๊ทธ๋ฆฌ๋“œ ๊ธฐ๋ฐ˜์ด๋ฉด ๊ฐ’์€ 1, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด 0

  • monochrome :: ํ‘๋ฐฑ(ํšŒ์ƒ‰ํ†ค)์žฅ์น˜์— ์ƒ‰์ƒ ๋‹น ๋น„ํŠธ ์ˆ˜. ์žฅ์น˜๊ฐ€ ํ‘๋ฐฑ์ด ์•„๋‹ˆ๋ฉด ๊ฐ’์€ 0

@media all and (monochrome) {}
/* ๋ชจ๋“  ํ‘๋ฐฑ์žฅ์น˜์— ์ ์šฉ. */
  • orientation :: ํ™”๋ฉด์ด ๊ฐ€๋กœ๋ชจ๋“œ ์ธ์ง€, ์„ธ๋กœ๋ชจ๋“œ ์ธ์ง€ ์ง€์ •.
@media all and (orientation: portrait) {}
/* ์˜ค์ง ์„ธ๋กœ ๋ฐฉํ–ฅ์—์„œ๋งŒ ์ ์šฉ. */
  • resolution :: ์ถœ๋ ฅ ์žฅ์น˜์˜ ํ•ด์ƒ๋„ ํ•ด์ƒ๋„๋Š” dpi(dots per inch)๋‚˜, dpcm(dots per cintimeter)๋กœ ์ง€์ •.
@media screen and (min-resolution: 300dpi) {}
/* ์ตœ์†Œ 300dpi์ด์ƒ ํ•ด์ƒ๋„๋ฅผ ์ง€๋‹Œ ์žฅ์น˜์— ์ ์šฉ. */

 

 

 

๋ฐ˜์‘ํ˜•