Style Sheet/CSS

๐Ÿ•น๏ธ CSS ์š”์†Œ ์ˆ˜ํ‰/์ˆ˜์ง ์ •๋ ฌ ๊ธฐ๋ฒ• ๋ชจ์Œ

์ธํŒŒ_ 2023. 2. 24. 14:42

css์ •๋ ฌ๊ธฐ๋ฒ•

์ˆ˜ํ‰ ์ค‘์•™ ์ •๋ ฌ

 

๐Ÿ“ข inline / inline-block ์š”์†Œ

์ •๋ ฌ ๋Œ€์ƒ ์š”์†Œ(inline)์˜ ๋ถ€๋ชจ ์š”์†Œ(block)์— text-align: center;๋ฅผ ์ง€์ •ํ•œ๋‹ค.

  • inline, inline-block, inline-table, inline-flex, ๊ธฐํƒ€ ๋“ฑ๋“ฑ์— ์ ์šฉ๋œ๋‹ค.
<!-- HTML -->
<nav>
  <a href="#">One</a> <!-- aํƒœ๊ทธ๋Š” inline์š”์†Œ ์ด๋‹ค -->
  <a href="#">Two</a>
  <a href="#">Three</a>
  <a href="#">Four</a>
</nav>

<!-- CSS -->
<style>
nav {
  text-align: center;
}
</style>

css์ •๋ ฌ๊ธฐ๋ฒ•

โ€‹

์ž์‹์š”์†Œ๊ฐ€ block์š”์†Œ์ด๋ฉด, ๊ฐ•์ œ๋กœ ์ž์‹์š”์†Œ๋ฅผ inline-block์š”์†Œ๋กœ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๋ฐฉ๋ฒ•๋„ ์žˆ๋‹ค.

<!-- HTML -->
<main class="inline-block-center">
  <div> I'm an element that is block-like with my siblings and we're centered in a row. </div>
  <div> I'm an element that is block-like with my siblings and we're centered in a row. I have more content in me than my siblings do. </div>
  <div> I'm an element that is block-like with my siblings and we're centered in a row.
  </div>
</main>

<!-- CSS -->
<style>
  .inline-block-center {
    text-align: center; /* inline์š”์†Œ๋งŒ ์ ์šฉ๋จ */
  }
  .inline-block-center div {
    display: inline-block; /* block์š”์†Œ์ธ div๋ฅผ inline์œผ๋กœ ๋งŒ๋“ค์–ด์ค€๋‹ค. */
    width: 200px;

    text-align: left; /* ์•ˆ์˜ ๋‚ด์šฉ ๊ธ€์ž ์ •๋ ฌ */
  }
</style>

css์ •๋ ฌ๊ธฐ๋ฒ•

โ€‹

๐Ÿ“ข block ์š”์†Œ

์ •๋ ฌ ๋Œ€์ƒ ์š”์†Œ์— ๋„ˆ๋น„(width)๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ์ง€์ •ํ•˜๊ณ  margin-right์™€ margin-left ํ”„๋กœํผํ‹ฐ์— auto๋ฅผ ์ง€์ •ํ•œ๋‹ค.

(width๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์œผ๋ฉด, ์–ด์ฐจํ”ผ block์š”์†Œ๋Š” ๊ฐ€๋กœ๋ฅผ ์ „์ฒด ์ฐจ์ง€ํ•˜๋‹ˆ ์ค‘์•™ ์ •๋ ฌ ์˜๋ฏธ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.)

.item {
  width: 200px;
  margin: 0px auto;
}

css์ •๋ ฌ๊ธฐ๋ฒ•

โ€‹

๐Ÿ“ข ์ด๋ฏธ์ง€

Image๋Š” ์™ผ์ชฝ, ์˜ค๋ฅธ์ชฝ margin์„ auto๋กœ ์„ค์ •ํ•˜๊ณ  display: block; ์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

