๐ŸŽจ Frontend/HTML & CSS

CSS โญ โ‘ข ํ•„์ˆ˜ ์†์„ฑ - (2) ํ™”๋ฉด ๋ฐฐ์น˜

kongmi 2023. 3. 15. 09:10

โญ์œ„์น˜ ์†์„ฑ์œผ๋กœ HTML ์š”์†Œ ๋ฐฐ์น˜

์œ„์น˜ ์†์„ฑ์€ ์š”์†Œ๋ฅผ ์ƒ๋Œ€์ ์œผ๋กœ ๋˜๋Š” ์ ˆ๋Œ€์ ์œผ๋กœ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

postion ์†์„ฑ

๐Ÿ‘‰ position์€ ์š”์†Œ์˜ ์œ„์น˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ๋˜๋Š” ์†์„ฑ

position ์†์„ฑ์„ ์ง€์ •ํ•˜๋ฉด top, right, bottom, left ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ์š”์†Œ์˜ ์œ„์น˜๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
๊ฐ ์†์„ฑ์€ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ง€์ •๋œ ๊ฑฐ๋ฆฌ๋งŒํผ ์ด๋™์‹œํ‚ต๋‹ˆ๋‹ค.

position ์†์„ฑ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋Š” ์†์„ฑ์œผ๋กœ z-index๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. z-index๋Š” ์š”์†Œ์˜ ์Œ“์ž„ ์ˆœ์„œ๋ฅผ ์ง€์ •ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ฐ’์ด ๋†’์„ ์ˆ˜๋ก ์œ„์— ์Œ“์ด๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

position ์†์„ฑ์€ ๋ ˆ์ด์•„์›ƒ์„ ๊ตฌ์„ฑํ•˜๋Š” ๋ฐ ๋งค์šฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

ํŠนํžˆ absolute์™€ fixed ๊ฐ’์€ ์š”์†Œ๋ฅผ ์ž์œ ๋กญ๊ฒŒ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฌํ•œ ์†์„ฑ์„ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ๋‹ค๋ฅธ ์š”์†Œ๋“ค๊ณผ ์ถฉ๋Œ์ด๋‚˜ ๊ฒน์นจ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์กฐ์‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

static

๐Ÿ‘‰ positoin ์†์„ฑ๊ฐ’์„ static์œผ๋กœ ์ง€์ •ํ•˜๋ฉด ์•„๋ฌด๋Ÿฐ ๋ณ€ํ™” ์—†์Œ

relative

๐Ÿ‘‰ ์š”์†Œ๋ฅผ ์ขŒํ‘œ ์†์„ฑ์— ๋”ฐ๋ผ ์žฌ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
   ์›๋ž˜ ์œ„์น˜ ๊ธฐ์ค€์œผ๋กœ ์ƒ๋Œ€์ ์œผ๋กœ ๋ฐฐ์น˜ ๋จ.

๐Ÿ“Œ ๋ถ€๋ชจ ์š”์†Œ์— ์œ„์น˜๋ฅผ ๋„ฃ๋Š”๋‹ค๋ฉด relative๋ฅผ..!

absolute

๐Ÿ‘‰ ์š”์†Œ๋ฅผ ๋ถ€๋ชจ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ ˆ๋Œ€์ ์ธ ์œ„์น˜์— ๋ฐฐ์น˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
   ์œ„์น˜์ƒ์˜ ๋ถ€๋ชจ ์š”์†Œ ๊ธฐ์ค€, ์ฆ‰ ๋ถ€๋ชจ์˜ position ์†์„ฑ์ด ๋ฐ˜๋“œ์‹œ relative, absolute, fixed ์ค‘ ํ•˜๋‚˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
  ์ƒ์œ„ ์š”์†Œ๊ฐ€ ์—†์œผ๋ฉด html ๊ธฐ์ค€์œผ๋กœ ์š”์†Œ์˜ ๊ธฐ์ค€ ์œ„์น˜ ๊ฒฐ

 

์›๋ž˜ ์œ„์น˜

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>์œ„์น˜ ์†์„ฑ์œผ๋กœ html ์š”์†Œ ๋ฐฐ์น˜ํ•˜๊ธฐ</title>
  <style>
    body {
      margin: 0;
    }
    .box {
      width: 100px;
      height: 100px;
    }
    .red-box {
      background-color: red;
    }
    .green-box {
      background-color: green;
    }
  </style>
</head>
<body>
  <div class="box red-box"></div>
  <div class="box green-box"></div>
</body>
</html>

 

relative ์ ์šฉ

 

absolute ์ ์šฉ

