LoginSignup
2
1

More than 5 years have passed since last update.

Material UI の theme を使って styled-components 内で media query を指定する

Last updated at Posted at 2019-03-25

styled-components の中で breakpoints 指定する際の簡単な方法です。
Material UI の theme を使用する前提です。

const ModalCard = styled.div`
  background: white;
  width: 600px;
  position: absolute;
  left: calc((100% - 600px) / 2);
  ${theme.breakpoints.down('xs')} {
    width: 100%;
    left: 0;
  }

解説

theme.breakpoints の返り値が css に直接使える API になっています。
https://material-ui.com/layout/breakpoints/#api

theme.breakpoints.down('xs')
// @media (max-width: 599.95px)

なので以下のような css が出力されます。

@media (max-width: 599.95px)
.jJmNcV {
}
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1