Tag Clouds



스타일 시트를 경량화하는 11가지 팁

PDF
Jeff Starr씨는 스타일 시트를 경량화(Micro-Optimization)하는 11가지 팁을 소개했습니다. 이것은 코드 전체를 압축(Minify)하는 것이 아닌, CSS 코드 자체를 효율적으로 작성하거나 배포하는 방법입니다.

Tip #1: 색상 값을 짧게 쓰기

색상을 지정하는 속성에 값을 기입할 때 가능하면 가장 짧은 값을 사용합니다.
article { background-color: rgb(255,255,255); } /* WTF? */
article { background-color: #ffffff; } /* better */
article { background-color: #fff; } /* good */

Tip #2: 중복되는 속성을 병합하기

반복적으로 중복되는 속성들은 통합합니다. 이 작업을 자동으로 처리해 주는 온라인 CSS 최적화 서비스도 있으니 참고하세요.
/* before */
#wrap p {
	font-family: Georgia, serif;
	font-weight: normal;
	line-height: 1.33em;
	font-size: 1.22em;
	}
	.
	.
	.
p {
	font-family: Georgia, serif;
	font-weight: normal;
	line-height: 1.33em;
	font-size: 1.33em;
	}

/* after */
p {
	font-family: Georgia, serif;
	font-weight: normal;
	line-height: 1.33em;
	font-size: 1.33em;
	}

Tip #3: 가능한 한 몰아쓰기

CSS 속성들 중에는 여러 값을 인식하는 것들이 있습니다. fontbackground, padding, border 등이 그렇습니다. 이런 경우 한줄로 몰아씁니다.
/* before */
p {
	font-family: Georgia, serif;
	font-weight: normal;
	line-height: 1.33em;
	font-size: 1.33em;
	}

/* after */
p {
	font: normal 1.33em/1.33 Georgia, serif;
	}

/* these 4 properties */
background-color: #fff;
background-image: url(i/dope.png);
background-repeat: repeat-x;
background-position: 0 0;
 
/* can be written as */
background: #fff url(i/dope.png) repeat-x 0 0;

/* these 4 properties */
margin-top:    10px;
margin-right:  20px;
margin-bottom: 10px;
margin-left:   20px;

/* can be written as */
margin: 10px 20px 10px 20px;

/* these 3 properties */
border-width: 1px;
border-style: solid;
border-color: red;

/* can be written as */
border: 1px solid red;

Tip #4: 같은 값은 결합하기

padding 혹은 margin 속성의 값이 동일한 경우 통합하여 지정할 수 있습니다.
 /* before */
margin: 10px 20px 10px 20px;
padding: 10px 10px 10px 10px;

/* after */
margin: 10px 20px;
padding: 10px;

Tip #5: 불필요한 "0"은 생략하기

소수점 이하의 수를 사용하거나 무의마한 "0"은 생략할 수 있습니다.
/* before */
padding: 0.1em;
margin: 10.0em;

/* after */
padding: .1em;
margin: 10em;

Tip #6: 값이 "0"이면 단위 생략하기

값의 크기가 0이라면 단위는 생략해도 무방합니다.
/* before */
padding: 0px;
margin: 0em;

/* before */
padding: 0;
margin: 0;

Tip #7: 마지막 세미콜론 생략하기

속성 기입이 끝나는 마지막의 세미콜론(;)은 생략해도 무방합니다.
/* before*/
p {
	font-family: Georgia, serif;
	font-weight: normal;
	line-height: 1.33em;
	font-size: 1.33em;
	}

/* after */
p {
	font-family: Georgia, serif;
	font-weight: normal;
	line-height: 1.33em;
	font-size: 1.33em
	}

/* optimized */
p { font: normal 1.33em/1.33 Georgia, serif }

Tip #8: 배포하기 전 코멘트 제거하기

작업과정에서 주석을 사용하는 것은 분명 도움이 되고 협업에도 유리합니다. 그러나 사용자에게는 100% 불필요한 데이터이며 무의미한 서버자원과 대역폭을 소비하므로 배포하기 전에는 반드시 코멘트를 제거하도록 합니다.

Tip #9: 공백 제거하기

값을 구분하기 위한 공백을 제외한 나머지 빈 공간은 모두 제거합니다.
/* before */
body {
	font-family: "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
	background-color: #333;
	text-align: center;
	margin: 0px auto;
	font-size: 62.5%;
	color: #FFF;
	}

/* after */
body{font-family:"Trebuchet MS",Verdana,Arial,Helvetica,sans-serif;text-align:center;background:#333;margin:0px auto;font-size:62.5%;color:#fff}

Tip #10: 줄바꿈 제거하기

효율적인 관리를 위해 탭(또는 공백) 그리고 줄바꿈을 삭제합니다. 싱글-라인 포멧으로 작성하는 것이 미친짓으로 보일지 모르겠지만 의외로 실용적인 작성법입니다. 성능 향상을 꾀하면서도 스크롤을 줄일 수 있고 프로퍼티 단위 디버깅이 가능하기 때문입니다.
/* before */
hr {
	margin: 25px 0 25px 0;
	background: #CF7400;
	text-align: left;
	padding: 15px 0;
	display: block;
	border: 0 none;
	color: #CF7400;
	height: 1px;
	clear: both;
	width: 96%;
	}
acronym, abbr {
	border-bottom: 1px dotted #514031;
	cursor: help;
	}
ins { text-decoration: underline; }
del { text-decoration: line-through; }
sup {
	font-size: 10px;
	line-height: 0;
	color: #cccc99;
	}
em       { font-style: italic; }
small    { font-size: 10px;    }
strong   { font-weight: bold;  }
strong:target, h3:target, h4:target {
	background: #CF7400;
	padding-left: 25px;
	}
code, kbd, samp, tt, var {
	font-family: "Courier New", Courier, monospace, sans-serif;
	color: #cccc99; /* #cc9933 #cccc66 #cccc99 */
	font-size: 11px;
	}
	h3 code { font-size: 13px; }
pre {
	border-left: 1px solid #CF7400;
	padding: 10px 0 12px 10px;
	background: #3B2E22;
	overflow: auto;
	margin: 25px 0;
	width: 525px; /* 95% of 555px = 525px */
	}
	pre:hover {
		border-left: 1px solid #FFFFCC;
		background: #3B2E22;
		}


/* after */
hr { background:#CF7400;margin:25px 0;text-align:left;padding:15px 0;display:block;border:0 none;color:#CF7400;height:1px;clear:both;width:96%; }
acronym,abbr { border-bottom:1px dotted #514031;cursor:help; }
ins { text-decoration:underline; }
del { text-decoration:line-through; }
sup { font-size:10px;line-height:0;color:#cc9; }
em { font-style:italic; }
small { font-size:10px; }
strong { font-weight:bold; }
strong:target,h3:target,h4:target { background:#CF7400;padding-left:25px; }
code,kbd,samp,tt,var { font-family:"Courier New",Courier,monospace,sans-serif;color:#cc9;font-size:11px; }
h3 code { font-size:13px; }
pre { border-left:1px solid #CF7400;padding:10px 0 12px 10px;background:#3B2E22;overflow:auto;margin:25px 0;width:525px; }
pre:hover { border-left:1px solid #FFC;background:#3B2E22; }

Tip #11: CSS 유효성 검사하기

끝으로, W3C에서 제공하는 CSS 유효성 검사기를 이용하여 오류가 있는지를 검토하세요. 당신이 놓쳤을 수도 있는 잡다한 오류들을 빠르게 찾아줍니다.

Bonus Tips: 보너스 팁들

이 게시물을 작성하는 동안 몇 가지 다른 아이디어가 떠올랐습니다.
a:link, a:visited {} /* before */
a:link,a:visited{} /* after */

/* before */
h1{color:#111}
h2{color:#333}
h3{color:#777}

/* after */
h1{color:#111}h2{color:#333}h3{color:#777}

위에서 언급한 팁들 중에는 자동으로 처리할 수 있는 것들이 있습니다. 온라인 도구를 사용하거나 서비스 빌드과정에 포함하여 자동화 할 수도 있다는 사실을 잊지 마세요. 그리고 규모가 큰 프로젝트는 배포/개발 버전을 구분해서 관리하는 것이 가장 효율적입니다.


Comments

Trackback : http://firejune.com/trackback/1613

  1. oliverne_

    @firejune 좋은 글 잘봤습니다.^^ 전부 공감가는 내용이나 TIP#7은 유지보수 시 문제가 생기는 경우가 있더군요. 세미콜론 없는 걸 모르고 속성 추가하다가요. 그리고 TIP#3에 오타 있네요. font 줄임 속성요..ㅎ

    Posted by Yun Cheolhun at from web | reply

     
     
  2. firejune

    @oliverne_ 원문에 오타가 있었네요. 수정했습니다. 감사합니다.

    Posted by firejune at from web | reply

     
     
  3. frends_kr

    스타일 시트를 경량화하는 11가지 팁 http://bit.ly/crrT2Z by @firejune

    Posted by FRENDS at from Topsy | reply

     
     
  4. parkto

    RT @firejune: 스타일 시트를 경량화하는 11가지 팁 http://bit.ly/crrT2Z #yam

    Posted by 박태웅 at from TweetDeck | reply

     
     
  5. gyu7e

    RT @parkto: RT @firejune: 스타일 시트를 경량화하는 11가지 팁 http://bit.ly/crrT2Z #yam

    Posted by 나부랭이 at from 립트윗 | reply

     
     
  6. FRENDS_kr

    스타일 시트를 경량화하는 11가지 팁 http://bit.ly/crrT2Z - by @firejune

    Posted by FRENDS at from TweetDeck | reply

     
     
  7. gyu7e

    RT @parkto: RT @firejune: 스타일 시트를 경량화하는 11가지 팁 http://bit.ly/crrT2Z #yam

    Posted by 나부랭이 at from 립트윗 | reply

     
     
  8. katselphrime

    RT @firejune: 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by Youngho Seo at from web | reply

     
     
  9. ifree

    셀렉터 단계를 너무 상세하게 하기 보다는 재활용 가능할 수 있는 선에서 경량하게 줄이는 것도 방법일것 같네요.

    Posted by 부침개 at from me2day

     
     
  10. ifree

    코드 경량화도 좋지만 경량화 때문에 ID나 class를 를 무의미한 약자나 단어로 줄여서 (ab, aa 등) 사용하는 것에 대해서는 어떻게 생각하세요?

    Posted by 부침개 at from me2day

     
     
  11. firejune

    부침개 id나 class가 경량화를 위해 빌드과정에 포함된 자동화 된 것이라면 찬성이지만 사람이 하기에는 가독성이 떨어져 오히려 안좋은 방법으로 생각되네요.

    Posted by 파이어준 at from me2day

     
     
  12. cmaniac

    RT @firejune: 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by cmaniac at from web | reply

     
     
  13. HanBaDa_

    RT @firejune 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by 김주희(JooHee Kim) at from web | reply

     
     
  14. SkyKiDSkr

    RT @HanBaDa_: RT @firejune 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by 차상민(Sangmin Cha) at from TweetDeck | reply

     
     
  15. ifree

    파이어준 빌드과정에서도 줄일수가 있군요 어떻게 돌아가는건지 궁금하네요.. 저도 사람이 하는건 좋지 않는 방법이라고 생각합니다. ^^

    Posted by 부침개 at from me2day

     
     
  16. firejune

    부침개 요즘 웹 개발 프레임웍들은 기상천외한 것들이 아주 많죠. ^^

    Posted by 파이어준 at from me2day

     
     
  17. dreamlife21

    좋은팁이네요~RT @HanBaDa_: RT @firejune 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by Heebok(마중별) at from twtkr | reply

     
     
  18. funnycom

    RT @dreamlife21 좋은팁이네요~RT @HanBaDa_: RT RT good!! RT @firejune 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613 #디자이너_

    Posted by 고경희 at from TwitBird | reply

     
     
  19. javadosa

    RT @dreamlife21: 좋은팁이네요~RT @HanBaDa_: RT @firejune 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by Ryan H. Lee (희동구™) at from twtkr | reply

     
     
  20. headvoy

    생각해볼만한 내용인 것 같아요. 놓치고 있는게 있는지 확인! RT @firejune 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by Toby 윤원진 at from TwitBird | reply

     
     
  21. qmouse80

    RT @headvoy: 생각해볼만한 내용인 것 같아요. 놓치고 있는게 있는지 확인! RT @firejune 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by 서경조 at from twtkr | reply

     
     
  22. funnycom

    RT @dreamlife21 좋은팁이네요~RT @HanBaDa_: RT RT good!! RT @firejune 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613 #디자이너_

    Posted by 고경희 at from TwitBird | reply

     
     
  23. ttoms

    RT @dreamlife21: 좋은팁이네요~RT @HanBaDa_: RT @firejune 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by 황성익 at from Topsy | reply

     
     
  24. idpii

    #웹디당_ RT @firejune 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by 디피 at from web | reply

     
     
  25. bur

    몰아쓰기에서 font 의 경우는 몰아쓰고 아래에서 특정 속성만 변경될 경우 상속때문에 같은 스타일을 전부 다시 써줘야 하기 때문에 몰아쓰기가 오히려 독이 되는 경우도… [글보러가기]

    Posted by 부르 at from me2day

     
     
  26. firejune

    부르 경량화에만 초점을 둔 글이라 꼭 이렇게 해야한다는 건 아닙니다. 상황에 따라서는 융통성을 발휘해야겠죠 ^^;

    Posted by 파이어준 at from me2day

     
     
  27. orangecolor

    RT @firejune 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by Seunghun Lee at from web | reply

     
     
  28. bur

    파이어준 넵.. 혹시 그대로 적용하시는 분이 계실가봐… 참고용으로 ^^. 11가지중 상황에 맞게 잘 쓰면 10%정도는 줄더라구요…

    Posted by 부르 at from me2day

     
     
  29. dracokr

    스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by Draco at from Topsy | reply

     
     
  30. grboard

    RT @DracoKr: 스타일 시트를 경량화하는 11가지 팁 http://firejune.com/1613

    Posted by sirini at from Topsy | reply

     
     
  31. dahlia

    사실 저는 이런 종류의 micro-optimization을 손으로 하는 것에 회의적입니다. 관련 글을 블로그에 썼습니다: 손으로 미세한 최적화를 하지 말자 [글보러가기]

    Posted by 홍민희 at from me2day

     
     
  32. lnyarl

    헐 mod_csstidy 멋지네요.

    Posted by 마개 at from me2day

     
     
  33. lnyarl

    마찬가지로 js도 해주면 좋겠네요.

    Posted by 마개 at from me2day

     
     
  34. d3m3vilurr

    홍민희옹의 mod_csstidy 개발 선언 [글보러가기]

    Posted by 데메 at from me2day

     
     
  35. icenfire

    동감합니다. 단순 경량화는 문제가 많지요

    Posted by 얼음과불의노래 at from me2day

     
     
  36. admire

    오오 … [글보러가기]

    Posted by 효준 at from me2day

     
     
  37. celeste

    손으로 하는 미세한 최적화도 그렇지만, ultimate-optimazation 할 수 있는 한 최대한의 최적화를 하자는 것이 목적이 아닌데도(산으로 가는 프로젝트…) 계속 몰두하는 것도 좀 회의적이에요. ㅠ_ㅠ [글보러가기]

    Posted by 세레 at from me2day

     
     
  38. firejune

    홍민희 네 옳은 말씀이십니다. 손으로 할 일은 아니라는 의견에 전적으로 동감합니다.

    Posted by 파이어준 at from me2day

     
     
  39. avatar

    다행히 대부분 잘 적용하고 있는 방법이었네요.

    Posted by 데미나인 at reply edit

     
     
  40. avatar

    몰아쓰기에서 font 의 경우는 몰아쓰고 아래에서 특정 속성만 변경될 경우 상속때문에 같은 스타일을 전부 다시 써줘야 하기 때문에 몰아쓰기가 오히려 독이 되는 경우도http://www.edhardykleidungshop.com/ed-hardy/mens/watches.html

    Posted by ed hardy uhren at reply edit

     
     
  41. avatar

    captcha