[JAYโŒจ๏ธCSS] ์š”์†Œ๋ฅผ ๊ฐ€๋กœ, ์„ธ๋กœ ์ค‘์•™ ์ •๋ ฌ ํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ชจ์Œ!

2023. 3. 21. 23:36ใ†html&css

๋ฐ˜์‘ํ˜•

 

 

 

์š”์†Œ๋ฅผ ๊ฐ€๋กœ, ์„ธ๋กœ ์ค‘์•™ ์ •๋ ฌ ํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ชจ์Œ!

 1๏ธโƒฃ Transform : translate()
  1. ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์€ ์š”์†Œ์˜ ๋ถ€๋ชจ ์š”์†Œ์— position : relative ์†์„ฑ์„ ์ฃผ๊ณ  ๊ธฐ์ค€์„ ์ƒ์„ฑ.
  2. ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์€ ์š”์†Œ์— position : absolute ์†์„ฑ์„ ์ฃผ๊ณ  left, top 50%, 50%์”ฉ ์ด๋™์‹œ์ผœ ์ค‘์•™์œผ๋กœ ์ด๋™.
  3. position์„ ํ†ตํ•ด ์ด๋™์‹œํ‚ฌ ๋•Œ ์š”์†Œ์˜ width, height ๊ฐ’์„ ํฌํ•จํ•ด์„œ ์ด๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— transform: translate(-50%, -50%);
    ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์š”์†Œ์˜ width, height ๊ฐ’์˜ -50% ๋งŒํผ ๋‹ค์‹œ ๋˜๋Œ๋ ค์„œ ๋ฐฐ์น˜๋˜์–ด ์ƒํ•˜์ขŒ์šฐ ์ค‘์•™ ์ •๋ ฌ ์™„์„ฑ!
  4. ์š”์†Œ ํฌ๊ธฐ์— ๋Œ€ํ•œ transform ๊ฐ’์„ ํผ์„ผํ‹ฐ์ง€๋กœ ์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์œ ์ง€๋ณด์ˆ˜์— ์šฉ์ดํ•ด์„œ ๊ฐ€์žฅ ๋ณดํŽธ์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•.
<div class="parent">
    <div class="children translate">
    	<!-- Contents -->
    </div>
</div>
.parent {
	/* ๋ถ€๋ชจ ์š”์†Œ์— relative๋ฅผ ์ฃผ๊ณ  ๊ธฐ์ค€ ์ƒ์„ฑ */
    position: relative;
}

.children {
	/* ์ž์‹ ์š”์†Œ์— absolute๋ฅผ ์ฃผ๊ณ  ์ƒํ•˜ 50%์”ฉ ๋ฐฐ์น˜ */
    position: absolute;
    top: 50%;
    left: 50%;
}


/* ๐Ÿ’ก Solution 01 | Transform : translate() */
.translate {
    transform: translate(-50%, -50%);
    -webkit-transform: translate(-50%, -50%);
    -moz-transform: translate(-50%, -50%);
    -ms-transform: translate(-50%, -50%);
    -o-transform: translate(-50%, -50%);
}

Transform : translate() ์†์„ฑ์„ ์ด์šฉํ•œ ์ค‘์•™ ์ •๋ ฌ
Transform : translate() ์†์„ฑ์„ ์ด์šฉํ•œ ์ค‘์•™ ์ •๋ ฌ

 

 

 

 

 

 2๏ธโƒฃ Negative Margin
  1. ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์€ ์š”์†Œ์˜ ๋ถ€๋ชจ ์š”์†Œ์— position : relative ์†์„ฑ์„ ์ฃผ๊ณ  ๊ธฐ์ค€์„ ์ƒ์„ฑ.
  2. ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์€ ์š”์†Œ์— position : absolute ์†์„ฑ์„ ์ฃผ๊ณ  left, top 50%, 50%์”ฉ ์ด๋™์‹œ์ผœ ์ค‘์•™์œผ๋กœ ์ด๋™.
  3. position์„ ํ†ตํ•ด ์ด๋™์‹œํ‚ฌ ๋•Œ ์š”์†Œ์˜ width, height ๊ฐ’์„ ํฌํ•จํ•ด์„œ ์ด๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์Œ์ˆ˜๋กœ margin ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋ฉด ์š”์†Œ์˜ width, height ๊ฐ’์˜ -50% ๋งŒํผ ๋‹ค์‹œ ๋˜๋Œ๋ ค์„œ ๋ฐฐ์น˜๋˜์–ด ์ƒํ•˜์ขŒ์šฐ ์ค‘์•™ ์ •๋ ฌ ์™„์„ฑ!
  4. ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด์ง€๋งŒ ๊ฐ’์„ ๊ณ ์ •์œผ๋กœ ์ฃผ์–ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ ์ง€๋ณด์ˆ˜์‹œ ์ฃผ์˜.
