λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
κ°œμΈκ³΅λΆ€/HTML5 + CSS3

[HTML+CSS][λ°˜μ‘ν˜• μ›Ή] 03 - κ°€λ³€ λ ˆμ΄μ•„μ›ƒκ³Ό κ°€λ³€ μš”μ†Œ

by πŸ‡λ°•λ΄‰λ΄‰πŸ‡ 2020. 8. 4.

 

 

[HTML+CSS][λ°˜μ‘ν˜• μ›Ή] 02 - κ°€λ³€ κ·Έλ¦¬λ“œ λ ˆμ΄μ•„μ›ƒ

[HTML+CSS][λ°˜μ‘ν˜• μ›Ή] 01 - λ°˜μ‘ν˜• μ›Ήκ³Ό 뷰포트(viewport)의 κ°œλ… [HTML+CSS][CSS3와 μ• λ‹ˆλ©”μ΄μ…˜] 03 - νŠΈλžœμ§€μ…˜ [HTML+CSS][CSS3와 μ• λ‹ˆλ©”μ΄μ…˜] 02 - λ³€ν˜•κ³Ό κ΄€λ ¨λœ 속성듀 [HTML+CSS][CSS3와 μ• λ‹ˆλ©”μ΄μ…˜] 01 -..

onelight-stay.tistory.com

 

κ°€λ³€ κ·Έλ¦¬λ“œ λ ˆμ΄μ•„μ›ƒμ— λŒ€ν•΄ λ°°μ› μœΌλ‹ˆ

λ ˆμ΄μ•„μ›ƒμ΄ 변경될 λ•Œμ—λ„ λ‹€λ₯Έ μš”μ†Œλ“€λ„

μžμ—°μŠ€λŸ½κ²Œ ν‘œν˜„ 될 수 있게 ν•˜λŠ” κ°€λ³€ μš”μ†Œλ“€μ— λŒ€ν•΄μ„œ

μ•Œμ•„λ³΄λ„λ‘ ν•˜μž.

 

 


 

 

κ°€λ³€ κΈ€κΌ΄

 

λ°˜μ‘ν˜• μ›Ή λ””μžμΈμ„ μœ„ν•΄ κ°€λ³€ κ·Έλ¦¬λ“œ λ ˆμ΄μ•„μ›ƒ μ‚¬μš© μ‹œ

κΈ€μž 크기도 μœ λ™μ μœΌλ‘œ λ°”λ€ŒλŠ” 것이닀.

 

em λ‹¨μœ„

λΆ€λͺ¨ μš”μ†Œμ—μ„œ μ§€μ •ν•œ 폰트의 λŒ€λ¬Έμž M의 λ„ˆλΉ„λ₯Ό

1em으둜 μ§€μ •ν•œ κ²ƒμœΌλ‘œ

1em은 16px이닀.

 

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>em λ‹¨μœ„ μ‚¬μš©ν•˜κΈ°</title>
    <style>
        #wrapper {
            width: 96%;
            margin: 0 auto;
        }
        header { 
            width: 100%;
            height: 120px;
            background-color: skyblue;
            border-bottom: 2px solid black;
        }
        .header-text { /*emλ‹¨μœ„λ‘œ λ³€κ²½*/
            font-size: 2em;
            color: white;
            text-align: center;
            line-height: 120px;
        }
        .content { /*emλ‹¨μœ„λ‘œ λ³€κ²½*/
            float: left;
            width: 62.5%;
            height: 400px;
            padding: 1.5625%;
            background-color: yellow;
            font-size: 1.5em;
            text-align: center;
            line-height: 380px;
        }
        .right-side { /*emλ‹¨μœ„λ‘œ λ³€κ²½*/
            float: right;
            width: 31.25%;
            height: 400px;
            padding: 1.5625%;
            background-color: greenyellow;
            font-size: 1.5em;
            text-align: center;
            line-height: 380px;
        }
        footer { /*emλ‹¨μœ„λ‘œ λ³€κ²½*/
            clear: both;
            width: 100%;
            height: 120px;
            background-color: darkred;
            font-size: 1.5em;
            text-align: center;
            line-height: 120px;
        }
    </style>
</head>
<body>
    <div id="wrapper">
        <header>
            <h1 class="header-text">κ°€λ³€ κ·Έλ¦¬λ“œ λ ˆμ΄μ•„μ›ƒ</h1>
        </header>
        <section class="content">
            <h4>λ³Έλ¬Έ</h4>
        </section>
        <aside class="right-side">
            <h4>μ‚¬μ΄λ“œλ°”</h4>
        </aside>
        <footer>
            <h4>ν‘Έν„°</h4>
        </footer>
    </div>
</body>
</html>

 

rem λ‹¨μœ„

 

