๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ปSTUDY/CSS

[CSS] ์ง€์ •ํ•œ ๋„“์ด์™€ ๋†’์ด์— ๋งž๊ฒŒ ์ด๋ฏธ์ง€ ๊ฐ€์šด๋ฐ ์ •๋ ฌ

๋ฐ˜์‘ํ˜•

์กฐ๊ฑด !

๋ถ€๋ชจ ๋„“์ด์™€ ๋†’์ด์— ๋งž๊ฒŒ ์ด๋ฏธ์ง€๊ฐ€ ๊ฐ€์šด๋ฐ ์ •๋ ฌ.

 

  • <img> ํƒœ๊ทธ ์“ธ ๋•Œ ์ด๋ฏธ์ง€ ๊ฐ€์šด๋ฐ ์ •๋ ฌ.
    1. position: absolute
      1. margin
      2. transform
    2. object-fit
    3. display: flex
  • background-image ์“ธ ๋•Œ ์ด๋ฏธ์ง€ ๊ฐ€์šด๋ฐ ์ •๋ ฌ.
    1. background-size

 

1. <img> ํƒœ๊ทธ

# ๊ธฐ๋ณธ html / css

<div class="absolute-img">
    <img src="https://cdn.pixabay.com/photo/2016/03/26/13/09/cup-of-coffee-1280537_1280.jpg">
</div>
html body { margin: 0; padding: 0; box-sizing: border-box; }

/* ๊ณตํ†ต */
.aboslute-img {
    width: 300px;
    height: 300px;
    position: relative;
    margin: auto;
    overflow: hidden;
    border: 1px solid red;
}
.absoluteimg > img {
    /* ์ด๋ฏธ์ง€๋ฅผ ๊ฐ€๋กœํญ์— ๋งž์ถœ ๋•Œ width: 100% */
    width: 100%;
    /* ์ด๋ฏธ์ง€๋ฅผ ์„ธ๋กœํญ์— ๋งž์ถœ ๋•Œ height: 100% */
    height: 100%;
}

 

1-1. position: absoltue

1-1-1. margin

<div class="absolute-img">
    <img class="absolute-img-margin" src="https://cdn.pixabay.com/photo/2016/03/26/13/09/cup-of-coffee-1280537_1280.jpg">
</div>
.absolute-img-margin {
    height: 100%;
    position: absolute;
    left: -50%;
    margin-left: 25%;
}

 

1-1-2. transform

<div class="absolute-img">
    <img class="absolute-img-trans" src="https://cdn.pixabay.com/photo/2016/03/26/13/09/cup-of-coffee-1280537_1280.jpg">
