当サイトは、アフィリエイト広告を利用しています
react + emotion11におけるメディアクエリー(@media)を使い方をメモ。
/** @jsxImportSource @emotion/react */import "./styles.css";import { css } from "@emotion/react";export default function App() {return (<div className="App"><h1>sample</h1><div css={[media_min]}>min-width</div><div css={[media_max]}>max-width</div></div>);}// widthが800px以上の場合に適用const media_min = () => [css`@media (min-width: 800px) {background-color: lightgreen;}`];// widthが800px以下の場合に適用const media_max = () => [css`@media (max-width: 800px) {background-color: skyblue;}`];
codesandboxで作成したサンプル
min-widthからmax-widthの間で適用されると考えると覚えやすい