emλ‹¨μœ„μ˜ 단점

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>em λ‹¨μœ„μ˜ 단점</title>
    <style>
        #wrapper {font-size: 12px;}
        .header-text {font-size: 2em;}
        .fluid-text {font-size: 1.5em;}
    </style>
</head>
<body>
    <div id="wrapper">
        <header class="header-text"> <!--λΆ€λͺ¨μš”μ†Œ 12px 2em=24px-->
            <p> κ°€λ³€ κ·Έλ¦¬λ“œ λ ˆμ΄μ•„μ›ƒ </p>
            <p class="fluid-text"> κ°€λ³€ 폰트 </p><!--λΆ€λͺ¨μš”μ†Œ 24px 1.5em=36px-->
        </header>
    </div>
</body>
</html>

em λ‹¨μœ„λŠ” λΆ€λͺ¨ μš”μ†Œμ˜ 글꼴을 κΈ°μ€€μœΌλ‘œ ν•˜κΈ° λ•Œλ¬Έμ—

μ€‘μ²©λœ λΆ€λͺ¨ μš”μ†Œμ˜ κΈ€μž 크기의 영ν–₯을 λ°›μ•„

λΆ€λͺ¨ μš”μ†Œμ˜ 크기에 따라 μžμ‹ μš”μ†Œμ˜ κΈ€μžν¬κΈ°κ°€ λ°”λ€Œλ©΄μ„œ

emμˆ˜μΉ˜κ°€ 계속 λ‹¬λΌμ§„λ‹€λŠ” 단점을 가지고 μžˆλ‹€.

 

rem λ‹¨μœ„ μ‚¬μš©

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>rem λ‹¨μœ„ μ‚¬μš©</title>
    <style>
        #wrapper {font-size: 12px;}
        .header-text {font-size: 2rem;}
        .fluid-text {font-size: 1.5rem;}
    </style>
</head>
<body>
    <div id="wrapper">
        <header class="header-text"> <!--λ£¨νŠΈμš”μ†Œ 16px 2em=32px-->
            <p> κ°€λ³€ κ·Έλ¦¬λ“œ λ ˆμ΄μ•„μ›ƒ </p>
            <p class="fluid-text"> κ°€λ³€ 폰트 </p><!--λ£¨νŠΈμš”μ†Œ 16px 1.5em=24px-->
        </header>
    </div>
</body>
</html>

이에 rem은 emκ³Όλ©΄ 달리 κΈ°λ³Έ 크기λ₯Ό μ§€μ •ν•˜κΈ° λ•Œλ¬Έμ—

쀑간에 기본값이 λ°”λ€Œμ§€ μ•ŠλŠ”λ‹€.

 

 


 

 

κ°€λ³€ 이미지

 

μ΄λ―Έμ§€λŠ” μ²˜μŒλΆ€ν„° 크기가 μ •ν•΄μ Έ μžˆμ–΄

λΈŒλΌμš°μ € 창의 크기가 변해도 λ„ˆλΉ„ 값은 λ³€ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ—

λ””μžμΈμ˜ λͺ¨μŠ΅μ„ ν•΄μΉ  수 μžˆμœΌλ―€λ‘œ

λΈŒλΌμš°μ €μ˜ 화면에 따라 μœ λ™μ μœΌλ‘œ 이비지가 λ°”λ€” 수 μžˆλ„λ‘

κ°€λ³€ μ΄λ―Έμ§€λ‘œ μ„€μ •ν•΄ μ£Όμ–΄μ•Ό ν•œλ‹€.

 

CSS μ΄μš©ν•˜κΈ°

이미지가 감싸고 μžˆλŠ” λΆ€λͺ¨ μš”μ†Œλ§ŒνΌλ§Œ μ»€μ§€κ±°λ‚˜ μž‘μ•„μ§ˆ 수 μžˆλ„λ‘

max-width 속성 값을 100%둜 지정해 μ€€λ‹€.

 

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>κ°€λ³€ 이미지 μ‚¬μš©ν•˜κΈ°</title>
    <style>
        #wrapper {
            width: 96%;
            margin: 0 auto;
        }
        header { 
            width: 100%;
            height: 120px;
            background-color: skyblue;
            border-bottom: 2px solid black;
        }
        .header-text { 
            font-size: 2em;
            color: white;
            text-align: center;
            line-height: 120px;
        }
        .content { 
            float: left;
            width: 62.5%;
            height: 400px;
            padding: 1.5625%;
            background-color: yellow;
            font-size: 1.5em;
        }
        .right-side { 
            float: right;
            width: 31.25%;
            height: 400px;
            padding: 1.5625%;
            background-color: greenyellow;
            font-size: 1.5em;
            text-align: center;
            line-height: 380px;
        }
        footer { 
            clear: both;
            width: 100%;
            height: 120px;
            background-color: darkred;
            font-size: 1.5em;
            text-align: center;
            line-height: 120px;
        }
        .content img { /*max-width : 100% 지정*/
            max-width: 100%;
            height: auto;
        }
    </style>
