Style Sheet/CSS

๐ŸŽจ SVG ๋‹ค๋ฃจ๊ธฐ ์ •๋ฆฌ (ํฌ๊ธฐ / ์ƒ‰์ƒ ๋ณ€๊ฒฝ)

์ธํŒŒ_ 2022. 3. 17. 15:17

css-svg-๋‹ค๋ฃจ๊ธฐ-ํฌ๊ธฐ-์ƒ‰์ƒ-๋ณ€๊ฒฝ
css-svg-๋‹ค๋ฃจ๊ธฐ-ํฌ๊ธฐ-์ƒ‰์ƒ-๋ณ€๊ฒฝ

SVG (Scalabe Vector Graphic)

SVG๋Š” ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ๋ฒกํ„ฐ ๊ทธ๋ž˜ํ”ฝ(Scalable Vector Graphics)์˜ ์ค„์ž„๋ง์ด๋‹ค.

SVG ์ด๋ฏธ์ง€ ํŒŒ์ผ์€ XML ๋ฌธ๋ฒ•์„ ๊ธฐ๋ณธ์œผ๋กœ ํ•˜๋Š” ๋ฌธ์„œ์ด๋ฉฐ ๋กœ๊ณ ๋‚˜ ์•„์ด์ฝ˜, ๊ทธ๋ž˜ํ”„ ๋“ฑ ๋„๋ฆฌ ์‚ฌ์šฉ๋œ๋‹ค.

์ฆ‰, 2์ฐจ์› ๊ทธ๋ž˜ํ”ฝ์„ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ XMLํŒŒ์ผ ํ˜•์‹์˜ ๋งˆํฌ์—… ์–ธ์–ด์ด๋ผ๊ณ  ๋ด๋„ ๋œ๋‹ค.
๋‹ค๋ฅธ png๊ณผ jpg ์ด๋ฏธ์ง€ ํŒŒ์ผ์— ๋น„ํ•ด ํ…์ŠคํŠธ ํŽธ์ง‘๊ธฐ์—์„œ CSS๋‚˜ JS๋กœ ์ˆ˜์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์ด ๊ฐ€์žฅ ํฐ ์žฅ์ ์ด๋ผ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ฐ€์žฅ ํฐ ํŠน์ง•์œผ๋กœ๋Š” ํ™•์žฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค๋Š” ์ ์—์„œ ํ™•๋Œ€ํ•ด๋„ ํ’ˆ์งˆ์ด ๋–จ์–ด์ง€์ง€ ์•Š๋Š”๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด ํŒŒ์›Œํฌ์ธํŠธ์—์„œ ์ด๋ฏธ์ง€๋ฅผ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ์ค„์ผ๋•Œ ํ™”์งˆ์ด ๊นจ์ง€์ง€ ์•Š๋Š” ๊ฒƒ๋„ ๋ฐ”๋กœ ์ด ์›๋ฆฌ๋ฅผ ์ด์šฉํ•œ ๊ฒƒ์ด๋‹ค.

  • SVG ์ด๋ฏธ์ง€๋Š” ํฌ๊ธฐ๋ฅผ ์ค„์ด๊ฑฐ๋‚˜ ๋Š˜๋ฆฌ๋”๋ผ๋„ ํ™”์งˆ์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š๋Š”๋‹ค.
  • ์›ํ•˜๋Š” ์ƒ‰์ƒ์œผ๋กœ ๋ฐ”๋กœ๋ฐ”๋กœ ์ž์œ ๋กญ๊ฒŒ ์ ์šฉ๊ฐ€๋Šฅ ํ•˜๋‹ค.

 

* ์ผ๋ฐ˜ PNG ํŒŒ์ผ 

css-svg-๋‹ค๋ฃจ๊ธฐ-ํฌ๊ธฐ-์ƒ‰์ƒ-๋ณ€๊ฒฝ

 

* SVG ํŒŒ์ผ 

css-svg-๋‹ค๋ฃจ๊ธฐ-ํฌ๊ธฐ-์ƒ‰์ƒ-๋ณ€๊ฒฝ


SVG ์‚ฌ์šฉํ•˜๊ธฐ

 

