Act #21: Research OOCSS - Object-Oriented CSS
`What is OOCSS?:
OOCSS (Object-Oriented CSS) is a CSS methodology that focuses on separating the structure of HTML from its appearance. It encourages code reuse and modularity by treating UI components as objects.
As with any object-based coding method, the purpose of OOCSS is to encourage code reuse and, ultimately, faster and more efficient stylesheets that are easier to add to and maintain.
Core Principles:
Separation of Structure and Skin: Divide elements’ structure (layout) and visual design (colors, fonts) into separate styles.
Almost every element on a styled Web page has different visual features (i.e. “skins”) that are repeated in different contexts.
An application's skin refers to the visual properties of elements such as:
Colors
Fonts
Shadows
Gradients
Example of core principles of separation of structure and skin
#button { width: 200px; height: 50px; padding: 10px; border: solid 1px #ccc; background: linear-gradient(#ccc, #222); box-shadow: rgba(0, 0, 0, .5) 2px 2px 5px; } #box { width: 400px; overflow: hidden; border: solid 1px #ccc; background: linear-gradient(#ccc, #222); box-shadow: rgba(0, 0, 0, .5) 2px 2px 5px; } #widget { width: 500px; min-height: 200px; overflow: auto; border: solid 1px #ccc; background: linear-gradient(#ccc, #222); box-shadow: rgba(0, 0, 0, .5) 2px 2px 5px; }
Separation of Container and Content: Make components flexible and reusable regardless of where they are placed in the layout.
Example of core principles of separation of container and content.
#sidebar h3 { font-family: Arial, Helvetica, sans-serif; font-size: .8em; line-height: 1; color: #777; text-shadow: rgba(0, 0, 0, .3) 3px 3px 6px; }
Advantages:
Speed- When using OOCSS, just follow the DRY rule: Don't repeat yourself. Consequently, you'll have CSS files that are smaller and quicker to download
Scalability- OOCSS allows you to freely mix and re-apply classes on different elements without much regard for their context.
Efficiency - Having fewer blocks of code makes CSS easier to scan, which makes editing and updating less of a hassle.
Maintainability -Adding or rearranging HTML markups no longer requires you to rethink your entire CSS flow. This is especially helpful for larger ongoing projects.
Readability-When other programmers see your CSS, they should be able to quickly understand its structure.
Readability to other concepts- Understanding the object oriented methodology makes it easier to learn programming languages like Ruby. Conversely, anyone who already understands Ruby can quickly pick up OOCSS.
Practice
<html lang="en">
<head>
<link rel="stylesheet" href="style.css">
<title>Demo</title>
</head>
<body>
<h1 class="Heading">Object-oriented CSS</h1>
<button class="btn">Button</button>
<button class="btn btn--large">Large Button</button>
<button class="btn btn--primary">Primary Button</button>
<button class="btn btn--large btn--primary">Large Primary Button</button>
</body>
</html>
.btn {
display: inline-block;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
text-decoration: none;
text-align: center;
}
.Heading{
color: black;
font-size: 3rem;
font-weight: 600;
text-align: center;
}
.btn {
background-color: #5643c2;
color: #ffffff;
}
.btn--primary {
background-color: blue;
color: #ffffff;
}
.btn--large {
padding: 15px 30px;
font-size: 20px;
background-color: rgb(187, 75, 94);
}
.btn--small {
padding: 5px 10px;
font-size: 12px;
background-color: yellow;
}
.btn:hover {
opacity: 0.8;
}
.btn:active {
transform: translateY(2px);
}
for references: https://www.smashingmagazine.com/2011/12/an-introduction-to-object-oriented-css-oocss/
https://www.keycdn.com/blog/oocss
GitHub repository: https://github.com/Koronuma4Dev/OOCSS