2. itCSS
âą ITCSS â âInverted Triangle CSS' is a new methodology. It involves visualising your entire CSS
project as a layered, upside-down triangle. This hierarchical shape represents a model that will help
you order your CSS in the most effective, least wasteful way.
âą gives us far better scalability and maintainability
3. âą Generic to explicit
We start out with the most generic, low-level, catch-all, unremarkable styles, and eventually progress to more
explicit and specific rules as we move through the project. We might start with our reset, then progress to slightly
more scoped rules like h1â6 {} , right through to extremely explicit rules such as .text-center {} .
Read more at http://technotif.com/manage-large-css-projects-with-itcss/#qKXhAprDAHwLVGpw.99
âą Low specificity to high specificity
The lowest-specificity selectors appear towards the beginning, with specificity steadily increasing as we progress
through the project. We want to ensure that we avoid as much of the Specificity Wars as we can, so we try and
refrain from writing higher-specificity selectors before lower-specificity ones. Weâre always adding specificity in
the same direction, thus avoiding conflicts.
âą Far-reaching to localised
Selectors towards the beginning of the project affect a lot of the DOM, with that reach being progressively
lessened as we go through the codebase. We want to make âpassesâ over the DOM by writing rules that affect
progressively less and less of it. We might start by wiping the margins and paddings off everything, then we might
style every type of element, then narrow that down to every type of element with a certain class applied to it, and
so on. It is this gradual narrowing of reach that gives us the triangle shape. Ordering our projects according to
these key metrics has several benefits. We can begin to share global and far-reaching styles much more effectively
and efficiently, we vastly reduce the likelihood of specificity issues, and we write CSS in a logical and progressive
order. This means greater extensibility and less redundancy, which in turn means less waste and much smaller file
sizes.
4.
5. âą Settings
If you are using a preprocessor, start here. This holds any global settings for your project. Settings like $heading-size-1 should
be defined all variables that holds values that uses
âą Tools
mixins and functions. Any mixin or function that does not need accessing globally should belong in the partial to which it
relates.Examples of global tools might be gradient mixins, font-sizing mixins and so on.
âą Generic
high-level, far reaching styles. This layer is seldom modified,It contains things like Normalize. css, global box-sizing rules,
CSS resets and so on.
âą Elements
Elements layer binds onto bare HTML element (or âtypeâ) selectors only. It is slightly more explicit than the previous layer in
that we are now saying âmake every h1 this bigâ , or âmake every a be a certain colourâ.
âą Objects
Users of OOCSS will be familiar with the concept of objects. This is the first layer in which we find class-based selectors.
These are concerned with styling non-cosmetic design patterns, or âobjectsâ. Objects can range from something as simple as
a .wrapper element, to layout systems
âą Components
The Components layer is where we begin to style recognisable pieces of UI. Weâre still binding onto classes here, so our
specificity hasnât yet increased. However, this layer is more explicit than the last one in that we are now styling explicit,
designed pieces of the DOM.
âą Trumps
This layer beats â or âtrumpsâ â all other layers, and has the power to override anything at all that has gone before it. It is
inelegant and heavyhanded, and contains utility and helper classes, hacks and overrides. A lot of the declarations in this layer
will carry !important (e.g. .text-center { text-align: centre !important; } ). This is the highest specificity layer
7. Object oriented css
âą Object-oriented CSS, at its core, is simply writing cleaner, DRYer CSS.
Itâs just a paradigm shift. The purpose of OOCSS is to encourage code reuse and, ultimately, faster and
more efficient stylesheets that are easier to add to and maintain
8. âą OOCSS is based on two main principles:
The first is to separate the structure from the skin :
Layout and design styling are separate .In other words separation Colors, borders and other visual characteristics
from styling background, color, animation ,border, etc. this means not to mix structure/positioning properties with
skin/styling properties on the same class.
The bad way The good way
.button-1 {
border: 1px solid #ccc;
width: 50px;
height: 50px;
border-radius: 5px;
}
.button-2 {
border: 1px solid #ccc;
width: 70px;
height: 20px ;
border-radius: 5px;
}
button-1 {
width: 50px;
height: 50px;
}
.button-2 {
width: 70px;
height: 20px ;
}
.button-border {
border: 1px solid #ccc;
border-radius: 5px;
}
9. The second is to separate the container from the content:
Break the dependency between the container module and the content objects it contains separating container from
content,A styled element should never be dependent on where itâs at in a page
Essentially, this means ârarely use location-dependent stylesâ.
<input> with .call_info form .left_call_info input <input> , like <input class=âinput_pos input_skin">.
The bad way The good way
.call_info form .left_call_info input {
border: none;
width: 400px;
height: 30px;
background-color: #e9e9e9;
color: #122c49;
padding-left: 5px;
font-size: 10px;
}
.input_pos {
width: 400px;
height: 30px;
padding-left: 5px;
}
.input_skin {
background-color: #e9e9e9;
color: #122c49;
font-size: 10px;
}
10. What about semantic css?
âą You shouldn't care about being non-semantic
The only way to make objects in plain CSS is to define non-semantic classes. However, this comes with
some problems:
âą DOM will be overloaded.
<a href="#" id=âclick_itâ class="btn border box-shadow btn-small btn-blue nice clickable">Click me!</a>
âą There's not a safe way to access some of the DOM elements. there is no best way so you have to
choose what is good for particular case .
document.getElementsByClassName(âclickable â);
x[0].innerHTML = "Hello World!";
Or
document.getElementById(âclick_itâ);
x.innerHTML = "Hello World!";
Or
document.querySelectorAll(â.clickable â);
x[0].innerHTML = "Hello World!";
11. Reusable CSS
âą OOCSSS encourage to avoid location dependent styles , you create smaller modules on the page
and extend them with subclasses as necessary
âą Reusability is also improved because of the OOCSS encourages abstracting CSS structural styles
and skin level styles.
12. OOCSS & CSS Preprocessors
âą OOCSS and CSS preprocessors solve different problems
âą Object oriented CSS is an approach and methodology to achieve the goal while a
preprocessor is a tool to support and enlarge the system.
13. DRY CSS
âą OOCSS focused specifically on abstracting snippets of code, Dry Css focuses primarily on not
repeating yourself..
âą The core recommendation of Dry Css is to group reusable properties, name them and then add all
selectors to the group..
âą Avoid specificity by harnessing the cascade
âą Compared with the OOCSS framework, DRY CSS argues that semantic HTML should be HTML
that reflects the content.
âą If the HTML is out of your control, it is obviously valuable, on the other hand it doesnât
necessarily encourage thoughtful HTML structuring.
14. âLessâ comes into play
The extend directive allows us to easily share styles between selectors.
The bad way The good way
a.twitter {
min-width: 100px;
padding: 1em;
border-radius: 1em;
background: #55acee color: #fff;
}
span.facebook {
min-width: 100px;
padding: 1em;
border-radius: 1em;
background: #3b5998;
color: #fff;
}
.button {
min-width: 100px;
padding: 1em;
}
.button-skin {
border-radius: 1em;
color: #fff;
}
.twitter-background {
background: #55acee;
}
.facebook-background {
background: #3b5998;
}
.btn {
&--twitter {
&:extend(.button);
&:extend(.twitter-background);
&:extend(.button-skin);
}
&--facebook {
&:extend(.button);
&:extend(.facebook-background);
&:extend(.button-skin);
}
}
15. Classes
âą According to most OOCSS methodologies, subclassing is always handled at the HTML level. Itâs better to
avoid attaching not more than 3-4 classes to a single element. When attaching a new class we have to
increase semantic value as well as it necessary or itâs better to mix oocss with dry css.
Compiled css Html Comparison
.button,
.btn--twitter,
.btn--facebook {
min-width: 100px;
padding: 1em;
}
.button-skin,
.btn--twitter,
.btn--facebook {
border-radius: 1em;
color: #fff;
}
.twitter-background,
.btn--twitter {
background: #55acee;
}
.facebook-background,
.btn--facebook {
background: #3b5998;
}
<!-- css way -->
<a href="#" class="twitter">Twitter</a>
<a href="#" class="facebook">Facebook</a>
<!â pure oocss way -->
<a href="#" class="button-skin button btn--twitter
">Twitter</a>
<a href="#" class="button-skin button btn--facebook
">Facebook</a>
<!-- dry oocss way with less -->
<a href="#" class=âbtn--facebook">Twitter</a>
<a href="#" class=âbtn--twitter">Facebook</a>
16. Mixins and OOCSS
âą With preprocessors, we can define so-called "mixins", which have some similarities to functions in
programming languages. In
I suggest you to donât include mixin when you donât give it arguments , you can just use extend and
take dry css as we mentioned in past slides or you can just attach it in your html because dry
preprocessor code does not mean dry css code when we compile our code to css we will end up with
repetitions of various properties.
.round(@radius: 5px) {
/* Safari 3-4, iOS 1-3.2, Android 1.6- */
-webkit-border-radius: @radius;
/* Firefox 1-3.6 */
-moz-border-radius: @radius;
/* Opera 10.5, IE 9, Safari 5, Chrome, Firefox 4, iOS 4, Android 2.1+ */
border-radius: @radius;
}
.form-info {
background-color: #ccc;
border: 2px solid rgba(0,0,0,0.7);
.round( 15px) ;
}
17. Some suggestions to avoid specificity
âą Avoid to use IDs in CSS, (anything you can do with an ID, you can do with a class), ID or just selecte it with just
attribute selector [id = âsome_idâ]
âą Do not nest selectors unnecessarily.
âą Do not qualify selectors unless you have a compelling reason to do so. If.nav {} will work, do not use ul.nav {};
to do so would not only limit the places you can use the .
âą Make heavy use of classes because they are the ideal selector: low specifity great portability, and high reusability.
âą Safely increasing specificity .btn.btn { } âŠwill select based on only one class (.btn) but with double the
specificity. We can take this as far as we need to:.btn.btn.btn.btn { }it just asks the same question n times.
âą Never use !importantant !important declarations should not be used unless they are absolutely
necessary after all other avenues have been exhausted. It ruins the order
âą Descending order of specificity
1. Inline styles
2. ID selectors â (#big)
3. Pseudo Classes (:hover), (:visited) etc.
4. Attributes Selectors â ([href=ââ] , [target=ââ]);
5. Class Selectors â (.clases)
6. Type Selectors (em, h1, p, ul, li)
7. Universal selectors â (*)
18. Choose wisely from the toolkit: mixins, extends, a new module or
subclass
Mixins, extends and classes are tools that allow the reduction of repetition in the the act of writing
CSS. Each of these however results in a very different outcome in the final compiled CSS and each
comes with a potential pitfall. Overuse of mixins results in highly repetetive and bloated CSS,
overuse of classes will start to feel like classitis, and an overuse of extends can result in too many
selectors attatched to the same CSS rule.
Itâs important to find a good balance and have a process for deciding which solution to use.
19. Avoid deep-nested selectors and selectors tightly coupled to HTML
âą At first , when I started to use preprocessors I was very impressed about that I could write nested
css styles and it was repeating the structure of the DOM , but my impression died when first Iâve
compiled
âą So then I realized that to much nesting styles gives you unnecessary specificity and css
downloading time is growing as the size of it, so then I found that there is some kind of
theoretical border for nesting levels : âNote that objects should have at most 4 levels. Most of the
time you'll stay around the 2-3 level range, and the fourth would be the interaction state.â
http://thesassway.com/beginner/the-inception-rule
âą I prefer to use not more then 2 levels but there is always some cases when I really need but I am
trying to keep in mind âdonât nested more then 4 levelsâ
preprocessor code compiled
.container {
.menu-list {
ul {
list-style: none;
li {
padding-left: 10px;
span {
background-image: url() 0 0 no-repeat;
}
}
}
}
}
.container .menu-list ul {
list-style: none;
}
.container .menu-list ul li {
padding-left: 10px;
}
.container .menu-list ul li span {
background-image: url() 0 0 no-repeat;
}
20. Conclusionâą Ultimately, the key to successfully using OOCSS and preprocessors together is understanding the problems that
OOCSS and preprocessors solve as well as the way the final CSS will compile. While CSS preprocessors offer a
number of conveniences to front-end developers, they donât guarantee efficient, maintainable and scalable code.
By creating a clear folder/file structure, maintaining consistent naming conventions, avoiding deep-nested
selectors or CSS/HTML coupling, and choosing wisely from the toolkit, it is possible to benefit from both OOCSS
and preprocessors.
âą OOCSS sources:
âą https://github.com/stubbornella/oocss
âą http://www.slideshare.net/stubbornella/object-oriented-css?qid=311faf43-46d6-4441-88b0-
b07dc8e3d8ab&v=qf1&b=&from_search=1
âą http://appendto.com/2014/04/oocss/
âą http://www.smashingmagazine.com/2011/12/an-introduction-to-object-oriented-css-oocss/
âą http://cwebbdesign.tumblr.com/post/23666803241/scalable-and-maintainable-css-approaches
âą OCSS and preprocessor sources:
âą http://thesassway.com/intermediate/using-object-oriented-css-with-sass
âą http://ianstormtaylor.com/oocss-plus-sass-is-the-best-way-to-css/
âą https://css-tricks.com/the-extend-concept/
âą http://thesassway.com/intermediate/avoid-nested-selectors-for-more-modular-css
âą http://thesassway.com/beginner/the-inception-rule
âą http://alancrissey.com/articles/more-object-oriented-css-with-less/
âą http://blog.mediumequalsmessage.com/guidelines-using-oocss-and-css-preprocessors