๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
CSS

๋ฏธ๋””์–ด ์ฟผ๋ฆฌ

by Youcodein 2022. 8. 16.
728x90
๋ฐ˜์‘ํ˜•

๋ฏธ๋””์–ด์ฟผ๋ฆฌ

๋ทฐํฌํŠธ์˜ ํ•ด์ƒ๋„์— ๋”ฐ๋ผ CSS๋ฅผ ๋ถ„๊ธฐ์‹œํ‚ค๋Š” ๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๋Š” ๋ฐ˜์‘ํ˜• ํ”„๋กœ์ ํŠธ์—์„œ ๋ฐ˜๋“œ์‹œ ํ•„์š”ํ•œ ๊ธฐ๋ฒ•์ž…๋‹ˆ๋‹ค.


1. < Link ~ media " " >

<link rel="stylesheet" type="text/css" media="all and (min-width: 1000px)" href="style_pc.css">

์ตœ์†Œ ๊ฐ€๋กœํญ์ด 1000px๋ณด๋‹ค ๋„“์€ ํ•ด์ƒ๋„์ผ ๋•Œ style_pc.css๋ฅผ ์—ฐ๊ฒฐํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

ํฌ๋กฌ ๋ธŒ๋ผ์šฐ์ €์—์„œ ๊ฐœ๋ฐœ์ž ๋„๊ตฌ๋ฅผ ์—ด๊ณ  Toggle device Toolbar๋ฅผ ๋ˆ„๋ฅด๋ฉด ๊ธฐ๊ธฐ๋งˆ๋‹ค์˜ ํ•ด์ƒ๋„๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๊ณ  ๊ธฐ๊ธฐ ๋ชจ๋ธ๋„ ์ถ”๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ๋‹ค์Œ ํŠธ๋กœ์ด (http://troy.labs.daum.net/) ์—์„œ๋Š” ๊ธฐ๊ธฐ๋ณ„ ํ•ด์ƒ๋„๋ฅผ ์ œ๊ณตํ•˜๊ณ  ์‹ค์ œ ์ž‘๋™ ํ™”๋ฉด์„ ํ™•์ธํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ชจ๋ฐ”์ผ ๊ธฐ๊ธฐ ๊ฐ€๋กœ ์„ธ๋กœ
iPhone X 375 812
iPhone 6, 7, 8 375 667
iPhone 6, 7, 8 plus 414 736
iPhone 5 320 568
Galaxy S5, S8 360 640
Galaxy Note8 360 640
iPad Pro 1024 1336
โ‹ฎ โ‹ฎ โ‹ฎ

2. @media

์ฒ˜์Œ ๋กœ๋”ฉํ•  ๋•Œ ์„ฑ๋Šฅ์ด ์ €ํ•˜๋˜์ง€ ์•Š๋„๋ก CSS ํŒŒ์ผ์„ ํ•˜๋‚˜๋กœ ๋งŒ๋“ค์–ด์„œ CSS ๋‚ด๋ถ€์—์„œ ์กฐ๊ฑด์— ๋”ฐ๋ผ ๋ถ„๊ธฐ์‹œํ‚ค๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ธ ํ˜•ํƒœ์˜ ๋ฐ˜์‘ํ˜• ์›น CSS์ž…๋‹ˆ๋‹ค.

@media all and (min-width: 1000px) {
๋ชจ๋“  ๊ธฐ๊ธฐ์—์„œ ํ•ด์ƒ๋„๊ฐ€ ์ตœ์†Œ 1000px์ธ ๊ฒฝ์šฐ ์ ์šฉํ•  ์†์„ฑ๋“ค โ€ฆ
}

all์€ Media Type์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. and ์ „, ํ›„์— ๋„์–ด ์”๋‹ˆ๋‹ค.

Media Type ์„ค๋ช…
all ๋ชจ๋“  ๋ฏธ๋””์–ด ์žฅ์น˜์—์„œ ์‚ฌ์šฉ(๊ธฐ๋ณธ๊ฐ’)
print ํ”„๋ฆฐํ„ฐ์— ์‚ฌ์šฉ
screen PC, ํƒœ๋ธ”๋ฆฟ, ์Šค๋งˆํŠธํฐ์— ์‚ฌ์šฉ
speech ์Šคํฌ๋ฆฐ๋ฆฌ๋”๊ธฐ๊ฐ€ ํŽ˜์ด์ง€๋ฅผ ์ฝ๋Š”๋ฐ ์‚ฌ์šฉ

๋ฏธ๋””์–ด ์ฟผ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ธŒ๋ผ์šฐ์ €์˜ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ ˆ์ด์•„์›ƒ์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@media only screen and (orientattion: protrait) {
     body {background: skyblue; }
}

์œ„ ๊ตฌ๋ฌธ์€ ๋ชจ๋ฐ”์ผ ๊ธฐ๊ธฐ๋ฅผ ์„ธ์›Œ ์„ธ๋กœ๊ฐ€ ๊ฐ€๋กœ๋ณด๋‹ค ๊ธด ์„ธ๋กœ๋ฐฉํ–ฅ์ด ๋˜๋ฉด ๋ฐฐ๊ฒฝ์ƒ‰์„ ํ•˜๋Š˜์ƒ‰์œผ๋กœ ์ง€์ •ํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

@media only screen and (orientattion: landscape) {
     body {background: lightgreen; } }

์œ„ ๊ตฌ๋ฌธ์€ ๋ชจ๋ฐ”์ผ ๊ธฐ๊ธฐ๋ฅผ ์„ธ์›Œ ๊ฐ€๋กœ๊ฐ€ ์„ธ๋กœ๋ณด๋‹ค ๊ธด ๊ฐ€๋กœ๋ฐฉํ–ฅ์ด ๋˜๋ฉด ๋ฐฐ๊ฒฝ์ƒ‰์„ ์—ฐ๋‘์ƒ‰์œผ๋กœ ์ง€์ •ํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์กฐ๊ฑด์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

@media only screen and (max-width: 900px) and (min-width: 600px), (min-width: 1000px) {
     body {color: red; }
}

์œ„ ๊ตฌ๋ฌธ์€ ๊ฐ€๋กœ๊ฐ€ 600px์—์„œ 900px ์‚ฌ์ด์ผ ๋•Œ, ๊ทธ๋ฆฌ๊ณ  1100px ์ด์ƒ์ผ ๋•Œ์—๋งŒ ๊ธ€์ž์ƒ‰์„ ๋นจ๊ฐ„์ƒ‰์œผ๋กœ ํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

3. media query๋ฅผ ํ‰๋‚ด๋‚ด๋Š” script

function initLayout(){
var widthWin = $(window).width();
if (widthWin < 1000 $('body').addClass('mob').removeClass('pc'));
else $('body').removeClass('mob').addClass('pc');
}

์ฐฝ์˜ ๊ฐ€๋กœ ํฌ๊ธฐ๋ฅผ ์žฌ๊ณ  1000px๋ณด๋‹ค ์ข์œผ๋ฉด body์— mobํด๋ž˜์Šค๋ฅผ ๋ถ™์ด๊ณ  pcํด๋ž˜์Šค๋Š” ์ œ๊ฑฐํ•˜๊ณ , 1000px ์ด์ƒ์ด๋ฉด body์— pcํด๋ž˜์Šค๋ฅผ ๋ถ™์ด๊ณ , mobํด๋ž˜์Šค๋Š” ์ œ๊ฑฐํ•œ๋‹ค๋Š” ๋œป์ž…๋‹ˆ๋‹ค.

$(window).resize(function(){
initLayout();
});

๋ฏธ๋ฆฌ ๋งŒ๋“ค์–ด๋‘” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ฐฝ ํฌ๊ธฐ๊ฐ€ ๋ฐ”๋€” ๋•Œ๋งˆ๋‹ค ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

โ€ฆ
body.mob { max-width: 720px }
โ€ฆ

css์—์„œ body.mob์— ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋ชจ๋ฐ”์ผ์ผ ๋•Œ์˜ ์†์„ฑ์ด ๋“ฑ๋ก๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์˜ˆ์ œ 2-75)

