重構:CSS也面向對象

最初接觸到面向對象的CSS仍是由於項目中的CSS已經超過八千行,缺少約束和管理,在近期或者是不遠的未來,有迫切的要求須要重構。在前端重構中,咱們已經討論過了JavaScript面向對象的重構,在這個時候再看一看CSS面向對象和模塊化,這給個人工做提供了很是好的思路。css

首先,我要說的是,在這個概念翻飛的年代裏,執拗地追求某些概念和涵義,並不能帶來更優秀的設計和更高的生產力,面向對象是一種思路,或者說是一種方法論,給CSS重構帶來一些啓示,這就足夠了,沒有必要去糾結OO CSS的定義和嚴謹性。前端

 

咱們的CSS代碼遇到了什麼問題?app

重用性差,看着一個CSS的名稱,很難說出哪些模塊可能引用到了它,這個CSS是用做網頁的哪些部分的;框架

結果誰也不敢修改和刪除,後面的樣式只能往上面堆積;模塊化

怕CSS重名,s一、s二、t一、t2這樣的命名開始出現了,這無異於飲鴆止渴,沒有人知道這些樣式是作什麼的;ui

因而,CSS愈來愈大了;spa

作高保真的美工和業務開發人員思路是徹底不一樣的,咱們看到各類風格的CSS定義和命名;.net

有時候,也會遇到CSS衝突的問題,這一切,仍是歸結爲那一句話:複雜是一切軟件問題的根源。設計

CSS重構的原則:code

縱向模塊化:由大到小創建CSS模塊文件,好比:公用 -> 門戶 -> 頻道 -> 欄目 -> 內容;

橫向模塊化:適用於一些獨立性高的組件,好比播放器模塊、彈出層模塊;

提供指導性CSS:例如,準備幾套list類型展現的樣式,把整個系統中的list展現所有收攏到這一處來,將來皮膚的更換、比較和修改,至關程度上便利了美工;

CSS的拇指原則:若是同一功能區域的兩個樣式很相近,只保留一個!

框架(欄目)用網格來控制寬度,而內容控制高度,當頁面上由若干個欄目組成,每一個欄目的寬度能夠固定或者被頻道按比例固定,可是高度須要取決於內容。

CSS的規則定下來之後,須要開源和節流兩個角度去控制與管理,一方面,新加入的CSS必須按照規則行事,這點須要給高保真設計人員和業務開發人員提要求;另外一方面,原有CSS須要進行逐步的重構。

CSS自己是支持繼承和模塊化的,另外在HTML頁面上,也能夠比較容易地作到樣式和擴展樣式的使用,好比:《div class="player wmpPlayer"》。

若是有一天,項目的前端複雜到了須要對HTML也作UML建模的時候,那麼把CSS也放進去吧。DOM和其附屬、關聯模型,自己就是很適合作建模的。



 

最後以http://oocss.org/的例子結尾:

Css代碼   收藏代碼
    1. /* **************** TEMPLATE ***************** */   
    2. /* ====== Page Head, Body, and Foot ====== */   
    3. body{/*_text-align:center;*/}/* IE5.5 */   
    4. .body{overflow:hidden; _overflow:visible; _zoom:1;}   
    5. .page{margin: 0 auto; width: 950px;/*_text-align:left;*/} /* wraps other template elems to set width */ /* text-align IE5.5 */   
    6. /* "old school" and "liquid" extend page to allow for different page widths */   
    7. .oldSchool{width:750px;}   
    8. .gs960{width:960px;}   
    9. .liquid{extends:.page; width: auto;margin:0;}   
    10. /* ====== Columns ====== */   
    11. .main{overflow: hidden;_overflow:visible;_zoom:1;}   
    12. .leftCol{float:left; width:250px;_margin-right:-3px;}   
    13. .rightCol{float:right; width: 300px;_margin-left:-3px;}   
    14. /* extend columns to allow for common column widths */   
    15. .gMail{width:160px;}   
    16. .gCal{width:180px;}   
    17. .yahoo{width:240px;}   
    18. .myYahoo{width:300px;}  
    19.   
    20. /* **************** CONTENT OBJECTS ***************** */   
    21. /* ====== Default spacing ====== */   
    22. h1, h2, h3, h4, h5, h6, ul, ol,dl, p,blockquote {padding:10px;}   
    23. h1, h2, h3, h4, h5, h6,img{padding-bottom:0px;}   
    24. pre{margin: 10px;}   
    25. table h1,table h2,table h3, table h4, table h5, table h6, table p, table ul, table ol, table dl{padding:0;}   
    26. /* ====== Elements ====== */   
    27. img{display:block;}   
    28. em{font-style: italic;}   
    29. strong{font-weight:bold;}   
    30. hr{border: 5px solid #e2e2e2; border-width: 0 5px 0; margin: 20px 10px 10px 10px;}   
    31. code{color:#0B8C8F;}   
    32. /* ====== Headings ====== */   
    33. /* .h1-.h6 classes should be used to maintain the semantically appropriate heading levels - NOT for use on non-headings */   
    34. h1, .h1{font-size:196%; font-weight:normal; font-style: normal; color:#AE0345;}   
    35. h2, .h2{font-size:167%; font-weight:normal; font-style: normal; color:#AE0345;}   
    36. h3, .h3{font-size:146.5%; font-weight:normal; font-style: normal; color:#DF2B72;}   
    37. h4, .h4{font-size:123.1%; font-weight:normal; font-style: normal; color: #333;}   
    38. h5, .h5{font-size:108%; font-weight:bold; font-style: normal; color:#AE0345;}   
    39. h6, .h6{font-size:108%; font-weight:normal; font-style: italic; color:#333;}   
    40. /* if additional headings are needed they should be created via additional classes, never via location dependant styling */   
    41. .category{font-size:108%; font-weight:normal; font-style: normal; text-transform:uppercase; color: #333;}   
    42. .category a{color: #333;}   
    43. .important a{font-weight:bold;}   
    44. /* links */   
    45. a { color: #036; font-weight:bold;text-decoration: none }   
    46. a:focus, a:hover { text-decoration: underline }   
    47. a:visited { color:#005a9c; }   
    48. /* ====== Lists ======*/   
    49. /* numbered list */   
    50. ol.simpleList li{list-style-type: decimal; margin-left:40px;}   
    51. /* standard list */   
    52. ul.simpleList li{list-style-type:disc; margin-left:40px;}   
    53. /* ====== Tables ====== */   
    54. .data{padding: 20px; position:relative; zoom:1;vertical-align: top;border-right:solid 1px transparent;/* border fixes a FF2 bug which causes the data table to overlay its borders*/}   
    55. .data table {width:100%;border:1px solid #AE0345;}   
    56. th, td{vertical-align:top;border:1px solid #AE0345;}   
    57. .txtC, .data .txtC td, .data .txtC th{text-align:center;}   
    58. .txtL, .data .txtL td, .data .txtL th{text-align:left;}   
    59. .txtR, .data .txtR td, .data .txtR th{text-align:right;}   
    60. .txtT, .data .txtT td, .data .txtT th{vertical-align:top;}   
    61. .txtB, .data .txtB td, .data .txtB th{vertical-align:bottom;}   
    62. .txtM, .data .txtM td, .data .txtM th{vertical-align:middle;}   
    63. .data th,.data td{padding:3px 20px}   
    64. .data thead tr{#fff0f8;}   
    65. .data th{color: #000; font-weight:bold}  
相關文章
相關標籤/搜索