fixed

 ๐Ÿ‘‰์š”์†Œ๋ฅผ ๋ทฐํฌํŠธ ๊ธฐ์ค€์œผ๋กœ ๊ณ ์ •๋œ ์œ„์น˜์— ๋ฐฐ์น˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
   ์Šคํฌ๋กค์ด ๋˜์–ด๋„ ํ™”๋ฉด์—์„œ ์‚ฌ๋ผ์ง€์ง€ ์•Š๊ณ  ํ•ญ์ƒ ๊ณ ์ •๋œ ์œ„์น˜์— ์žˆ์Šต๋‹ˆ๋‹ค.
   absolute์™€ ๋˜‘๊ฐ™์ด ์ž‘๋™ํ•˜๋Š” ๊ฒƒ ์ฒ˜๋Ÿผ ๋ณด์ด์ง€๋งŒ, ์Šคํฌ๋กค ํ–ˆ์„ ๋•Œ ์ฐจ์ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“Œ ์ƒ๋‹จ ๊ณ ์ • ๋ฉ”๋‰ด๋ฐ” ๊ฐ™์€ ....

sticky

 ๐Ÿ‘‰์Šคํฌ๋กค์ด ๋ฐœ์ƒํ•  ๋•Œ ์ง€์ •ํ•œ ์œ„์น˜์— ๊ณ ์ •๋˜๋Š” ์š”์†Œ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉ

 ๐Ÿ‘‰ fixed์™€ ๋‹ฌ๋ฆฌ ์Šคํฌ๋กค์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์œผ๋ฉด ์ผ๋ฐ˜์ ์ธ ๋ฌธ์„œ ํ๋ฆ„์— ๋”ฐ๋ผ ๋ฐฐ์น˜๋จ.

 ๐Ÿ‘‰ ์Šคํฌ๋กค์ด ๋ฐœ์ƒํ•˜๋ฉด ์ง€์ •ํ•œ ์œ„์น˜๊นŒ์ง€๋งŒ ์›€์ง์ด๊ณ  ๊ทธ ๋’ค๋กœ๋Š” ๊ณ ์ •๋จ.
๐Ÿ“Œ
์‚ฌ์ด๋“œ๋ฐ” ๋А๋‚Œ...?

z-index

๐Ÿ‘‰ position ์†์„ฑ์œผ๋กœ ๋ฐฐ์น˜ํ•œ ์š”์†Œ์˜ ์Œ“์ด๋Š” ์œ„์น˜(๋†’๋‚ฎ์ด)๋ฅผ ๊ฒฐ์ • (z์ถ•)

๐Ÿ‘‰์šฐ์„ ์ˆœ์œ„ ๋ณ€๊ฒฝ, ๊ฐ’์ด ์ปค์งˆ ์ˆ˜๋ก ์šฐ์„ ์ˆœ์œ„๊ฐ€ ์ปค์ง. ์œ„์น˜ ๋’ค์ง‘๊ธฐ(?)
   ๋‹จ, ๋™๋“ฑ ์กฐ๊ฑด์ผ ๋•Œ์—๋งŒ ์ ์šฉ๋จ. (static์ด๋ฉด ์ ์šฉ ์•ˆ๋จ)


๐Ÿธ ์—ฐ์Šต๋ฌธ์ œ

์•„๋ž˜์˜ ํ™”๋ฉด๊ณผ ๊ฐ™์ด ๋ฐฐ์น˜ ํ•ด ๋ณด์„ธ์š”.

  • ์ „์ฒด ๋ฐ•์Šค ๊ฐ€๋กœ ํฌ๊ธฐ 300px (๋ฐฐ๊ฒฝ์ƒ‰์ƒ royalblue)
  • ๋‚ด๋ถ€ ๋ฐ•์Šค ํฌ๊ธฐ 100px์˜ ์ •์‚ฌ๊ฐํ˜• (๋ฐฐ๊ฒฝ ์ƒ‰์ƒ orange)
  • ์„  ๋‘๊ป˜ 4px

 

๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    body {
      margin: 0;
    }
    .background {
      width: 300px;
      height: 200px;
      background-color: royalblue;
    }
    .box {
      width: 100px;
      height: 100px;
      background-color: orange;
      border: 4px dashed red;
      box-sizing: border-box;
    }
    .box1 {
      position: absolute;
      left: 0;
      top: 0;
      z-index: 1;
    }
    .box2 {
      position: absolute;
      left: 50px;
      top: 50px;
      z-index: 2;
    }
    .box3 {
      position: relative;
      bottom: 100px;
      z-index: 3;
    }
  </style>
</head>
<body>
  <div class="background"></div>
  <div class="box box1"></div>
  <div class="box box2"></div>
  <div class="box box3"></div>
</body>
</html>

 