SVG ์‚ฝ์ž…

<svg width="100%" height="100%" viewBox="0 0 172 140" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xml:space="preserve" xmlns:serif="http://www.serif.com/" style="fill-rule:evenodd;clip-rule:evenodd;stroke-linejoin:round;stroke-miterlimit:2;">
    <g transform="matrix(1,0,0,1,-282.321,-396.307)">
        <path id="path1" style="fill:rgb(42,169,224);fill-rule:nonzero;" d="M453.826,412.806C447.516,415.605 440.734,417.496 433.617,418.347C440.882,413.992 446.461,407.097 449.088,398.88C442.289,402.913 434.759,405.84 426.743,407.418C420.326,400.58 411.181,396.307 401.06,396.307C381.629,396.307 365.874,412.061 365.874,431.493C365.874,434.25 366.185,436.936 366.785,439.511C337.542,438.044 311.615,424.036 294.26,402.747C291.232,407.944 289.496,413.988 289.496,420.436C289.496,432.644 295.708,443.414 305.149,449.724C299.382,449.541 293.956,447.958 289.212,445.323C289.208,445.469 289.208,445.617 289.208,445.765C289.208,462.813 301.337,477.033 317.434,480.268C314.481,481.072 311.373,481.502 308.164,481.502C305.897,481.502 303.693,481.281 301.544,480.871C306.022,494.849 319.016,505.022 334.414,505.305C322.371,514.743 307.2,520.369 290.714,520.369C287.874,520.369 285.073,520.201 282.321,519.876C297.892,529.861 316.388,535.686 336.258,535.686C400.978,535.686 436.371,482.07 436.371,435.572C436.371,434.046 436.337,432.529 436.269,431.019C443.143,426.059 449.108,419.863 453.825,412.806L453.826,412.806Z"/>
    </g>
</svg>

SVG๋Š” ๋„ํ˜•๊ณผ ์„ ์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๊ณ , ์œ„ ์ฝ”๋“œ๋ฅผ ๋ณผ ์ˆ˜์žˆ๋“ค์ด ๊ธธ๋‹ค๋ž€ g๋‚˜ path์˜ ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด ์ง„ ๊ฒƒ์„ ์•Œ์ˆ˜ ์žˆ๋‹ค.

SVG ํŒŒ์ผ์˜ ์‹ค์ œ ๋ฒกํ„ฐ ๋“œ๋กœ์ž‰ ๋ฐ์ดํ„ฐ๋Š” <svg></svg> ํƒœ๊ทธ ์•ˆ์— ํ‘œํ˜„๋œ๋‹ค.
<g></g>ํƒœ๊ทธ๋Š” ์ขŒํ‘œ๋ฅผ ๋ณด์ •ํ•˜๋Š” ์—ญํ• ์„ ํ•˜๋ฉฐ
<path/>ํƒœ๊ทธ๋Š” ์‹ค์ œ ๋„ํ˜•์„ ๊ทธ๋ฆฌ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค. 
์ •ํ˜• ๋„ํ˜•์„ ๊ทธ๋ฆด๋•Œ๋Š” <ellipse/>, <rectangle/>, <circle/> ๋“ฑ์˜ ๋„ํ˜• ํƒœ๊ทธ๋กœ ํ‘œํ˜„ํ•œ๋‹ค.

 

SVG๋ฅผ html์— ์‚ฝ์ž…ํ•˜๋Š” ๋ฐฉ์‹์€ ๋‹ค์–‘ํ•˜๋‹ค.

์šฐ์„  ์œ„ ์ฝ”๋“œ์™€ ๊ฐ™์ด svgํƒœ๊ทธ ์ž์ฒด๋ฅผ ์ธ๋ผ์ธ์œผ๋กœ ์‚ฝ์ž…ํ•ด๋„ ๋ฌธ์ œ์—†์ด ๋™์ž‘ํ•œ๋‹ค.