</div>
.absolute-img-trans {
    position: absolute;
    height: 100%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

 

์ •๋ฆฌ.

IE์—์„œ object-fit์ด ์ง€์›๋˜์ง€ ์•Š์„ ๋•Œ absolute ์‚ฌ์šฉ.

(๋‹จ, transform์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” IE8์ดํ•˜์—์„œ๋Š” margin-top, margin-left๋กœ ์Œ์ˆ˜๊ฐ’์„ ์ค€๋‹ค.)

 

 

1- 2. object-fit

  • <img>๋‚˜ <video> ๊ฐ™์€ ์˜ค๋ธŒ์ ํŠธ์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ์ปจํ…Œ์ด๋„ˆ ํฌ๊ธฐ์— ๋งž์ถฐ ์กฐ์ ˆ๊ฐ€๋Šฅ.
  • ๋„“์ด์™€ ๋†’์ด๊ฐ’์„ ์ž…๋ ฅํ•˜๊ณ  object-fit ์†์„ฑ๋งŒ ์ง€์ •ํ•˜๋ฉด ๋œ๋‹ค.

 

value ์„ค๋ช…
fill ๊ธฐ๋ณธ๊ฐ’. ์ฃผ์–ด์ง„ ๋„ˆ๋น„์™€ ๋†’์ด์— ๋งž๊ฒŒ ์กฐ์ ˆ. ์ด๋ฏธ์ง€์˜ ๊ฐ€๋กœ์„ธ๋กœ ๋น„์œจ ์œ ์ง€์•ˆ๋จ.
contain ๊ฐ€๋กœ์„ธ๋กœ ๋น„์œจ ์œ ์ง€ํ•œ ์ฑ„ ์‚ฌ์ด์ฆˆ ์กฐ์ ˆ ๊ฐ€๋Šฅ. ์ด๋ฏธ์ง€์™€ ์ปจํ…Œ์ด๋„ˆ ๊ฐ„์˜ ๋น„์œจ์ด ๋งž์ง€ ์•Š์„ ๊ฒฝ์šฐ ์ž๋ฆฌ๊ฐ€ ๋‚จ๊ฒŒ ๋œ๋‹ค.
cover ๊ฐ€๋กœ์„ธ๋กœ ๋น„์œจ ์œ ์ง€ํ•œ ์ฑ„ ์‚ฌ์ด์ฆˆ ์กฐ์ ˆ ๊ฐ€๋Šฅ. ๋น„์œจ์ด ๋งž์ง€ ์•Š๋”๋ผ๋„ ์ปจํ…Œ์ด๋„ˆ ์™„์ „ํžˆ ์ฑ„์›€
(background-size: cover ๊ณผ ๊ฐ™๋‹ค.)
none ๋ณธ๋ž˜์˜ ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ ์œ ์ง€. ์ด๋ฏธ์ง€๊ฐ€ ๊ฐ€์šด๋ฐ๋กœ ๋ณด์—ฌ์ง„๋‹ค.
scale-down none ๋˜๋Š” contain ์ค‘์— ๋” ์ ์ ˆํ•œ ๋ฑกํ–ฅ์œผ๋กœ ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ ์กฐ์ ˆ.
<div class="absolute-img">
    <img class="object-img" src="https://cdn.pixabay.com/photo/2016/03/26/13/09/cup-of-coffee-1280537_1280.jpg">
</div>
.object-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

 

์ตœ๋Œ€๋‹จ์ .

IE ๋ฏธ์ง€์›

 

1-3. display: flex

<div class="flexbox-img">
    <img src="https://cdn.pixabay.com/photo/2016/03/26/13/09/cup-of-coffee-1280537_1280.jpg">
</div>
.flexbox-img {
    display: flex;
    /* justify-content: center; */
    align-items: center;
    width: 300px;
    height: 300px;
    margin: auto;
    border: 10px solid red;
    overflow: hidden;
}
.flexbox-img > img {
    width: auto;
    height: 100%;
}

์ตœ๋Œ€๋‹จ์ .

IE ๋ฏธ์ง€์›

 

 

 

2. background-image

2-1. background-size

  • ๋ฐฐ๊ฒฝ์—์„œ๋งŒ ๋“ค์–ด๊ฐˆ ์ˆ˜ ์žˆ์–ด ์ด๋ฏธ์ง€ ํƒœ๊ทธ์—๋Š” ์‚ฌ์šฉ ๋ถˆ๊ฐ€๋Šฅ.
  • ์„œ๋ฒ„์—์„œ ์ด๋ฏธ์ง€ ์ฃผ์†Œ๋ฅผ ์ „๋‹ฌํ•˜๋Š” ๊ฒฝ์šฐ, ์ธ๋ผ์ธ ์Šคํƒ€์ผ๋กœ background-image๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค.
  • ์ด๋ฏธ์ง€ ํƒœ๊ทธ๋ฅผ ์“ธ ์ˆ˜ ์—†์–ด SEO๋‚˜ ์‚ฌ์šฉ์ž ์ธก๋ฉด์—์„œ๋Š” ๊ธฐ๋Šฅ์ด ๋–จ์–ด์ง.

 

value ์„ค๋ช…
contain ์ด๋ฏธ์ง€์˜ ๊ฐ€๋กœ์„ธ๋กœ ๋น„์œจ ์œ ์ง€. ๋„“์ด์— ๋งž์ถค.
cover ์ด๋ฏธ์ง€์˜ ๊ฐ€๋กœ์„ธ๋กœ ๋น„์œจ ์œ ์ง€. ๋†’์ด์— ๋งž์ถฐ ์ฃผ์–ด์ง„ ํฌ๊ธฐ ๊ฝ‰ ์ฑ„์›€.
์‚ฌ์ด์ฆˆ ์ง์ ‘ ์ž…๋ ฅ
(100%, 200px, 50em ๋“ฑ)
๋น„์œจ์— ๋ฌด๊ด€ํ•˜๊ฒŒ ์ž…๋ ฅํ•œ ๊ฐ’์— ๋งž์ถฐ ์‚ฌ์ด์ฆˆ ์กฐ์ ˆ.
ํ•˜๋‚˜๋งŒ ์ž…๋ ฅํ•˜๋ฉด ๊ฐ€๋กœ๊ฐ’, ์‰ผํ‘œ ์—†์ด ๋‘๊ฐœ ์ž…๋ ฅํ•˜๋ฉด ๊ฐ€๋กœ๊ฐ’๊ณผ ์„ธ๋กœ๊ฐ’ ๋™์‹œ ์„ค์ •.
<div class="bg-img"></div>
.bg-img {
    width: 300px;
    height: 300px;
    margin: auto;
	background-image: url('https://cdn.pixabay.com/photo/2016/03/26/13/09/cup-of-coffee-1280537_1280.jpg');
    background-position: center;
    background-repeat: no-repeat;
    background-size: cover;
}

 

๋ฐฐ๊ฒฝ์ด๋ฏธ์ง€์—์„œ๋งŒ ์‚ฌ์šฉ ์‚ฌ์šฉ.

IE9+ ์ง€์›.

 

 

 

Reference

http://thenewcode.com/723/Seven-Ways-of-Centering-With-CSS

https://nykim.work/86

๋ฐ˜์‘ํ˜•