본문으로 바로가기 본문 바로가기
WIKI 기술 지식 베이스

Container queries

원문 보기 위키 갱신

Container queries (컨테이너 쿼리)

Material UI는 테마 breakpoints를 기반으로 CSS 컨테이너 쿼리를 만드는 유틸리티 함수를 제공합니다.

출처: 문서

본문

사용법 (Usage)

CSS 컨테이너 쿼리를 만들려면 theme.breakpoints에서 사용할 수 있는 메서드와 함께 theme.containerQueries를 사용하세요. 값은 단위가 없거나(이 경우 픽셀로 렌더링됨), 문자열이거나, breakpoint 키일 수 있습니다. 예를 들어:

theme.containerQueries.up('sm'); // => '@container (min-width: 600px)'
{{"demo": "BasicContainerQueries.js"}}

:::info 조상 요소 중 하나에 CSS container type이 지정되어 있어야 합니다. :::

명명된 컨테이너 컨텍스트 (Named containment contexts)

컨테이너 컨텍스트를 참조하려면 모든 breakpoint 메서드에 접근하기 위해 컨테이너 이름과 함께 containerQueries 메서드를 호출하세요:

theme.containerQueries('sidebar').up('500px'); // => '@container sidebar (min-width: 500px)'

단축 문법 (Shorthand syntax)

sx prop을 사용해 스타일을 추가할 때는 @<size> 또는 @<size>/<name> 표기법을 사용해 테마를 참조하지 않고 컨테이너 쿼리를 적용하세요.

  • <size>: 너비 또는 breakpoint 키.
  • <name> (선택): 명명된 컨테이너 컨텍스트.
{{"demo": "SxPropContainerQueries.js"}}

주의사항 (Caveats)

  • 단위가 없는 값의 @ 접두사는 px로 렌더링되므로, @500은 500px과 동일합니다—하지만 @500px는 잘못된 문법이며 올바르게 렌더링되지 않습니다.

  • 숫자가 없는 @는 0px으로 렌더링됩니다.

  • 컨테이너 쿼리는 동일한 단위를 공유해야 합니다(크기는 어떤 순서로든 정의할 수 있음). 아래를 참고하세요:

    // ✅ These container queries will be sorted correctly.
    padding: {
      '@40em': 4,
      '@20em': 2,
      '@': 0,
    }
    
    // ❌ These container queries won't be sorted correctly
    //    because 40em is typically greater than 50px
    //    and the units don't match.
    padding: {
      '@40em': 4,
      '@50': 2,
      '@': 0,
    }
    

API

CSS 컨테이너 쿼리는 breakpoints API에서 사용할 수 있는 모든 메서드를 지원합니다.

// For default breakpoints
theme.containerQueries.up('sm'); // => '@container (min-width: 600px)'
theme.containerQueries.down('md'); // => '@container (max-width: 900px)'
theme.containerQueries.only('md'); // => '@container (min-width: 600px) and (max-width: 900px)'
theme.containerQueries.between('sm', 'lg'); // => '@container (min-width: 600px) and (max-width: 1200px)'
theme.containerQueries.not('sm'); // => '@container (max-width: 600px)'

더 알아보기 (Learn more)