์ธ๋ผ์ธ์œผ๋กœ SVG ํƒœ์Šค๋ฅผ ์‚ฝ์ž… ๋ฐฉ์‹์˜ ์žฅ์ ์€ ์ด๋ฏธ์ง€ ๋กœ๋“œ๊ฐ€ ํ•„์š” ์—†๋‹ค๋Š” ์ ์ด๋‹ค.
๋ธŒ๋ผ์šฐ์ €๋Š” ์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ฌ ๋•Œ๋งˆ๋‹ค HTTP ์š”์ฒญ์„ ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ์ด ๊ฒฝ์šฐ์—” ์š”์ฒญ-์‘๋‹ต์˜ ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋กœ๋“œ๋˜๋Š” ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.
ํ•˜์ง€๋งŒ ๋‹จ์ ์€ htmlํฌ๊ธฐ๊ฐ€ ์ปค์ง€๊ณ  ์บ์‹ฑ์ด ๋ถˆ๊ฐ€๋Šฅ ํ•ด์ง„๋‹ค.

 

์ด์™ธ์—๋„ img ํƒœ๊ทธ๋‚˜ object ํƒœ๊ทธ, css์˜ background-image ์†์„ฑ์„ ํ†ตํ•ด์„œ๋„ svg ํ™•์žฅ์ž ํŒŒ์ผ์„ ๋ฌธ์„œ์— ์‚ฝ์ž…์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

<img src="nana.svg" />
<object type="image/svg+xml" data="./potion.svg" class="nana"></object>
.className {
    width: 300px; /* svg ํฌ๊ธฐ ์กฐ์ • */
    height: 120px;
    outline: 2px solid red;
    background-image: url("./potion.svg");
}

SVG ํฌ๊ธฐ ๋ณ€๊ฒฝ 

css-svg-๋‹ค๋ฃจ๊ธฐ-ํฌ๊ธฐ-์ƒ‰์ƒ-๋ณ€๊ฒฝ

์˜์™ธ๋กœ ๊ฐ„๋‹จํ•˜๋‹ค.

svgํƒœ๊ทธ ๋‚ด๋ถ€์— width์™€ height ์†์„ฑ์„ ๋ช…์‹œํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๊ฒŒ ํฌ๊ธฐ๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

<svg width="200" height="auto" viewBox="0 0 1981.355 1651.144" ...>
	...
</svg>

๋‹จ, viewBox ๊ฐœ๋…์€ ์ƒ์†Œํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ๊ฐ„๋‹จํžˆ ๋งํ•˜์ž๋ฉด ๋‹๋ณด๊ธฐ ๊ฐœ๋…์œผ๋กœ ์ดํ•ดํ•˜๋ฉด ๋œ๋‹ค.

๋ทฐํฌํŠธ(viewport)
๋ทฐํฌํŠธ๋Š” svg์˜ ๊ฐ€์‹œ ์˜์—ญ์œผ๋กœ ํ•ด๋‹น ์š”์†Œ์˜ ๋„ˆ๋น„์™€ ๋†’์ด๋ฅผ ์˜๋ฏธํ•œ๋‹ค.
svg๋‚˜ css๋ฅผ ํ†ตํ•ด width์™€ height ํŠน์„ฑ์„ ์„ค์ •ํ•  ๊ฒฝ์šฐ ์ด ๋ทฐํฌํŠธ ์˜์—ญ์ด ๋ฐ”๋€๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
์‰ฝ๊ฒŒ ๋งํ•ด ์˜ํ™”๊ด€์˜ ์Šคํฌ๋ฆฐ ์˜์—ญ(๊ฐ€์‹œ ์˜์—ญ)์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž. ์˜ํ™”๊ด€ ์Šคํฌ๋ฆฐ๋ณด๋‹ค ํฐ ์˜์ƒ์„ ๋น„์ถ”๋ฉด ํ™”๋ฉด์— ์•ˆ ๋ณด์ด๊ฒŒ ๋œ๋‹ค.
๋˜‘๊ฐ™์ด ๋ทฐํฌํŠธ ๋ฐ–์œผ๋กœ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ์‚์ ธ๋‚˜๊ฐ„๋‹ค๋ฉด ๋ณผ ์ˆ˜ ์—†๋‹ค.
 
