search
HomeWeb Front-endHTML TutorialRefactoring: CSS is also object-oriented_html/css_WEB-ITnose

I first came into contact with object-oriented CSS because the CSS in the project has exceeded 8,000 lines and lacks constraints and management. In the near future or in the near future, there are urgent requirements for reconstruction. In the front-end refactoring, we have already discussed the object-oriented refactoring of JavaScript. At this time, let's take a look at CSS object-oriented and modularity, which provides a very good idea for my work.

First of all, what I want to say is that in this era of turbulent concepts, stubborn pursuit of certain concepts and meanings will not lead to better design and higher productivity. Object-oriented is a This idea, or a methodology, brings some inspiration to CSS reconstruction. This is enough. There is no need to worry about the definition and rigor of OO CSS.

What problem did our CSS code encounter?

The reusability is poor. Looking at the name of a CSS, it is difficult to tell which modules may reference it and which parts of the web page this CSS is used for;

As a result, no one dares to modify and Delete, the following styles can only be piled on top;

Afraid of duplicate CSS names, names like s1, s2, t1, and t2 began to appear. This is tantamount to drinking poison to quench thirst. No one knows what these styles do. ;

As a result, CSS is getting bigger and bigger;

The ideas of high-fidelity artists and business developers are completely different. We see various styles of CSS definitions and naming;

Sometimes, you will also encounter the problem of CSS conflict. It all boils down to that sentence: Complexity is the root of all software problems.

Principles of CSS reconstruction:

Vertical modularization: Create CSS module files from large to small, such as: Public-> Portal-> Channel-> Column-> Content ;

Horizontal modularization: suitable for some highly independent components, such as player modules and pop-up layer modules;

Provide guiding CSS: for example, prepare several sets of styles for list type display , all the list displays in the entire system are gathered into this place. The replacement, comparison and modification of future skins will greatly facilitate the artist;

CSS thumb principle: If two of the same functional areas The styles are very similar, only one is kept!

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

After the rules of CSS are set, they need to be controlled and managed from the perspectives of open source and cost reduction. On the one hand, newly added CSS must act in accordance with the rules. This needs to be given to high-fidelity designers and business developers. Make requirements; on the other hand, the original CSS needs to be gradually reconstructed.

CSS itself supports inheritance and modularization. In addition, on HTML pages, it is easier to use styles and extended styles, such as: "div class="player wmpPlayer"".

If one day, the front-end of the project becomes so complex that UML modeling is required for HTML, then put CSS in it too. DOM and its affiliated and associated models are very suitable for modeling.



Finally ends with the example from 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.   
    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 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}  

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Why are HTML attributes important for web development?Why are HTML attributes important for web development?May 12, 2025 am 12:01 AM

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

What is the purpose of the alt attribute? Why is it important?What is the purpose of the alt attribute? Why is it important?May 11, 2025 am 12:01 AM

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

HTML, CSS, and JavaScript: Examples and Practical ApplicationsHTML, CSS, and JavaScript: Examples and Practical ApplicationsMay 09, 2025 am 12:01 AM

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

How do you set the lang attribute on the  tag? Why is this important?How do you set the lang attribute on the tag? Why is this important?May 08, 2025 am 12:03 AM

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

What is the purpose of HTML attributes?What is the purpose of HTML attributes?May 07, 2025 am 12:01 AM

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

How do you create a list in HTML?How do you create a list in HTML?May 06, 2025 am 12:01 AM

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.

HTML in Action: Examples of Website StructureHTML in Action: Examples of Website StructureMay 05, 2025 am 12:03 AM

HTML is used to build websites with clear structure. 1) Use tags such as, and define the website structure. 2) Examples show the structure of blogs and e-commerce websites. 3) Avoid common mistakes such as incorrect label nesting. 4) Optimize performance by reducing HTTP requests and using semantic tags.

How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools