Refactoring: CSS is also object-oriented

Source: Internet
Author: User

The initial exposure to object-oriented CSS is due to the fact that there are more than 8,000 lines of CSS in the project, lack of constraints and management, and there is a compelling need for refactoring in the near future or not far away. In front-end refactoring, we've talked about JavaScript object-oriented refactoring, and at this point look at CSS object-oriented and modular, which gives me a very good idea of the job.

First of all, I would like to say that in this era of the concept of flying, stubbornly pursue certain concepts and meanings, does not lead to better design and higher productivity, object-oriented is a way of thinking, or a methodology, to bring some inspiration to the CSS refactoring, which is enough, there is no need to tangle Oo The definition and rigor of CSS.

What's wrong with our CSS code?

Reusability is poor, look at a CSS name, it is difficult to say which modules may be referenced to it, which CSS is used as part of the Web page;

As a result, no one dared to modify and delete, the latter style only stacked on top;

Afraid of CSS duplicate name, s1, S2, T1, T2 such naming began to appear, this is tantamount to quench thirst, no one knows what these styles are doing;

As a result, the CSS is getting bigger;

The idea of doing hi-fidelity artwork and business developers is completely different, and we see CSS definitions and naming of various styles;

Sometimes, you will encounter CSS conflicts, all this, or it boils down to that sentence: complexity is the root of all software problems.

The principle of CSS refactoring:

Vertical modularity: From large to small set up CSS module files, such as: public----portal-----------content;

Horizontal modularity: Suitable for a number of high-independence components, such as player module, pop-up layer module;

Provide instructional CSS: for example, to prepare several sets of list type display style, the whole system of the list display all collapsed to this place, the future skin replacement, comparison and modification, to a considerable extent to facilitate the art;

CSS Thumb principle: If the two styles in the same functional area are similar, keep only one!

The frame (column) uses the grid to control the width, while the content control height, when the page is composed of several columns, each column width can be fixed or fixed by the channel, but the height needs to depend on the content.

CSS rules are fixed, need to open source and throttling two angles to control and management, on the one hand, the new CSS must follow the rules, this need to the high-fidelity designers and business developers to request, on the other hand, the original CSS needs to be gradually reconstructed.

CSS itself is to support inheritance and modularity, in addition to the HTML page, it can be easier to do the style and extension of the use of style, such as: "Div class=" Player Wmpplayer ".

If one day, the front end of the project is complicated to need to do UML modeling for HTML, then put the CSS in it. The DOM and its affiliated and associated models are inherently good for modeling.



Finally end with the example of http://oocss.org/:

CSS Code
    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. /* **************** CONTENT OBJECTS ***************** */
    20. /* ====== Default spacing ====== */
    21. H1, H2, H3, H4, H5, H6, UL, Ol,dl, p,blockquote {padding:10px;}
    22. H1, H2, H3, H4, H5, h6,img{padding-bottom:0px;}
    23. pre{margin:10px;}
    24. Table h1,table h2,table H3, table H4, table H5, table H6, table P, table UL, table ol, table dl{padding:0;}
    25. /* ====== Elements ====== */
    26. Img{display:block;}
    27. Em{font-style:italic;}
    28. Strong{font-weight:bold;}
    29. hr{border:5px solid #e2e2e2; Border-width: 0 0 5px 0; margin:20px 10px 10px 10px;}
    30. Code{color: #0B8C8F;}
    31. /* ====== headings ====== */
    32. /* H1-.H6 classes should is used to maintain the semantically appropriate heading Levels-not for use on Non-headings */
    33. H1,. H1{font-size:196%; font-weight:normal; font-style:normal; color: #AE0345;}
    34. H2,. H2{font-size:167%; font-weight:normal; font-style:normal; color: #AE0345;}
    35. H3,. H3{font-size:146.5%; font-weight:normal; font-style:normal; color: #DF2B72;}
    36. H4,. H4{font-size:123.1%; font-weight:normal; font-style:normal; color: #333;}
    37. H5,. H5{font-size:108%; font-weight:bold; font-style:normal; color: #AE0345;}
    38. H6,. H6{font-size:108%; font-weight:normal; font-style:italic; color:#333;}
    39. /* If additional headings is needed they should is created via additional classes, never via location dependant styling * /
    40. . category{font-size:108%; font-weight:normal; font-style:normal; text-transform:uppercase; color: #333;}
    41. . Category A{color: #333;}
    42. . important A{font-weight:bold;}
    43. /* Links */
    44. A {color: #036; Font-weight:bold;text-decoration:none}
    45. A:focus, a:hover {text-decoration:underline}
    46. a:visited {color: #005a9c;}
    47. /* ====== Lists ======*/
    48. /* Numbered list */
    49. Ol.simplelist li{list-style-type:decimal; margin-left:40px;}
    50. /* Standard list */
    51. Ul.simplelist li{list-style-type:disc; margin-left:40px;}
    52. /* ====== Tables ====== */
    53. . 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*/}
    54. . data Table {width:100%;border:1px solid #AE0345;}
    55. Th, td{vertical-align:top;border:1px solid #AE0345;}
    56. . TXTC,. Data. TXTC TD,. Data. TXTC Th{text-align:center;}
    57. . TXTL,. Data. TXTL TD,. Data. TXTL Th{text-align:left;}
    58. . TXTR,. Data. txtr TD,. Data. txtr Th{text-align:right;}
    59. . TXTT,. Data. txtt TD,. Data. Txtt Th{vertical-align:top;}
    60. . TXTB,. Data. TXTB TD,. Data. TXTB Th{vertical-align:bottom;}
    61. . TXTM,. Data. TXTM TD,. Data. Txtm Th{vertical-align:middle;}
    62. . Data Th,.data td{padding:3px 20px}
    63. . Data thead tr{#fff0f8;}
    64. . Data th{color: #Font-weight:bold}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.