๋ทฐ๋ฐ•์Šค(viewbox)
๋ทฐ๋ฐ•์Šค๋Š” ์ขŒํ‘œ์™€ ๊ฐ€๋กœ, ์„ธ๋กœ์˜ ๋น„์œจ์„ ๊ฒฐ์ •ํ•˜๋Š” svg์˜ ํŠน์„ฑ์ด๋‹ค (viewBox="x์ขŒํ‘œ  y์ขŒํ‘œ  width๋น„์œจ  height๋น„์œจ)
๋ทฐํฌํŠธ์™€ ๋ทฐ๋ฐ•์Šค๊ฐ€ ๋™์ผํ•˜๋‹ค๋ฉด ์›๋ž˜ ํฌ๊ธฐ์™€ ๋™์ผํ•œ ์—˜๋ฆฌ๋จผํŠธ๊ฐ€ ๋ณด์ธ๋‹ค.
๋ฐ˜๋ฉด์— ๋ทฐํฌํŠธ๊ฐ€ ๋ทฐ๋ฐ•์Šค๋ณด๋‹ค ์ž‘๋‹ค๋ฉด(๋ทฐํฌํŠธ < ๋ทฐ๋ฐ•์Šค) ์ถ•์†Œ๋˜์–ด ๋ณด์ด๊ณ ,
๋ทฐํฌํŠธ๊ฐ€ ๋ทฐ๋ฐ•์Šค๋ณด๋‹ค ํฌ๋‹ค๋ฉด(๋ทฐํฌํŠธ > ๋ทฐ๋ฐ•์Šค) ํ™•๋Œ€๋˜์–ด ๋ณด์ด๊ฒŒ ๋œ๋‹ค.
viewBox๋ฅผ ์„ค์ •ํ•˜๋ฉด์„œ viewBox์˜ ๊ฐ€๋กœ(width), ์„ธ๋กœ(height) ๋น„์œจ(=๋ทฐํฌํŠธ ๋น„์œจ)๋„ ๊ผญ ๊ฐ™์ด ์„ค์ •ํ•ด ์ฃผ๋Š”๊ฒƒ์ด ์ข‹๋‹ค.

 

SVG ๊ฐ•์ œ ๋น„์œจ ์กฐ์ • (๊ฐ€๋กœ/์„ธ๋กœ ํฌ๊ธฐ๋ฅผ ๋ง˜๋Œ€๋กœ)

svg๋ฅผ ์˜์—ญ์— ๊ฐ•์ œ ๊ณ ์ •๋˜์–ด ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ ์ค„์ด๊ณ  ์‹ถ๋‹ค๋ฉด, preserveAspectRatio ์†์„ฑ์„ ์กฐ์ •ํ•˜๋ฉด ๋œ๋‹ค.
svg ์ด๋ฏธ์ง€ ์‚ฌ์ด์ฆˆ(๊ฐ€๋กœ/์„ธ๋กœ)๋Š” ๋น„์œจ๋กœ ๋‚˜์˜ค๋Š”๋ฐ, ์ง์ ‘์ ์œผ๋กœ ์กฐ์ •ํ•˜๊ณ  ์‹ถ์„๋•Œ๋Š” ‘none’์œผ๋กœ ์ง€์ •ํžˆ๋ฉด ๋œ๋‹ค.

<svg width="200" height="300" viewBox="0 0 1981.355 1651.144" ...>
	...
</svg>

css-svg-๋‹ค๋ฃจ๊ธฐ-ํฌ๊ธฐ-์ƒ‰์ƒ-๋ณ€๊ฒฝ

<svg width="200" height="300" viewBox="0 0 1981.355 1651.144" preserveAspectRatio="none" ...>
	...
</svg>

css-svg-๋‹ค๋ฃจ๊ธฐ-ํฌ๊ธฐ-์ƒ‰์ƒ-๋ณ€๊ฒฝ

<svg preserveAspectRatio="xMaxYMax slice" viewBox="0 0 30 40" width="50" height="30">&Smile;</svg>

<image preserveAspectRatio='xMinYMin meet' width='200' height='300' xlink:href='https://www.google.com/images/logos/google_logo_41.png' ></image>

css-svg-๋‹ค๋ฃจ๊ธฐ-ํฌ๊ธฐ-์ƒ‰์ƒ-๋ณ€๊ฒฝ

 

svg ํฌ๊ธฐ ์กฐ์ ˆ ์›น์‚ฌ์ดํŠธ

 

์—ฌ๋Ÿฌ SVG ํŒŒ์ผ ํฌ๊ธฐ๋ฅผ ์‹ ์†ํ•˜๊ฒŒ ๋ฌด๋ฃŒ๋กœ ์กฐ์ ˆํ•˜์„ธ์š”!

SVG ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ๋ฌด๋ฃŒ๋กœ ์กฐ์ ˆํ•˜์„ธ์š”! ํ”ฝ์…€ ๋˜๋Š” ํผ์„ผํŠธ๋ฅผ ์ง€์ •ํ•˜์—ฌ SVG ์ด๋ฏธ์ง€ ํฌ๊ธฐ๋ฅผ ๋ณ€๊ฒฝํ•˜์„ธ์š”.

www.iloveimg.com


SVG ์ƒ‰์ƒ ๋ณ€๊ฒฝ 

svg ํƒœ๊ทธ ์•ˆ์— "style", "fill" ์ด๋ผ๋Š” ์†์„ฑ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

์ด ์†์„ฑ์ด ๋ฒกํ„ฐ ๊ฒฝ๋กœ ๋ฐ์ดํ„ฐ์— ์ƒ‰์ƒ์„ ์ฑ„์šฐ๋Š” ์ปฌ๋Ÿฌ ๊ฐ’์„ ์ง€์ •ํ•˜๋Š” ์†์„ฑ์ด๋‹ค.

css-svg-๋‹ค๋ฃจ๊ธฐ-ํฌ๊ธฐ-์ƒ‰์ƒ-๋ณ€๊ฒฝ

 

fill ํ”„๋กœํผํ‹ฐ๋ฅผ ๋ฐ”๋กœ ์ธ๋ผ์ธ์œผ๋กœ ์‚ฌ์šฉํ•ด๋„ ๋˜๊ณ ,

<svg width="44" height="44" viewBox="0 0 104 104" fill="yellow">
	<path d="M8.64453 8.64453L92.2962 31.0589L31.0589 92.2962L8.64453 8.64453Z" stroke="currentColor" stroke-width="8"></path>
</svg>

 

์ธ๋ผ์ธ style ํ”„๋กœํผํ‹ฐ ์•ˆ์— css ์†์„ฑ๊ฐ’์œผ๋กœ์„œ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค.

<svg width="44" height="44" viewBox="0 0 104 104" style="fill: yellow">
	<path d="M8.64453 8.64453L92.2962 31.0589L31.0589 92.2962L8.64453 8.64453Z" stroke="currentColor" stroke-width="8"></path>
</svg>

svg style์ด๋ผ๊ณ  ํ•ด์„œ ํŠน๋ณ„ํ•œ๊ฒƒ ์—†๋‹ค.

๋˜‘๊ฐ™์ด ๋งˆํฌ์—… ์Šคํƒ€์ผ ์†์„ฑ์ด๋ผ, ๋‹น์—ฐํžˆ cssํŒŒ์ผ๋กœ ๋ถ„๋ฆฌํ•ด ์ œ์–ด๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

svg {
	fill: yellow;
}
fill์†์„ฑ์„ ๋ฐ˜๋“œ์‹œ <svg>ํƒœ๊ทธ์—์„œ๋งŒ ์“ธ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.
๋งŒ์ผ ๋ณต์žกํ•œ ๊ทธ๋ฆผ์˜ svg์ผ๊ฒฝ์šฐ, ๊ฐ๊ฐ์˜ <path> ํƒœ๊ทธ ์•ˆ์—๋‹ค๊ฐ€ fill ์†์„ฑ์„ ์ง€์ •ํ•ด์ฃผ๋ฉด ์ปฌ๋Ÿฌํ’€ํ•œ svg์ด๋ฏธ์ง€๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

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

https://mingoogle.tistory.com/9