CSSλ₯Ό μ΅œμ ν™”ν•˜κ³  파일크기λ₯Ό μ€„μ΄λŠ” 15κ°€μ§€ 방법

2012. 12. 4. 10:03Β·Before. 2021

CSSμ½”λ“œλ₯Ό 효율적이고 μ΅œμ ν™”ν•˜λ©΄μ„œ νŒŒμΌμ‚¬μ΄μ¦ˆλ₯Ό 쀄일 수 μžˆλŠ” CSSμ΅œμ ν™” 방법에 λŒ€ν•΄μ„œ μ–˜κΈ°λ₯Ό ν•˜λ €κ³  ν•œλ‹€.


- μ†Œκ°œ
λ”μš± 더 λ³΅μž‘ν•œ CSSμ½”λ“œλ₯Ό μ“°κ²Œ λ˜λ©΄μ„œ μ΄λŸ¬ν•œ κΈ°μˆ μ€ CSS파일 μ‚¬μ΄μ¦ˆλ₯Ό 단지 λͺ‡kilobytes라도
쀄여야할것이닀.



1. CSS Sprite의 μ‚¬μš©

"CSS SpriteλŠ” image request의 μš”μ²­μ„ κ°μ†Œμ‹œν‚€λŠ” 방법이닀. μ—¬λŸ¬μ΄λ―Έμ§€λ“€μ„ ν•˜λ‚˜μ˜ μ΄λ―Έμ§€λ‘œ
ν•©μΉ˜κ³  CSS background-image ν˜Ήμ€  background-position속성을 μ΄μš©ν•˜μ—¬ μ΄λ―Έμ§€μ˜ 뢀뢄듀을
ν›Œλ₯­ν•˜κ²Œ 보여쀀닀. - Yahoo Deloper Rule(http://developer.yahoo.com/performance/rules.html)

μ΄λŸ¬ν•œ 방법은 HTTP Request와 μ›Ήμ‚¬μ΄νŠΈμ˜ νŠΈλž˜ν”½μ„ 효율적으둜 쀄여쀀닀.

CSS Sprtie μ°Έκ³ 
- A List Aprt - CSS Sprite (http://www.alistapart.com/articles/sprites)
- Azamsharp - Combining Images, CSS Sprites to Increase Performance (http://azamsharp.com/Posts/216_Combining_Images_CSS_Sprites_to_Increase_Performance.aspx)


2. λͺ¨λ“  CSSνŒŒμΌμ„ ν•˜λ‚˜μ˜ 파일둜 합쳐라

μ‚¬μš©μžλ“€μ΄ μ›Ήμ‚¬μ΄νŠΈλ₯Ό μ ‘μ†ν• λ•Œ νŽ˜μ΄μ§€μ˜ λͺ¨λ“  객체(이미지 λ˜λŠ” 슀크립트)λ₯Ό μ„œλ²„μ— μš”μ²­ν•˜κ²Œλœλ‹€.
당신이 λͺ‡κ°œμ˜ 객체듀을 λ‹€μ‹œ 읽어 듀인닀면 νŽ˜μ΄μ§€μ˜ 지연을 λ”μš± 가쀑 μ‹œν‚¬ 것이고,
μ΄λŸ¬ν•œ HTTP Request듀은 λ‹Ήμ‹  μ‚¬μ΄νŠΈμ˜ μ‘λ‹΅μ‹œκ°„μ„ λŠλ¦¬κ²Œν•  것이닀.

이 κΈ°μˆ μ€ CSS Sprite와 μœ μ‚¬ν•˜λ‹€. (μ΅œμ†Œν•œ λͺ©ν‘œλŠ” κ°™λ‹€) HTTP Requestλ₯Ό κ°μ†Œμ‹œν‚€λŠ” 것이닀.
3개의 CSSνŒŒμΌμ„ λ‘œλ”©ν•˜κ³  μžˆλ‹€λ©΄ λΈŒλΌμš°μ €λŠ” 3개의 HTTP Request의 μš”μ²­μ„ 보낸닀. κ°„λ‹¨ν•˜κ²Œ
μˆ˜ν•™μ μœΌλ‘œ 1개의 CSS File = 1개의 HTTP Reuqest인 것이닀. λ‚˜λŠ” 일반적인 μ›Ήμ‚¬μ΄νŠΈμ—μ„œ
6~7개의 CSSνŒŒμΌμ„ Headerμ—μ„œ μ‚¬μš©ν•œλ‹€. μ•žμ— 방법을 μ‚¬μš©ν•œλ‹€λ©΄ λ‘œλ”©μ‹œκ°„μ„ λ”μš± 효율적으둜
λ§Œλ“€μ–΄ 쀄 것이닀.

- Before

<link rel="stylesheet" type="text/css" href="content.css" />  
<link rel="stylesheet" type="text/css" href="about.css" />  
<link rel="stylesheet" type="text/css" href="contact.css" />  

- After
<link rel="stylesheet" type="text/css" href="layout.css" />  


3. μ™ΈλΆ€νŒŒμΌ(external file)둜 CSSλ₯Ό λ§Œλ“€μ–΄λΌ

μ™ΈλΆ€νŒŒμΌ(external file)을 μ‚¬μš©ν•˜λ©΄ CSS νŒŒμΌλ“€μ΄ λΈŒλΌμš°μ Έμ— μ˜ν•˜μ—¬ μΊμ‹œλ˜μ–΄μ Έ
더 λΉ λ₯Έ νŽ˜μ΄μ§€λ₯Ό λ§Œλ“€ 수 μžˆλ‹€. CSSλ₯Ό νŽ˜μ΄μ§€λ‚΄ inline으둜 μ‚¬μš©ν•˜κ²Œ 되면 맀번
HTML을 μš”μ²­ν• λ•Œ λ§ˆλ‹€ λ‹€μš΄λ˜μ–΄ μ§„λ‹€. μ΄λŸ¬ν•œ HTTP requestλ₯Ό 쀄이기 μœ„ν•΄μ„œλŠ”
μ™ΈλΆ€νŒŒμΌ(external file)을 μ‚¬μš©ν•˜λ©΄ λœλ‹€. ν•˜μ§€λ§Œ HTML의 ν¬κΈ°λŠ” λŠ˜λ‚˜κ²Œ λœλ‹€.
κ·ΈλŸ¬λ‚˜ external file(css)κ°€ λΈŒλΌμš°μ €μ— μ˜ν•΄ μΊμ‹œλ˜μ–΄μ§€λ©΄, HTML λ¬Έμ„œμ˜ 
ν¬κΈ°λŠ” μ»€μ§€μ§€λ§Œ HTTP Request의 μš”μ²­μ€ 쀄어듀것이닀. 
- yahoo (http://developer.yahoo.net/blog/archives/2007/07/rule_8_make_jav.html)


μ™ΈλΆ€ μžλ°”μŠ€ν¬λ¦½νŠΈ λ˜λŠ” cssνŒŒμΌμ„ λ™μ μœΌλ‘œ λ‘œλ”©ν•˜λŠ” 법

- 전톡적인 방법
<head>
<script type="text/javascript" src="myscript.js"></script>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>

- DOM Methodλ₯Ό μ΄μš©ν•˜μ—¬ μž‘μ„±ν•˜λŠ” 법

 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 if (typeof fileref!="undefined")
  document.getElementsByTagName("head")[0].appendChild(fileref)
}

loadjscssfile("myscript.js", "js") //dynamically load and add this .js file
loadjscssfile("javascript.php", "js") //dynamically load "javascript.php" as a JavaScript file
loadjscssfile("mystyle.css", "css") ////dynamically load and add this .css file

- λ‘œλ”©μ‹œ 이미 ν•΄λ‹ΉνŒŒμΌμ΄ λ‘œλ”©λ˜μ—ˆλŠ”μ§€λ₯Ό μ²΄ν¬ν•˜λŠ” 방법
var filesadded="" //list of files already added

function checkloadjscssfile(filename, filetype){
 if (filesadded.indexOf("["+filename+"]")==-1){
  loadjscssfile(filename, filetype)
  filesadded+="["+filename+"]" //List of files added in the form "[filename1],[filename2],etc"
 }
 else
  alert("file already added!")
}

checkloadjscssfile("myscript.js", "js") //success
checkloadjscssfile("myscript.js", "js") //redundant file, so file not added


4. cssνŒŒμΌμ€ header/top에 μœ„μΉ˜ν•΄μ•Όν•œλ‹€.

λ¬Έμ„œμ˜ header에 stylesheetsλ₯Ό μ΄λ™μ‹œν‚€κ²Œ 되면 νŽ˜μ΄μ§€μ˜ λ‘œλ”©μ„ λ”μš± λΉ λ₯΄κ²Œ ν•  수 μžˆλ‹€.
이 방법은 νŽ˜μ΄μ§€μ˜ λ Œλ”λ§μ„ 훨씬 λΉ λ₯΄κ²Œν•΄μ€€λ‹€. - yahoo(http://developer.yahoo.com/performance/rules.html#css_top)

head에 stylesheetsλ₯Ό μœ„μΉ˜μ‹œν‚΄μœΌλ‘œμ¨ 두가지 문제λ₯Ό ν”Όν•  수 μžˆλ‹€.
μŠ€νƒ€μΌμ΄ μ μš©λ˜μ§€ μ•Šμ€ λ‚΄μš©λ“€μ΄ 빈 ν°μƒ‰νŽ˜μ΄μ§€λ‚˜ λ²ˆμ©κ±°λ¦Όμ„ ν”Όν•  수 μžˆλ‹€.
Steve Souders씨가 stylesheetsλ₯Ό μ›ΉνŽ˜μ΄μ§€μ˜ header뢀뢄에 λ„£μ§€ μ•Šμ•˜μ„λ•Œμ˜ λ¬Έμ œμ μ„
ν…ŒμŠ€νŠΈν•΄ λ†“μ•˜λ‹€.(http://stevesouders.com/hpws/rule-css-top.php)

Before
<html>  
    <head>  
        <title>CSS</title>  
        <style>  
        body {   
            font-family:verdana;   
            margin:0;   
            padding:0;   
        }   
           
        h1 {   
            font-weight:700;       
            margin:5px 0;   
        }   
           
        ......   
        ......   
        ......   
           
        </style>  
    </head>  
</html> 


After
<html>  
    <head>  
        <title>CSS</title>  
        <link rel="stylesheet" type="text/css" href="style.css"/>  
    </head>  
</html> 

<html>
 <head>
  <title>CSS</title>
  <link rel="stylesheet" type="text/css" href="style.css"/>
 </head>
</html>


5. @import λŒ€μ‹ μ— Linkλ₯Ό μ‚¬μš©ν•΄λΌ

http://seye2.egloos.com/2319809 (이곳을 μ°Έμ‘°ν•˜λ©΄ λœλ‹€.)


6. CSSλ₯Ό 짧게 써라.

같은 속성듀을 같이 λ¬Άμ–΄ κ·Έλ£Ήν™”ν•¨μœΌλ‘œμ¨ CSSλ₯Ό 쀄이고 μ΅œμ ν™” μ‹œν‚€λŠ”λ° 도움이 될 것이닀.
μ—¬λŸ¬μ€„μ— 속성을 μ‚¬μš©ν•˜λŠ” λŒ€μ‹ μ— ν•˜λ‚˜μ˜ 라인에 μ‚¬μš©ν•  수 μžˆλ‹€. 
μ•„λž˜μ˜ 예제λ₯Ό μ°Έμ‘°ν•˜λ©΄ λœλ‹€.

CSS Shorthand

/* MARGIN */
h1 {margin:1em 0 2em 0.5em;}

h1 {margin-top:1em;
    margin-right:0;
    margin-bottom:2em;
    margin-left:0.5em;
}

/* BORDER */
h1 {border:1px solid #000;}

h1 {border-width:1px;
    border-style:solid;
    border-color:#000;
}

/* BORDER WIDTH */
h1 {border-width:1px 2px 3px 4px;}

h1 {border-top-width:1px;
 border-right-width:2px;
 border-bottom-width:3px;
 border-left-width:4px;
}

/* BACKGROUND */
div {background:#f00 url(background.gif) no-repeat fixed 0 0;}

div {background-color:#f00;
  background-image:url(background.gif);
  background-repeat:no-repeat;
  background-attachment:fixed;
  background-position:0 0;
}

/* FONT */
h1 {font:italic small-caps bold 1em/140% "Lucida Grande",sans-serif;}

h1 {font-style:italic;
 font-variant:small-caps;
 font-weight:bold;
 font-size:1em;
 line-height:140%;
 font-family:"Lucida Grande",sans-serif;
}

/* LIST STYLE */
ul {list-style:square inside url(image.gif);}

ul {list-style-type:square;
 list-style-position:inside;
 list-style-image:url(image.gif);
}

/* OUTLINE */
h1 {outline:#f00 solid 2px;}

h1 {outline-color:#f00;
 outline-style:solid;
 outline-width:2px;
}


7. λΉ„μŠ·ν•œ μŠ€νƒ€μΌμ„ κ·Έλ£Ήν™”

이런 일은 μ˜€λžœμ‹œκ°„μ˜ κ°œλ°œν›„μ— μ‰½κ²Œ λ°œμƒλœλ‹€. 같은 μŠ€νƒ€μΌμ΄ μ‘΄μž¬ν•œλ‹€λŠ” 것을 μžŠμ–΄λ²„λ¦¬κ³ 
λ°˜λ³΅ν•΄μ„œ μ„ μ–Έν•œλ‹€. κ·Έλ£Ήν™” ν•¨μœΌλ‘œμ¨ 쒀더 μ‰½κ²Œ 뢄석할 수 있고, μœ μ‚¬ν•œ μŠ€νƒ€μΌμ„
μ‰½κ²Œ κ·Έλ£Ήν™” ν•  수 μžˆλ‹€.

Before
h1 {padding:5px 0; font-family:verdana; font-weight:700;}
#box1 .heading  {padding:5px 0; font-family:verdana; font-weight:700;}
#box2 .heading  {padding:5px 0; font-family:verdana; font-weight:700;}

After
h1, #box1 .heading, #box2 .heading  {padding:5px 0; font-family:verdana; font-weight:700;}

κ·ΈλŸ¬λ‚˜ λŒ€λΆ€λΆ„μ˜ 경우 CSS Styles은 μ•„λž˜μ™€ 같이 ν•˜λŠ”κ²ƒμ΄ ν•„μˆ˜μ μ΄μ§€λŠ” μ•Šλ‹€.

Before
#nav a.home {padding:5px 0; font-family:verdana; font-weight:700; background:#fff url(home.gif) no-repeat 5px 5px}
#nav a.portfolio {padding:5px 0; font-family:verdana; font-weight:700; background:#fff url(portfolio.gif) no-repeat 5px 5px}
#nav a.contact {padding:5px 0; font-family:verdana; font-weight:700; background:#fff url(contact.gif) no-repeat 5px 5px}
#nav a.about {padding:5px 0; font-family:verdana; font-weight:700; background:#fff url(about.gif) no-repeat 5px 5px}

After
#nav a {padding:5px 0; font-family:verdana; font-weight:700; background:#fff no-repeat 5px 5px}

#nav a.home {background:url(home.gif)}
#nav a.portfolio {background:url(portfolio.gif)}
#nav a.contact {background:url(contact.gif)}
#nav a.about {background:url(about.gif)}

λ‹€μ–‘ν•œ μ˜ˆμ œλ“€μ„ λ³΄μ•˜κ³ , 이것듀은 CSS파일크기λ₯Ό 쀄이고 가독성을 λ†’μ΄λŠ”λ° ν›Œλ₯­ν•œ 방법이닀.


8. 쀄내림을 μ€„μ΄μž

h2 {
 font-family:verdana;
 padding:0;
 margin:5px 0;
 color:#333;
 text-decoration:underline; 
}

/* you can do it like this */
h2 {font-family:verdana;padding:0;margin:5px 0;color:#333;text-decoration:underline;}


9. λ§ˆμ§€λ§‰ μ„Έλ―Έμ½œλ‘ μ„ μ—†μ• μž.

h2 {font-family:verdana;color:#333;}

/* removed */
h2 {font-family:verdana;color:#333}


10. 주석을 κ°„λ‹¨ν•˜κ²Œ

/************************************/
/*          Content Page            */
/************************************/

vs

/* content page */


11. 색섀정을 κ°„λ‹¨ν•˜κ²Œ

전체 μ½”λ“œ 컬러λ₯Ό μ‚¬μš©ν•˜λŠ” λŒ€μ‹ μ—:
#FFFFFF, #113344, #AABBCC, #FF0000

μ€„μ—¬μ„œ μ‚¬μš©ν•  수 μžˆλ‹€.
#FFF, #134, #ABC, #F00

κ·ΈλŸ¬λ‚˜ μ•„λž˜μ™€ 같은 κ²½μš°λŠ” λ°”κΏ€ 수 μ—†λ‹€.:
#C4C4C4, #1A2B3C


12. px을 μ—†μ• μž

h2 {padding:0px; margin:0px;}

/* removed */
h2 {padding:0; margin:0}


13. μ€„μ—¬μ„œ(μ•½μ–΄λ“±)을 μ‚¬μš©ν•˜μž

- μœ„μ—μ„œ μ–ΈκΈ‰ν•œλ°λ‘œ νŒŒμΌμ‚¬μ΄μ¦ˆλ₯Ό 쀄 일 수 μžˆλ‹€.


14. λΆˆν•„μš”ν•œ ν΄λž˜μŠ€μ‚¬μš©μ„ μ€„μ΄μž.

- IE Conditional Comments 내에 link stylesheetsλ₯Ό μ§€μ›ν•œλ‹€.
- 일반적인 CSS핡을 μ§€μ›ν•œλ‹€.(* html #fooλŒ€μ‹ μ— html #fooλ₯Ό μ‚¬μš©ν•œλ‹€)


15. CSS Compression Toolsλ₯Ό μ‚¬μš©ν•œλ‹€.


CSS Optimizer(http://www.cssoptimiser.com/)
Flumpcakes CSS Optimizer(http://flumpcakes.co.uk/css/optimiser/)
Clean CSS (http://flumpcakes.co.uk/css/optimiser/)
CSS Drive CSS Compressor(http://www.cssdrive.com/index.php/main/csscompressor/)
Online YUI Compressor(http://www.refresh-sf.com/yui/)
Style Neat(http://www.styleneat.com/)

μ°Έκ³ μ‚¬μ΄νŠΈ
- http://developer.yahoo.com/performance/rules.html
- http://www.sohtanaka.com/web-design/optimizing-css-tutorial/
- http://www.dailyblogtips.com/speed-up-your-site-optimize-your-css/
- http://www.pat-burt.com/web-development/10-tips-for-a-smaller-css-file

원문 μ‚¬μ΄νŠΈ
- http://www.queness.com/post/588/15-ways-to-optimize-css-and-reduce-css-file-size

-http://seye2.egloos.com/2428232

μ €μž‘μžν‘œμ‹œ

'Before. 2021' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

ν…μŠ€νŠΈ μ€„λ°”κΏˆ νƒœκ·Έ  (0) 2012.12.21
CSS3 box-Shadow  (0) 2012.12.13
textarea 크기쑰정 막기  (0) 2012.11.27
input νƒœκ·Έ disabled & readonly 속성  (0) 2012.11.26
μ›Ή μ ‘κ·Όμ„± μ§€ν‘œλ³„ 전문가심사 평가 κΈ°μ€€  (0) 2012.11.09
'Before. 2021' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€
  • ν…μŠ€νŠΈ μ€„λ°”κΏˆ νƒœκ·Έ
  • CSS3 box-Shadow
  • textarea 크기쑰정 막기
  • input νƒœκ·Έ disabled & readonly 속성
빙고ꡬ맛탕
빙고ꡬ맛탕
μ›ΉνΌλΈ”λ¦¬μ…”λ‘œ μ‹œμž‘ν•΄ ν”„λ‘ νŠΈμ—”λ“œ 개발자둜 μ„±μž₯ν•˜κΈ°κΉŒμ§€μ˜ 여정을 λ‹΄λ‹€.
  • 빙고ꡬ맛탕
    π•Žπ”Όπ”Ή 𝔸𝕃𝕃 𝕃𝕆𝔾
    빙고ꡬ맛탕
  • 전체
    였늘
    μ–΄μ œ
    • 전체보기 (40)
      • Before. 2021 (40)
  • 졜근 κΈ€

  • hELLOΒ· Designed Byμ •μƒμš°.v4.10.2
빙고ꡬ맛탕
CSSλ₯Ό μ΅œμ ν™”ν•˜κ³  파일크기λ₯Ό μ€„μ΄λŠ” 15κ°€μ§€ 방법
μƒλ‹¨μœΌλ‘œ

ν‹°μŠ€ν† λ¦¬νˆ΄λ°”