</head>
<body>
    <div id="wrapper">
        <header>
            <h1 class="header-text">κ°€λ³€ κ·Έλ¦¬λ“œ λ ˆμ΄μ•„μ›ƒ</h1>
        </header>
        <section class="content">
            <h4>λ³Έλ¬Έ</h4>
            <img src="images/fireworks.png">
        </section>
        <aside class="right-side">
            <h4>μ‚¬μ΄λ“œλ°”</h4>
        </aside>
        <footer>
            <h4>ν‘Έν„°</h4>
        </footer>
    </div>
</body>
</html>

 

<img> νƒœκ·Έμ™€ srcset 속성

첫번째 방법을 μ‚¬μš©ν•΄λ„ λ˜μ§€λ§Œ

λ§Œμ•½ 고해상도 이미지λ₯Ό 크기만 쀄여 λͺ¨λ°”일에 ν‘œμ‹œν•˜λ©΄

큰 파일 μ‚¬μ΄μ¦ˆλ‘œ 인해 λͺ¨λ°”μΌμ—μ„œ λ‹€μš΄λ‘œλ“œν•˜λŠ”λ° μ‹œκ°„μ΄ 였래 걸리며,

ν…μŠ€νŠΈκ°€ ν¬ν•¨λœ 이미지일 경우

λͺ¨λ°”일 화면에 맞게 쀄여 ν‘œμ‹œν•˜λ©΄

ν…μŠ€νŠΈ λ‚΄μš©μ„ μ•Œμ•„λ³΄κΈ° νž˜λ“€λ‹€λŠ” 단점이 있기 λ•Œλ¬Έμ—

<img> νƒœκ·Έμ—μ„œ srcset 속성을 μ‚¬μš©ν•΄

ν™”λ©΄ λ„ˆλΉ„ κ°’μ΄λ‚˜ ν”½μ…€ 밀도에 따라

κ³  ν•΄μƒλ„μ˜ 이미지 νŒŒμΌμ„ 지정해 μ‚¬μš©ν•  수 μžˆλ‹€.

 

<picture> νƒœκ·Έμ™€ <source> νƒœκ·Έ

- μƒν™©λ³„λ‘œ λ‹€λ₯Έ μ΄λ―Έμ§€ν‘œμ‹œ-

<picture> νƒœκ·Έμ™€ <source> νƒœκ·Έλ₯Ό ν•¨κ»˜ μ‚¬μš©ν•΄ 

ν™”λ©΄ ν•΄μƒλ„λΏλ§Œ μ•„λ‹ˆλΌ ν™”λ©΄ λ„ˆλΉ„μ— 따라

λ‹€λ₯Έ 이미지 νŒŒμΌμ„ ν‘œμ‹œν•  μˆ˜ μžˆλ‹€.

 

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ν™”λ©΄ 해상도와 ν™”λ©΄ λ„ˆλΉ„μ— 따라 이미지 지정</title>
</head>
<body>
    <picture>
		<source srcset="images/shop-large.jpg" media="(min-width:1024px)">
		<source srcset="images/shop-medium.jpg" media="(min-width:768px)">
		<source srcset="images/shop-small.jpg" media="(min-width:320px)">
		<img src="images/shop.jpg" alt="fill with coffee" style="width:100%;">
	</picture>
</body>
</html>

 

 

 


 

 

κ°€λ³€ λΉ„λ””μ˜€

 

CSSλ₯Ό μ‚¬μš©ν•΄ λ§ˆμ°¬κ°€μ§€λ‘œ max-width속성을 100%둜 지정해

크기λ₯Ό μ‘°μ ˆν•  수 있게 ν•œλ‹€.

 

 

<!DOCTYPE html>
<html lang="ko">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>κ°€λ³€λΉ„λ””μ˜€</title>
    <style>
        video {
            max-width: 100%;
        }
    </style>
</head>
<body>
    <video src="cars.mp4" controls></video>
</body>
</html>

 

 


 

 

κ°€λ³€μš”μ†Œκ°€ νŽΈλ¦¬ν•˜λ©΄μ„œλ„

μ‚¬μš©ν•˜κΈ° 은근 μ–΄λ €μš΄ 것 κ°™λ‹€.

 

자주자주 μ—°μŠ΅ν•˜λ©΄μ„œ μ΅μˆ™ν•˜κ²Œ λ§Œλ“€μ–΄μ•Όκ² λ‹€.

 

 


 

 

< μ°Έκ³  >

Do it! HTML5+CSS3 μ›Ή ν‘œμ€€μ˜ 정석

 

 

β€» ν•΄λ‹Ή 글은 μŠ΅λ“ λͺ©μ μœΌλ‘œ μž‘μ„±λœ κΈ€μž…λ‹ˆλ‹€ β€»

 

λ°˜μ‘ν˜•

λŒ“κΈ€