<div class="parent">
    <div class="children negativeMargin">
    	<!-- Contents -->
    </div>
</div>
.parent {
	/* ๋ถ€๋ชจ ์š”์†Œ์— relative๋ฅผ ์ฃผ๊ณ  ๊ธฐ์ค€ ์ƒ์„ฑ */
    position: relative;
}

.children {
	/* ์ž์‹ ์š”์†Œ์— absolute๋ฅผ ์ฃผ๊ณ  ์ƒํ•˜ 50%์”ฉ ๋ฐฐ์น˜ */
    position: absolute;
    top: 50%;
    left: 50%;
    width: 400px;
    height: 200px;
}


/* ๐Ÿ’ก Solution 02 | negative Margin = width์˜ 2/1 px */
.negativeMargin {
	/* ์š”์†Œ์˜ 2/1 ํฌ๊ธฐ๋งŒํผ ์Œ์ˆ˜๊ฐ’ ์ง€์ • */
    margin-left: -200px;  
    margin-top: -100px;
}

Negative Margin = width์˜ 2/1 px ์„ ํ†ตํ•œ ์ค‘์•™ ์ •๋ ฌ
Negative Margin = width์˜ 2/1 px ์„ ํ†ตํ•œ ์ค‘์•™ ์ •๋ ฌ

 

 

 

 

 

3๏ธโƒฃ inset ์†์„ฑ๊ณผ Margin auto
  1. ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์€ ์š”์†Œ์˜ ๋ถ€๋ชจ ์š”์†Œ์— position : relative ์†์„ฑ์„ ์ฃผ๊ณ  ๊ธฐ์ค€์„ ์ƒ์„ฑ.
  2. ๊ฐ€์šด๋ฐ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์€ ์š”์†Œ์— position : absolute ์†์„ฑ์„ ์ฃผ๊ณ left, right, top, bottom ๊ฐ๊ฐ 0์”ฉ ๊ฐ’์„ ์ฃผ๋ฉด ์ขŒ, ์šฐ๋กœ ๋Š˜์–ด๋‚˜์„œ ๊ฝ‰ ์ฐจ๋Š” ํ˜•ํƒœ๊ฐ€ ๋œ๋‹ค. ์ด๋•Œ margin:auto ๋ฅผ ์ฃผ๋ฉด ์ƒํ•˜์™€ ์ขŒ์šฐ์— ๋™์ผํ•œ ๋น„์œจ์˜ margin ๊ฐ’์ด ๋ถ€์—ฌ๋˜๋ฏ€๋กœ ์ƒํ•˜์ขŒ์šฐ ์ค‘์•™ ์ •๋ ฌ ์™„์„ฑ!
  3. left: 0; right: 0; top: 0; bottom:0; ๊ฐ’ ์€ inset : 0 ์ด๋ผ๋Š” ๋‹จ์ถ• ์†์„ฑ์œผ๋กœ ๋™์ผํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  4. inset์ด๋ผ๋Š” ์ƒˆ๋กœ์šด ์†์„ฑ์œผ๋กœ ๊ณ ์ •๊ฐ’ ์—†์ด ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์–ด์„œ Ngative Margin ๋ณด๋‹ค ์œ ์ง€๋ณด์ˆ˜์— ์šฉ์ดํ•œ ๋ฐฉ๋ฒ•!
<div class="parent">
    <div class="children inset">
    	<!-- Contents -->
    </div>
</div>
.parent {
	/* ๋ถ€๋ชจ ์š”์†Œ์— relative๋ฅผ ์ฃผ๊ณ  ๊ธฐ์ค€ ์ƒ์„ฑ */
    position: relative;
}

.children {
	/* ์ž์‹ ์š”์†Œ์— absolute */
    position: absolute;
}


/* ๐Ÿ’ก Solution 03 | inset + Margin auto */
.inset {
    /* inset = left:0, right:0; top:0; right:0 ์˜ ๋‹จ์ถ•์†์„ฑ*/
    inset: 0;
    /* ์ปจํ…์ธ ๋ฅผ ๋‹ค์“ฐ๋Š” ์ƒํƒœ๊ฐ€ ๋˜์„œ ๋‚จ๋Š” ๊ณต๊ฐ„์— margin:auto ์‚ฌ์šฉ ๊ฐ€๋Šฅ */
    margin: auto;
}

inset ์†์„ฑ๊ณผ Margin auto ๋ฅผ ํ†ตํ•œ ์ค‘์•™ ์ •๋ ฌ
inset ์†์„ฑ๊ณผ Margin auto ๋ฅผ ํ†ตํ•œ ์ค‘์•™ ์ •๋ ฌ

 

 

 

 

 

 

๋ฐ˜์‘ํ˜•