Object Oriented CSS is to design a code to reuse for faster and more efficient stylesheets. It is easier to add and maintain.

Two principles of Object Oriented CSS

1. Separate structure and Skin

This means to define repeating visual features (like border and background styles) as separate “skins” that you can mix-and-match with your various objects in the webpage.

Example:
Repeating visual patterns

#box {
width: 75px;
height: 30px;
background: #eed;
border: 1px solid #000;
border-radius: 2px;
}

#block{
width: 250px;
height: 500px;
background: #eed;
border: 1px solid #000;
border-radius: 2px;
}

Instead we can add below style, to reuse with less code

.box {
width: 75px;
height:30px;
}.block {
width: 250px;
height: 500px;
}

.skin {
background: #eed;
border: 1px solid #000;
border-radius: 2px;
}

HTML

<div class=”box skin”>box content</div>
<div class=”block skin”>block content</div>

2. Separate container and Skin

The idea here is to consciously differentiate the rules of containers and the content that comes within them, so that content can be plugged into any containers irrespective of their parents. Avoid location dependent style.

For an example, instead of having sidebar class, we can have module class. By naming sidebar we are restricting to a certain position of our page. We are localizing it. Instead if we create module class, in future if we want to use same styling to some other position of our page. We can use it.

The CSS specifications define a specific format that vendors should follow.
The identifiers may begin with ‘-‘ (dash) or ‘_’ (underscore).

‘-‘ + vendor specific identifier + ‘-‘ + meaningful name

Microsoft
-ms-

Microsoft Office
mso-

Mozilla Foundation (Gecko-based browsers)
-moz-

Opera Software
-oo-

Safari (and other WebKit-based browsers)
-webkit-

Konqueror browser
-khtml-

The WAP Forum
-wap-

 

$ua = $_SERVER[‘HTTP_USER_AGENT’];

if(strstr($ua,”MSIE”)) echo ‘IE';

if(strstr($ua,”Firefox”)) echo ‘FIREFOX';

if(strstr($ua,”Chrome”)) echo ‘CHROME';

if(strstr($ua,”Safari”)) echo ‘SAFARI';

if(strstr($ua,”Opera”)) echo ‘Opera';

 

Quirks mode is a technique used my modern browsers, which mimics the behavior of older browsers.

Closure: Variables that are in scope and accessed from a function declaration will stay accessible by that function.

Code Snippet for Closure:

(function() {
function sum(x) {
var temp = 5;
return function (y) {
console.log(x + y + (++temp));
}
}

var total = sum(3); // total is now a closure.
total(1); // 10
total(1); // 11
})();