img {
  display: block;
  margin-left: auto;
  margin-right: auto;
  width: 40%;
}

โ€‹

๐Ÿ“ข Position / Transform ์ด์šฉ

item {
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}

โ€‹

๐Ÿ“ข Flex ์ด์šฉ

.flex-center {
  display: flex;
  justify-content: center;
}

์ˆ˜์ง ์ค‘์•™ ์ •๋ ฌ

 

๐Ÿ“ข inline / inline-block ์š”์†Œ

- ํ•œ์ค„ ์ผ ๊ฒฝ์šฐ

disaplay: block ์„ค์ • ํ›„ height์™€ line-height๋ฅผ ๊ฐ™์€ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๋ฉด ๋œ๋‹ค.

์ด ๋•Œ white-space: nowrap ์†์„ฑ์œผ๋กœ ์ž๋™ ์ค„๋ฐ”๊ฟˆ์ด ์•ˆ๋˜๋„๋ก ํ•ด์•ผํ•œ๋‹ค.

main span {
  display: block;
  width: 50%;
  
  height: 100px;
  line-height: 100px;
  
  white-space: nowrap;
}

โ€‹

- ์—ฌ๋Ÿฌ์ค„ ์ผ ๊ฒฝ์šฐ

.center { /* ๋ถ€๋ชจ */
   display: table;
}
.center span {
   display: table-cell;
   vertical-align: middle; /* top, bottom */
}

โ€‹

๐Ÿ“ข block ์š”์†Œ

- ์ž์‹ ์š”์†Œ์˜ ๋†’์ด๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ

๋ถ€๋ชจ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ ˆ๋Œ€ ์œ„์น˜๋ฅผ ์ง€์ •ํ•œ๋‹ค.

.parent {
  position: relative;
}

.child {
  height: 100px; /* ์š”์†Œ์˜ ๋†’์ด๊ฐ€ ๊ณ ์ • */
  position: absolute;
  top: 50%;
  margin-top: -50px; /* ์š”์†Œ์˜ ๋†’์ด(100px)์˜ ๋ฐ˜ ๋งŒํผ ์œ„๋กœ ์ด๋™ */
}

โ€‹

- ์ž์‹ ์š”์†Œ์˜ ๋†’์ด๊ฐ€ auto ์ธ ๊ฒฝ์šฐ

๋ถ€๋ชจ ์š”์†Œ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ ˆ๋Œ€ ์œ„์น˜๋ฅผ ์ง€์ •ํ•œ๋‹ค.

.parent {
  position: relative;
}

.child {
  position: absolute;
  top: 50%;
  /*์š”์†Œ์˜ ๋†’์ด์˜ ๋ฐ˜(50%) ๋งŒํผ ์œ„๋กœ ์ด๋™*/
  transform: translateY(-50%);
}

โ€‹

๐Ÿ“ข Position / Transform ์ด์šฉ

