0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

メディアクエリ

Posted at

はじめに

media queryの使い方を知らなかったのでメモ

デバイスに応じてスタイルを変えれるようになる

@mediaを使って何かしらのパラメータを設定

.css
@media (max-width: 800px) {
    h1 {
        color: red;
    }
}

サンプル

.html
<!DOCTYPE html>
<html lang="ja">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Media Queries</title>
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet">

    <link rel="stylesheet" href="style.css">
</head>

<body>
    <nav>
        <a href="#home">Home</a>
        <ul>
            <li>
                <a href="#Home">Learn More</a>
            </li>
            <li>
                <a href="#Home">About</a>
            </li>
            <li>
                <a href="#Home">Contact</a>
            </li>
        </ul>
        <a href="#signup">Sign Up</a>
    </nav>
    <h1>Media Queries</h1>
</body>

</html>
.css
body {
    font-family: 'Open Sans', sans-serif;
}

h1 {
    font-size: 6rem;
    text-align: center;
}

nav {
    font-size: 1.5rem;
}

ul,li {
    display: inline;
    margin: 0;
    padding: 0;
}

@media (width: 800px) {
    h1 {
        color: red;
    }
}


/* 800以上のサイズがある時に適用 */
@media (min-width: 800px) {
    h1 {
        color: red;
    }
}

/* 800px以下のサイズの時に適用 */
@media (max-width: 800px) {
    h1 {
        color: red;
    }
}

/* 600pxから800pxの間に適用 */
@media (min-width:600px) and (max-width: 800px) {
    h1 {
        color: red;
    }
}

/* 以下の順番だと、1000pxまでの方が優先されるのでオレンジのまま */
@media (max-width:500px) {
    h1 {
        color: red;
    }
}

@media (max-width:1000px) {
    h1 {
        color: orange;
    }
}

/* 順番を入れ替えると想定通り */
@media (max-width:1000px) {
    h1 {
        color: orange;
    }
}

@media (max-width:500px) {
    h1 {
        color: red;
    }
}

/* デバイスを横に向けると背景色が変わる */
@media(orientation: landscape){
    body {
        background-color: yellow;
    }
}
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?