Academy / Advanced Level: HTML and CSS
CSS Architecture and Methodologies
OOCSS (Object-Oriented CSS)
OOCSS (Object-Oriented CSS) is a methodology that emphasizes the separation of structure and skin (presentation). The goal of OOCSS is to create reusable, flexible styles by treating UI components as “objects.” This separation reduces duplication and ensures that styles are easy to maintain and reuse.
Principles of OOCSS:
- Separation of Structure and Skin:
- Structure: Defines the layout and positioning of elements (e.g., width, height, padding).
- Skin: Defines the visual styling (e.g., colors, borders, fonts).
- Separation of Containers and Content:
- OOCSS encourages reusable styles that aren’t tied to a specific context or container.
Example of OOCSS:
/* Structure (layout) */
.box {
width: 300px;
padding: 20px;
}
/* Skin (visual styling) */
.box--blue {
background-color: #3498db;
color: white;
}
.box--green {
background-color: #2ecc71;
color: white;
}
In this example:
- The box class defines the structure.
- The box--blue and box--green classes define different skins, allowing flexibility in styling without duplicating the layout rules.
Key Benefits of OOCSS:
- Reusability: Encourages creating generic styles that can be reused in different contexts.
- Consistency: Promotes consistent styles across a project by separating layout and design.
- Efficiency: Reduces duplication and makes maintenance easier.