[CSS] 20) 미디어쿼리 (#@media #DPI)

2021. 2. 8. 01:56· Web
목차
  1. DAY20

Codecademy front-end engineer course

DAY20

  • Learn Responsive Design 2) Media Queries

**When a website responds to the size of the screen it’s viewed on, it’s called a responsive website.

 

 

-Media Queries [미디어 쿼리란?]

CSS uses media queries to adapt a website’s content to different screen sizes.

-An example of media queries-

@media only screen and (max-width: 480px) {
  body {
    font-size: 12px;
  }
}
@media This keyword begins a media query rule and instructs the CSS compiler on how to parse the rest of the rule.
only screen - Indicates what types of devices should use this rule.
- screen is the media type always used for displaying content, no matter the type of device.
The only keyword is added to indicate that this rule only applies to one media type (screen).
and (max-width : 480px) the rule is called a media feature(the conditions that must be met in order to render the CSS within a media query.), and instructs the CSS compiler to apply the CSS styles to devices with a width of 480 pixels or smaller. 
another declaration CSS rules are nested inside of the media query’s curly braces. The rules will be applied when the media query is met.

 

 

- Range

By using multiple widths and heights, a range can be set for a media query.

@media only screen and (min-width: 320px) and (max-width: 480px) {
    /* ruleset for 320px - 480px */
}

-> apply its CSS rules only when the screen size is between 320 pixels and 480 pixels.

Notice the use of a second and keyword after the min-width media feature.

This allows us to chain two requirements together.

@media only screen and (min-width: 320px) { 
    /* ruleset for >= 320px */
}
 
 
@media only screen and (min-width: 480px) { 
    /* ruleset for >= 480px */
}

it can override CSS rules present in the first media query or apply additional CSS rules that are not already present in the first.

 

 

 

- Dots Per Inch (DPI)

Another media feature we can target is screen resolution. 해상도 관련

To target by resolution, we can use the min-resolution and max-resolution media features.

These media features accept a resolution value in either dots per inch (dpi) or dots per centimeter (dpc).

@media only screen and (min-resolution: 300dpi) {
    /* CSS for high resolution screens */
}

-> the example above targets high resolution screens by making sure the screen resolution is at least 300 dots per inch.

 

 

 

 

- And Operator 

we chained multiple media features of the same type in one media query by using the and operator.

The and operator can be used to require multiple media features. 

@media only screen and (max-width: 480px) and (min-resolution: 300dpi) {
    /* CSS ruleset */
}

-> By placing the and operator between the two media features, the browser will require both media features to be true before it renders the CSS within the media query. 

 

 

 

- Comma Separated List 

 media features can be separated in a comma separated list.

@media only screen and (min-width: 480px), (orientation: landscape) {
    /* CSS ruleset */
}

->In the example above, we used a comma (,) to separate multiple rules. 

  • The screen is more than 480 pixels wide
  • The screen is in landscape mode

(Note that the second media feature is orientation. The orientation media feature detects if the page has more width than height. If a page is wider, it’s considered landscape, and if a page is taller, it’s considered portrait.)

 

 

 

 

-Breakpoints [브레이크포인트]

How do we determine what queries to set? [쿼리를 어떻게 설정할까?에서부터 시작]

The points at which media queries are set are called breakpoints.

Breakpoints are the screen sizes at which your web page does not appear properly.

@media only screen and (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
    /* CSS ruleset */
}

-> if we want to target tablets that are in landscape orientation, we can create the following breakpoint above.

 

 

*However, setting breakpoints for every device imaginable would be incredibly difficult because there are many devices of differing shapes and sizes.

Rather than set breakpoints based on specific devices, the best practice is to resize your browser to view where the website naturally breaks based on its content. 

The dimensions at which the layout breaks or looks odd become your media query breakpoints. 

 

you can set media query breakpoints that create the best possible user experience on a project by project basis, rather than forcing every project to fit a certain screen size. 

Check out this list of breakpoints by device widths. Use it as a reference of screen widths to test your website 

 

@media only screen and (min-width:768px) and (max-width:1024px) and (orientation:landscape){
  .page-title, .page-description {
    float: left;
    width: 20em;
}
 
  .page-description {
    text-align: left;
}
}

 

 

 

 

** 크롬 개발자 툴에서 여러 media queries 확인하기

https://developers.google.com/web/tools/chrome-devtools/device-mode

 

Chrome DevTools에서 Device Mode로 휴대기기 시뮬레이션  |  Google Developers

Chrome의 Device Mode에서 가상 기기를 사용하여 모바일 우선 웹사이트를 빌드할 수 있습니다.

developers.google.com

 

반응형
저작자표시 비영리 변경금지 (새창열림)

'Web' 카테고리의 다른 글

[js #1] 자바스크립트를 왜 배울까?  (3) 2022.02.04
[CSS] 21) 플렉스박스 (#display:flex)  (0) 2021.02.19
[Front-end] 17) Secondary navigation: 브레드크럼  (0) 2021.02.05
[CSS] 16)링크와 버튼 (#title, #hover state)  (0) 2021.02.04
[Front-end] 15)웹페이지 텍스트 디자인 꿀팁  (0) 2021.02.01
  1. DAY20
'Web' 카테고리의 다른 글
  • [js #1] 자바스크립트를 왜 배울까?
  • [CSS] 21) 플렉스박스 (#display:flex)
  • [Front-end] 17) Secondary navigation: 브레드크럼
  • [CSS] 16)링크와 버튼 (#title, #hover state)
이티권
이티권
programming, design
이티권
ET WORLD
이티권
전체
오늘
어제
  • 분류 전체보기 (85)
    • Web (43)
      • Three.js (1)
      • javascript (6)
      • React (2)
    • algorithm (18)
      • BOJ (6)
    • Record (9)
      • 생각 (6)
      • 경험 (3)
    • Art (7)
    • Technology (8)
      • Data (2)
      • Info (5)

블로그 메뉴

  • WEB
  • RECORD
  • ALGORITHM
  • TECH
  • ART

공지사항

인기 글

태그

  • js
  • 레포지터리만들기
  • 알고리즘
  • 알고리즘문제
  • 깃허브하는법
  • 깃허브시작
  • 나머지매개변수
  • 뷰라우터
  • 순환
  • 컴퓨터공학힘든가요
  • HTML
  • ChatGPT
  • 맥북소리녹화
  • 뷰 프롭스
  • html tag
  • 스프레드문법
  • 컴포넌트 통신
  • 뷰링크
  • vue.js
  • 깃 CLI
  • 기명함수 표현식
  • 포토샵
  • 재귀함수
  • 퓨 이벤트
  • svgr
  • 페이지 링크
  • CSS
  • 불확신
  • 시니어프론트엔드개발자
  • C언어

최근 댓글

최근 글

hELLO · Designed By 정상우.v4.2.1
이티권
[CSS] 20) 미디어쿼리 (#@media #DPI)
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.