๋งˆํฌ์—…/Html & Css

[CSS] ๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ด๋ฏธ์ง€, margin-auto

Rainbow๐ŸŒˆCoder 2023. 1. 15. 17:06
728x90

https://developer.mozilla.org/ko/docs/Web/CSS/CSS_Backgrounds_and_Borders/Resizing_background_images

 

๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ์กฐ์ •ํ•˜๊ธฐ - CSS: Cascading Style Sheets | MDN

CSS ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€์˜ ๊ธฐ๋ณธ ์„ค์ •๊ฐ’์—์„œ๋Š” ์›๋ณธ ์ด๋ฏธ์ง€๊ฐ€ ํฌ๊ธฐ ๋ณ€ํ™” ์—†์ด ๋ฐ”๋‘‘ํŒ์‹์œผ๋กœ ๋ฐฐ์—ด๋ฉ๋‹ˆ๋‹ค. background-size ์†์„ฑ์— ๊ฐ€๋กœ์™€ ์„ธ๋กœ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•ด ํฌ๊ธฐ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ์ง€๋Š” ์›ํ•˜๋Š”๋Œ€๋กœ

developer.mozilla.org

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>๋กœ๊ทธ์ธํŽ˜์ด์ง€ ๋งˆํฌ์—…</title>
    <style>
        div {
            color: black;
        }

        .login-title > div {
            color: white;
            background-size : 16px;
            background-image: url('https://avatars.githubusercontent.com/u/93479286?v=4');
            background-position: center;
            background-size: 300px;
            width: 300px;
            height: 300px;
            border-radius: 10px;
            text-align: center;
            display: flex;
            flex-direction: column;
            justify-content: center;
            margin : auto;
            margin-top: 100px;
        }
        .wrap {
            width: 300px;
            margin : auto;
        }


    </style>
</head>
<body>
<div class="login-title">
<div>
    <h1>๋กœ๊ทธ์ธ ํŽ˜์ด์ง€</h1>
    <h5>์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.</h5>
</div>
</div>
    <div class="wrap">
        <div>
            <h3>๋กœ๊ทธ์ธ</h3>
            <p>
                ID = <input type="text" class="">
            </p>
            <p>
                PW = <input type="text" class="">
            </p>
            <button>๋กœ๊ทธ์ธํ•˜๊ธฐ</button>
        </div>
    </div>
        
</body>
</html>

margin auto auto; ์™€ text-align: center; ์˜ ์ฐจ์ด

 

1. text-align: center

์ด ์ •๋ ฌ๋ฐฉ๋ฒ•์€ inline ์š”์†Œ๊ฐ€ ์ •๋ ฌ์ด ๋˜๋Š” ๋ฐฉ์‹

 ๋ถ€๋ชจ block ์š”์†Œ๋ฅผ ๊ฐ€์ง„ ๋ถ€๋ถ„์— ์ ์šฉ

inline ์š”์†Œ๋Š” ๊ทธ content์˜ ๋„“์ด๋งŒํผ๋งŒ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๊ฒŒ ๋œ๋‹ค.

๊ทธ ์š”์†Œ์— ์ง์ ‘ text-align: center ๋ฅผ ํ•œ๋‹ค๋ฉด ์ด๋ฏธ ๋”ฑ ๊ทธ ์š”์†Œ์˜ ๋„“์ด๋งŒํผ๋งŒ ๊ณต๊ฐ„์„ ์ฐจ์ง€ํ•˜๊ณ 

์žˆ์œผ๋‹ˆ ๊ฑฐ๊ธฐ๋‹ค๊ฐ€ text-align์„ ์ ์šฉํ•ด๋„ ์›€์ง์ผ ๊ณต๊ฐ„์ด ์—†๋‹ค. block ์š”์†Œ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ํ•œ์ค„์„ ๋‹ค ์ฐจ์ง€

 

2. block ์š”์†Œ์— margin: 0 auto ์ด์šฉํ•œ ์ •๋ ฌ

์ด ์ •๋ ฌ๋ฐฉ๋ฒ•์€ ์ •๋ ฌ๋Œ€์ƒ์ด block ์š”์†Œ์ผ ๋•Œ ์“ธ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ด block ์š”์†Œ์˜ width๊ฐ€ ์žˆ์–ด์•ผํ•œ๋‹ค.

 

์ด๋ฏธ์ง€ ๊ฐ™์€ ๊ฒฝ์šฐ์—” ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด๋ฏธ์ง€๋งˆ๋‹ค ์‚ฌ์ด์ฆˆ๊ฐ€ ์žˆ์œผ๋‹ˆ img ์— ์ง์ ‘ ์ ์šฉํ•  ์ˆ˜ ์žˆ๊ฒ ์ง€๋งŒ,

div ํƒœ๊ทธ ๊ฐ™์€ ๊ฒฝ์šฐ์—” width="165" ๊ณผ ๊ฐ™์ด ๊ณ ์ •๋œ ๊ฐ’์ด ์žˆ์–ด์•ผ ํ•ฉํ•œ๋‹ค.

 

div์˜ ๊ธฐ๋ณธ width๋Š” auto์ด๋‹ค.

๊ทธ๋ž˜์„œ div ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ width:100% ์ฆ‰ ๋ถ€๋ชจ width๋ฅผ ๋‹ค ๊ฐ€์ง€๊ฒŒ ๋˜๋ฏ€๋กœ width ๋ฅผ ์ง€์ •ํ•ด์ฃผ์ง€ ์•Š๋Š”ํ•œ ์ขŒ์šฐ ๋‚จ๋Š” margin ์ด ์—†๊ฒŒ ๋œ๋‹ค.

 

3. ํฐํŠธ ์ ์šฉ๊ณผ ์Šคํƒ€์ผ์‹œํŠธ ๋ถ„๋ฆฌ

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Noto+Serif+KR:wght@500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" type="text/css" href = "style.css">
    <title>๋กœ๊ทธ์ธํŽ˜์ด์ง€ ๋งˆํฌ์—…</title>
</head>
<body>
<div class="login-title">
<div>
    <h1>๋กœ๊ทธ์ธ ํŽ˜์ด์ง€</h1>
    <h5>์•„์ด๋””, ๋น„๋ฐ€๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.</h5>
</div>
</div>
    <div class="wrap">
        <div>
            <h3>๋กœ๊ทธ์ธ</h3>
            <p>
                ID = <input type="text" class="">
            </p>
            <p>
                PW = <input type="text" class="">
            </p>
            <button>๋กœ๊ทธ์ธํ•˜๊ธฐ</button>
        </div>
    </div>
        
</body>
</html>
* {
    font-family: 'Noto Serif KR', serif;
}
div {
    color: black;
}

.login-title > div {
    color: white;
    background-size : 16px;
    background-image: url('https://avatars.githubusercontent.com/u/93479286?v=4');
    background-position: center;
    background-size: 300px;
    width: 300px;
    height: 300px;
    border-radius: 10px;
    text-align: center;
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin : auto;
    margin-top: 100px;
}
.wrap {
    width: 300px;
    margin : auto;
}
728x90