item {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

โ€‹

๐Ÿ“ข Flex ์ด์šฉ

๋ถ€๋ชจ ์š”์†Œ์— Flexbox layout์„ ์ง€์ •ํ•œ๋‹ค.

.parent {
  display: flex;
  
  /*์œ„์—์„œ ์•„๋ž˜๋กœ ์ˆ˜์ง ๋ฐฐ์น˜*/
  flex-direction: column;
  
  /*์ค‘์•™์ •๋ ฌ*/
  justify-content: center;
}

 

๐Ÿ“ข ํ…์ŠคํŠธ ์„ธ๋กœ ์ •๋ ฌ

a {
  height: 130px; 
  display: table-cell; 
  vertical-align: middle;
}

์ˆ˜ํ‰ + ์ˆ˜์ง ์ค‘์•™ ์ •๋ ฌ

 

๐Ÿ“ข Position / Transform ์ด์šฉ

- ์ž์‹ ์š”์†Œ์˜ ๋†’์ด๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ

.center {
  position: relative;
}

.center div {
  position: absolute;
  width: 300px;
  height: 200px;

  top: 50%;
  left: 50%;
  margin: -100px 0 0 -150px; /* margin-top์„ width์˜ ์ ˆ๋ฐ˜๋งŒํผ ๋งˆ์ด๋„ˆ์Šค ์„ค์ •, */
                             /* margin-left๋ฅผ height์˜ ์ ˆ๋ฐ˜๋งŒํผ ๋งˆ์ด๋„ˆ์Šค๋กœ ์„ค์ • */
}

โ€‹

- ์ž์‹ ์š”์†Œ์˜ ๋†’์ด๊ฐ€ auto ์ธ ๊ฒฝ์šฐ

.parent {
  position: relative;
}
.child {
  position: absolute;
  top: 50%;
  left: 50%;
  /*์š”์†Œ์˜ ๋†’์ด/๋„ˆ๋น„์˜ ๋ฐ˜(50%)๋งŒํผ ์œ„/์™ผ์ชฝ์œผ๋กœ ์ด๋™*/
  transform: translate(-50%, -50%);
}

css์ •๋ ฌ๊ธฐ๋ฒ•

โ€‹

๐Ÿ“ข Flex ์ด์šฉ

.parent {
  display: flex;
  justify-content: center; /* ํ”Œ๋ž™์Šค ๋ฐ•์Šค ์ •๋ ฌ */
  align-items: center; /* ํ”Œ๋ž™์Šค ์•„์ดํ…œ ์ •๋ ฌ */
}

โ€‹

๐Ÿ“ข Grid ์ด์šฉ

.wrapper {
  display: grid;
  place-items: center;
}

์ขŒ / ์šฐ์ธก ์ชฝ์œผ๋กœ ์ •๋ ฌ

 

๐Ÿ“ข inline / inline-block ์š”์†Œ

.container {
  text-align: right; /* left */
}

.item { /* inline ์š”์†Œ */
  width: 150px;
  display: inline-block; /* block์š”์†Œ์ผ ๊ฒฝ์šฐ ์ผ๋ถ€๋Ÿฌ inline-block์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค. */
}

โ€‹

๐Ÿ“ข block ์š”์†Œ

.right {
  position: absolute;
  right: 0px; /* left: 0px */
}
์ฐธ๊ณ : absolute๋กœ ์„ค์ •ํ•œ element๋Š” ์ •์ƒ ํ๋ฆ„์—์„œ ์ œ๊ฑฐ๋˜์–ด ๋‹ค๋ฅธ element์™€ ๊ฒน์น  ์ˆ˜ ์žˆ๋‹ค.

โ€‹

๐Ÿ“ข float ์ด์šฉ

.right {
  float: right; /* left */
  width: 300px;
  border: 3px solid #73AD21;
  padding: 10px;
}

โ€‹

๐Ÿ“ข position ์ด์šฉ

header {
  position: relative;
}

nav {
  position: absolute;
  top: 0;
  right: 0; /* left: 0 */
  padding: 20px;
}

โ€‹

๐Ÿ“ข Flex (์šฐ์ธก)

header {
  display: flex;
  justify-content: flex-end /* or space-between; */
}
header {
  display: flex;
}

nav {
  margin-left: auto;
}

โ€‹

๐Ÿ“ข Grid (์šฐ์ธก)

์š”์†Œ๋“ค์ด ๊ฐ๊ธฐ ๋‹ค๋ฅธ ๊ฐ€๋กœ๊ธธ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์„๋•Œ 1fr๋กœ ์„ค์ •ํ•˜๊ณ  ์—ด๋Œ€๋กœ ์ฐจ๋ก€๋กœ ๋ฐฐ์น˜ํ•˜๊ฒŒ๋˜๋ฉด, ๋‹ค๋ฅธ ๊ฐ€๋กœ๊ธธ์ด๋•Œ๋ฌธ์— ์ขŒ์šฐ์ธก ์ •๋ ฌ์ด ๋˜๊ธฐ๋„ ํ•˜๋‹ค.

header {
  display: grid;
  grid-auto-flow: column;
  grid-template-columns: 1fr;
}

css์ •๋ ฌ๊ธฐ๋ฒ•


์–‘๋ ์ •๋ ฌ

 

๐Ÿ“ข position ์†์„ฑ์„ ์ด์šฉํ•œ ์ขŒ์šฐ ์ •๋ ฌ

์ ˆ๋Œ€ ์œ„์น˜ ์ง€์ • ๋ฐฉ์‹์œผ๋กœ ์œ„์น˜ํ•œ ์š”์†Œ๋Š” ์ •์ƒ์ ์ธ ๋ ˆ์ด์•„์›ƒ์—์„œ ๋ฒ—์–ด๋‚˜ ๋‹ค๋ฅธ ์š”์†Œ์™€ ๊ฒน์น  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

๋”ฐ๋ผ์„œ ์ด ํŠน์„ฑ์„ ์ด์šฉํ•˜๋ฉด HTML ์š”์†Œ๋ฅผ ์ˆ˜ํ‰ ๋ฐฉํ–ฅ์œผ๋กœ ์ขŒ์šฐ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

position ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌํ•  ๊ฒฝ์šฐ์—๋Š” <body>์š”์†Œ์— margin๊ณผ padding ์†์„ฑ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ์›น ๋ธŒ๋ผ์šฐ์ €๋งˆ๋‹ค ๋ ˆ์ด์•„์›ƒ์ด ๋‹ค๋ฅด๊ฒŒ ๋ณด์ด๋Š” ๊ฒƒ์„ ๋ฏธ๋ฆฌ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

 body {
     padding: 0;
     margin: 0;
}

 div {
     border: 3px solid #4B0082;
     width: 300px;
     padding: 10px;
     margin: 0;
     position: absolute;
     right: 0; /* ์šฐ์ธก์— ๋”ฑ ๋ถ™์ด๊ธฐ */
}

css์ •๋ ฌ๊ธฐ๋ฒ•

โ€‹

โ€‹

๐Ÿ“ข float ์†์„ฑ์„ ์ด์šฉํ•œ ์ขŒ์šฐ ์ •๋ ฌ

float ์†์„ฑ์„ ์ด์šฉํ•˜๋ฉด ์ˆ˜ํ‰ ๋ฐฉํ–ฅ์œผ๋กœ ์ขŒ์šฐ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

 

float ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ์ •๋ ฌํ•  ๊ฒฝ์šฐ์—๋Š” <body>์š”์†Œ์— margin๊ณผ padding ์†์„ฑ๊ฐ’์„ ์„ค์ •ํ•˜๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.

์ด๋ ‡๊ฒŒ ํ•จ์œผ๋กœ์จ ์›น ๋ธŒ๋ผ์šฐ์ €๋งˆ๋‹ค ๋ ˆ์ด์•„์›ƒ์ด ๋‹ค๋ฅด๊ฒŒ ๋ณด์ด๋Š” ๊ฒƒ์„ ๋ฏธ๋ฆฌ ๋ฐฉ์ง€ํ•  ์ˆ˜ ์žˆ๋‹ค.

body {
     padding: 0;
     margin: 0;
}

div {
     border: 3px solid #4B0082;
     width: 350px;
     padding: 10px;
     margin: 0;
}

div.left {
     float: left 
}

div.right {
     float: right 
}

css์ •๋ ฌ๊ธฐ๋ฒ•


# ์ฐธ๊ณ ์ž๋ฃŒ

https://tcpschool.com/css/css_position_align

https://poiemaweb.com/css3-centering

https://cbw1030.tistory.com/21

https://takeu.tistory.com/18

https://www.daleseo.com/css-centering/