๊ฐ•์‚ฌ๋‹˜ ์ฝ”๋“œ

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    .container {
      width: 300px;
      background-color: royalblue;
      position: relative;
    }
    .container .item {
      width: 100px;
      height: 100px;
      border: 4px dashed red;
      background-color: orange;
    }
    .container .item:nth-child(2) {
      position: absolute;
      left: 50px;
      top: 50px;
    }
    .container .item:nth-child(3) {
      z-index: 1;
      position: relative;
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="item">1</div>
    <div class="item">2</div>
    <div class="item">3</div>
  </div>
</body>
</html>

๋‚ด ์ฝ”๋“œ์™€ ๋‹ค๋ฅธ ์ 

  • ํŒŒ๋ž€์ƒ‰ ๋ฐ•์Šค๋ฅผ ๋ถ€๋ชจ ์š”์†Œ๋กœ ๋„ฃ๊ณ  ์˜ค๋ Œ์ง€ ๋ฐ•์Šค 3๊ฐœ๋ฅผ ์ž์‹์œผ๋กœ ๋‘์–ด ๋ถ€๋ชจ ๋ฐ•์Šค ๋†’์ด๊ฐ€ ์ž์‹ ๋ฐ•์Šค ํฌ๊ธฐ์— ๋งž์ถฐ ๋Š˜์–ด๋‚˜๋„๋ก ํ–ˆ๋‹ค.
  • ์ž์‹๋“ค์„ ์„ ํƒ์ž๋กœ ์ง€์ •ํ•˜์—ฌ ์œ„์น˜ ์†์„ฑ ์ ์šฉ
  • ์ž์‹์ด ์—ฌ๋Ÿฌ๊ฐœ ์ผ ๋•Œ ๊ตณ์ด ํด๋ž˜์Šค ์—ฌ๋Ÿฌ๊ฐœ ๋งŒ๋“ค์ง€ ๋ง๊ณ  nth-child(n) ์†์„ฑ ์ด์šฉํ•  ๊ฒƒ

float

๐Ÿ‘‰ ๋Œ€์ƒ ์š”์†Œ๋ฅผ ๊ณต์ค‘์— ๋„์›Œ ๋‹ค์Œ์— ์˜ค๋Š” ์š”์†Œ๋ฅผ ์ฃผ๋ณ€์— ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ๋ฐฐ์น˜

๐Ÿ‘‰ margin์„ ์•ˆ์ฃผ๋ฉด ์˜†์— ๋”ฑ ๋ถ™์–ด๋ฒ„๋ฆผ

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    img {
      float: left;
      margin-right: 14px;
    }
  </style>
</head>
<body>
  <img src="./images/coffee.jpg" alt="์ปคํ”ผ">
  <p>Lorem, ipsum dolor sit amet consectetur adipisicing elit. 
    Corrupti ut dolor ratione blanditiis iste, harum fugit, soluta eum
    obcaecati, expedita perspiciatis quam culpa? Doloribus repellendus
    labore sed inventore iusto eveniet. Lorem, ipsum dolor sit amet
    consectetur adipisicing elit. Maiores at natus aliquid numquam vitae nisi, 
    voluptates laudantium saepe error sapiente nobis adipisci et accusamus 
    molestiae commodi consequuntur earum, ut dicta? Lorem ipsum dolor sit amet consectetur, 
    adipisicing elit. Id molestiae perspiciatis non temporibus voluptates alias 
    ducimus libero esse recusandae placeat, natus quo mollitia sint enim? 
    Ex numquam non officia ipsam?</p>
</body>
</html>

๋ธ”๋ก ์š”์†Œ๋ฅผ ์ธ๋ผ์ธ ์š”์†Œ์ฒ˜๋Ÿผ ๋ฐฐ์น˜ ํ•˜๊ธฐ

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    div {
      color: white;
    }
    .red-box {
      background-color: red;
      float: left;
      width: 100px;
    }
    .blue-box {
      background-color: blue;
      width: 100px;
      float: left;
    }
    .green-box {
      background-color: green;
      width: 100px;
      float: left;
    }
  </style>
</head>
<body>
  <div class="red-box">red-box</div>
  <div class="blue-box">blue-box</div>
  <div class="green-box">green-box</div>
</body>
</html>
  • ๊ณ„์† ๋ฐฐ์น˜ํ•˜๋ ค๋ฉด float:left ํ•ด์•ผํ•จ..
  • ํ•˜์ง€๋งŒ float ๋ณด๋‹จ flex-box๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด ๋” ์ง๊ด€์ ์ด๊ณ  ์ข‹์Œ.
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    div {
      color: white;
    }
    .red-box {
      background-color: red;
      float: left;
      width: 100px;
    }
    .blue-box {
      background-color: blue;
      width: 100px;
      float: left;
    }
    .green-box {
      background-color: green;
      width: 100px;
      float: left;
    }
    p {
      color: black;
      clear: left;
    }
  </style>
</head>
<body>
  <div class="red-box">red-box</div>
  <div class="blue-box">blue-box</div>
  <div class="green-box">green-box</div>
  <div>
    <p>์—ฌ๊ธฐ์„œ ๋ถ€ํ„ฐ๋Š” ๋ณธ๋ฌธ ์ž…๋‹ˆ๋‹ค.</p>
  </div>  
</body>
</html>
  • clear ์•ˆ ์“ฐ๋ฉด p๋„ ๋ฐ•์Šค ์˜†์— ๋ถ™์Œ!
  • ์•„๋ž˜๋กœ ๋‚ด๋ฆฌ๋ ค๋ฉด clear:left; ์“ฐ๋ฉด ๋œ๋‹ค.