<!DOCTYPE html>
<html lang="ko">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>์˜ค๋ฅด์„ธ ๋ฏธ์ˆ ๊ด€</title>
    <style type="text/css">
        h1, h2, ul, li, p, div {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        li {
            list-style-type: none;
        }
        .clear:after {
            content: "";
            display: block;
            clear: both;
        }
        a {
            line-height: 1.5;
            color: #333;
        }
        body {
            background: #f1f1f1;
            font-size: 13px;
        }
        header {
            background: coral;
        }
        h1 {
            font-size: 25px;
            color: #cc0;
            padding: 10px;
            color: #fff;
        }
        .lnb li {
            margin: 10px;
            padding: 10px;
            background: lightgreen;
        }
        .content {
            padding: 20px;
        }
        h2 {
            font-size: 20px;
        }
        .ext {
            margin: 10px;
            padding: 20px;
            background: lightgreen;
        }
        footer {
            padding: 10px;
            background: #aaa;
        }

        @media all and (min-width: 768px) and (max-width: 1024px) {
            .lnb {
                float: left;
                width: 25%;
            }
            .content {
                float: left;
                width: 75%;
            }
            .aside {
                clear: both;
            }
        }

        @media all and (min-width: 1025px) {
            .wrap {
                max-width: 1280px;
                margin: 0 auto;
            }
            .lnb {
                float: left;
                width: 20%;
            }
            .content {
                float: left;
                width: 50%;
            }
            .aside {
                float: right;
                width: 30%;
            }
        }
    </style>
</head>

<body>
    <div class="wrap">
        <header>
            <h1>Musee d'Orsay</h1>
        </header>
        <div id="container" class="clear">
            <div class="lnb">
                <ul>
                    <li><a href="#">์ž‘๊ฐ€์˜ ์ž‘ํ’ˆ</a></li>
                    <li><a href="#">์ž‘๊ฐ€์˜ ์‹œ๋Œ€</a></li>
                    <li><a href="#">์ž‘๊ฐ€์˜ ์ผ์ƒ</a></li>
                </ul>
            </div>
            <div class="content">
                <h2>PICTURES</h2>
                <p>
                    <a href="#">ํ”ผ๋ฆฌ๋ถ€๋Š” ์†Œ๋…„</a><br>
                    <a href="#">๊ณ ํ์˜ ๋ฐฉ</a><br>
                    <a href="#">ํ™ฉ์ƒ‰ ๊ทธ๋ฆฌ์Šค๋„๊ฐ€ ์žˆ๋Š” ํ™”๊ฐ€์˜ ์žํ™”์ƒ</a><br>
                    <a href="#">์˜คํŽ˜๋ผ์ขŒ์˜ ๊ด€ํ˜์•…๋‹จ</a><br>
                    <a href="#">๋งŒ์ข…</a><br>
                    <a href="#">์ œ๋น„๊ฝƒ ์žฅ์‹์„ ๋‹จ ๋ฒ ํŠธ๋ฅด๋ชจ๋ฆฌ์กฐ</a><br>
                    <a href="#">ํ”ผ๋ฆฌ๋ถ€๋Š” ์†Œ๋…„</a> <br>
                    <a href="#">๊ณ ํ์˜ ๋ฐฉ</a><br>
                    <a href="#">ํ™ฉ์ƒ‰ ๊ทธ๋ฆฌ์Šค๋„๊ฐ€ ์žˆ๋Š” ํ™”๊ฐ€์˜ ์žํ™”์ƒ</a><br>
                    <a href="#">์˜คํŽ˜๋ผ์ขŒ์˜ ๊ด€ํ˜์•…๋‹จ</a><br>
                    <a href="#">๋งŒ์ข…</a><br>
                    <a href="#">์ œ๋น„๊ฝƒ ์žฅ์‹์„ ๋‹จ ๋ฒ ํŠธ๋ฅด๋ชจ๋ฆฌ์กฐ</a>
                </p>
            </div>
            <div class="aside">
                <ul class="ext">
                    <li>
                        <h2>Workshop Go</h2><a href="#">Life drawing workshop</a>
                    </li>
                    <li>
                        <h2>Summer Exhibition</h2>
                        <a href="#">A-level Summer Exhibition Online 2010</a>
                    </li>
                    <li>
                        <h2>RA Collection</h2>
                        <a href="#">RA Collections</a>
                    </li>
                </ul>
            </div>
        </div>
    </div>
    <footer>
        <p>©2018 Les Amis du Musee d'Orsay. All rights reserved.</p>
    </footer>
</body>

</html>
๊ฒฐ๊ณผ ๋ณด๊ธฐ

See the Pen 2-75 by HwangYoujin (@youJin2) on CodePen.

728x90
๋ฐ˜์‘ํ˜•

'CSS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์ด๋ฏธ์ง€ ํ‘œํ˜„  (3) 2022.08.22
Scss๋กœ ๊ฐ•์•„์ง€ ๋งŒ๋“ค๊ธฐ  (7) 2022.08.18
Scss  (5) 2022.08.18
CSS ๊ธฐ๋ณธ ๊ทœ์น™  (4) 2022.08.15
01. ๋น„ํŠธ๋งต๊ณผ ๋ฒกํ„ฐ  (9) 2022.08.10

๋Œ“๊ธ€