Home > Article > Web Front-end > An in-depth analysis of CSS architecture OOCSS
This article brings you relevant knowledge about css architecture oocss, including separation of structure and skin, separation of structure and content, etc. I hope it will be helpful to everyone.
Object-oriented programming
If you have experience in object-oriented programming, you can skip this section. .
Before entering the study of OOCSS, we must first understand the following object-oriented programming. Object-oriented programming began to emerge in the context of the Artificial Intelligence Group at MIT in the late 1950s, according to Wikipedia:
Object-oriented programming (OOP) is a programming paradigm that represents concepts as having data fields (description properties of an object) and associated procedures called methods. Objects are instances of classes and are used to interact with each other to design applications and computer programs.
Object-oriented also has three major characteristics: inheritance, encapsulation, and polymorphism.
OOP has been widely used in JavaScript and backend languages for a few years, but organizing CSS according to its principles is still relatively new. In layman's terms, OOP is the practice of making your code reusable, efficient, and fast.
There are too many concepts that need not be introduced. Let’s take a look at the Animal class when we get started with JavaScript. I believe everyone has learned and practiced the Animal class to help us understand OOP:
// 基类 / 父类 class Animail { constructor() {} getName() {} } // 子类 class Cat extends Animail { constructor() {} run() {} jump() {} } // 子类 class Fish extends Animail { constructor() {} swim () {} }
What is OOCSS?
Concepts are always hard to understand, so let’s quickly jump into an example and then learn what is OOCSS?
When we were new to CSS, when we organized CSS code, we would sometimes write the following code:
/* 不好的方式 */ .box-1 { border: 1px solid #ccc; width: 200px; height: 200px; border-radius: 10px; } .box-2 { border: 1px solid #ccc; width: 120px; height: 120px border-radius: 10px; }
It is not difficult to find that there are some repeated styles in the code. When maintaining this code, if you want to change the border-radius or border property values, you have to modify them in two places at the same time.
In order to facilitate maintenance, we can extract the duplicate code and put it into a new class name as the base class name, so that when there are new changes, there is no need to maintain two copies of the code:
/* 好的方式 */ /* 重复的代码 */ .box-border{ border: 1px solid #CCC; border-radius: 10px; } .box-1 { width: 200px; height: 200px; } .box-2 { width: 120px; height: 120px; }
In the HTML structure, we can use it like this:
<div class="box-border box-1">Learn OOP</div> <div class="box-border box-2">Learn CSS</div>
If we abstract the newly changed CSS code, we can think of it like this:
If we want Let the styles of the two divs achieve the desired effect. Without the common class name box-border, box-1 and box-2 alone cannot achieve the desired style effect. In other words, box-border is the base class box- 1 and box-2 are subcategories.
This is the OOP concept hard abstracted in CSS, called OOCSS.
However, Nicole Sullivan, the author of OOCSS, used the following sentence to summarize CSS object-oriented programming.
It's a repeating visual pattern, that can be abstracted into an independent snippet of HTML, CSS, and possibly JavaScript.
This is a repeating visual pattern, that can be abstracted into HTML , CSS and possibly JavaScript.
Nicole Sullivan
Understand what OOCSS is. I believe you have a certain understanding of how OOCSS can write scalable and maintainable CSS. At this time, you should also understand , although you may not have heard of the concept of OOCSS before, this skill was definitely used unknowingly in the project.
Okay, what do we actually learn about OOCSS next?
OOCSS (Object-Oriented CSS) is the leading modular or component-based system for organizing CSS. It was first introduced by Nicole Sullivan at the Web Directions North conference in 2008.
She also mentioned that when building OOCSS, abstraction is the first thing to consider, but there are two basic principles to follow:
Separate structure (structure) and skin. You should preserve structure and positioning in the base object, and visual characteristics (such as background or border) in the extending class. This way you don't have to override visual properties.
Separate container and content. Never imitate the structure of HTML in CSS. In other words, don't reference tags or IDs in stylesheets. Instead, try to create and apply classes that describe the use of relevant tags. And keep nested classes to a minimum.
The core of remembering these two principles is to write reusable and maintainable styles.
Separate structure and skin
Skin is the visual attribute we can see, for example:
Colors Color
Fonts
Shadows
GradientsGradient
BackgroundColos Background
The structure is of course our invisible visual attributes, for example:
Height Height
Width Width
Position Position
Margin
Padding
Overflow
这么分离也是有依据的,给你举个生动形象的例子,都打过王者荣耀吧,如果你是忠爱粉可能还花钱买过皮肤,刷刷的一换,英雄瞬间逼格高了不少,我们网页的结构和皮肤相互分离和王者的英雄换肤一个道理。
这个好例子就是我们上面举的这个例子:
/* 好的方式 */ /* 重复的代码 */ .box-border{ border: 1px solid #CCC; border-radius: 10px; } .box-1 { width: 200px; height: 200px; } .box-2 { width: 120px; height: 120px; }
在 HTML 结构:
<div class="box-border box-1">Learn OOP</div> <div class="box-border box-2">Learn CSS</div>
分离容器和内容
我们对着下面这个例子讲解:
<!DOCTYPE html> <html> <head> <style> div { font-size: 20px; } div h2 { font-size: 20px; } </style> </head> <body> <div> <h2></h2> <p></p> </div> </body> </html>
上面这个例子,h2 被锁定在 menu 这个容器里面了,如果一不小心改变了 HTML 的结构就会导致我们写的 CSS 无效,非常的不便于维护,而且作用于 h2 标签上的样式还无法复用,真是让人头疼。
根据容器和内容分离的原则,我们应该让容器和内容有各自的样式,同时避免使用标签选择器,改写得到如下代码
<!DOCTYPE html> <html> <head> <style> .menu { width: 200px; height: 200px; } .menu-title { font-size: 20px; } </style> </head> <body> <div> <h2></h2> <p></p> </div> </body> </html>
OK,这样代码就非常便于维护和复用了,切记在项目中,我们应该禁止使用和位置相关的样式还有标签选择器。
优点和缺点
上面反复强调使用 OOCSS 的好处就是 编写可复用和可维护的样式 这两个特点,此篇我们来总结下 OOCSS 的优缺点:
优点
扩展性: OOCSS 允许您在不同元素上自由混合和重新应用类,而无需过多考虑它们的上下文。一个项目的新手可以重用他们的前辈已经抽象出来的东西,而不是堆积在 CSS 上。
维护性: 添加或重新排列 HTML 标记不再需要您重新考虑整个 CSS 流程。这对于正在进行的大型项目尤其有用。
提高网站速度。 减少重复有助于应用程序运行得更快。CSS 文件习惯于随着网站的复杂性增加而呈指数级扩展,从而增加网页大小。
可读性: 当其他程序员看到您的 CSS 时,他们将能够快速理解其结构。
快速上手: 尤其是对了解面向对象编程的新手来说。
缺点
虽然使用 OOCSS 有很多好处,但也有一些缺点:
不适合小项目: 小型项目不一定需要可扩展性、可读性和可维护性。
增加元素类的数量: 您可能需要将多个类添加到一个元素以说明所有样式元素。这可能会给不熟悉 OOCSS 的人带来一些困惑,并且会使您的标记变得混乱。
有一个学习曲线: 如果您正在使用 OOCSS 而您的同事不熟悉它,这将需要他们在继续之前学习如何使用它,这需要时间。
无语义化的类名: 根据两个核心的分离原则,我们代码中不可能会出现 .btn 这样一个类名搞定样式的情况,我们只会拆分的很细,但同时我们又需要 .btn 这样的业务类名。所以我们需要一种机制来解决这个问题。
语义化和可维护也是需要平衡的,不过对于我更需要的是代码的可维护,对于这个我们可以使用 CSS 预处理器解决,例如 Sass/Less。
Sass/Less 的继承
还记得 OOP 编程三大特性吧,其中之一就是继承,正好对应了 Sass/Less 的 extend,你说这不巧了吗不是。
根据 OOCSS 当我们需要一个按钮:
/* 不好的方式 */ .button-structure { min-width: 100px; padding: 1em; border-radius: 1em; } .button-skip { color: #fff; background: #55acee; }
<button class="button-structure button-skip"></button>
在 Sass 中,我们可以使用 %placeholder 来创建对象,通过 @extend 在类中调用,将其合在一起。这样就可以自己组织代码:
/* 好的方式 */ %button-structure { min-width: 100px; padding: 1em; border-radius: 1em; } %button-skip { color: #fff; background: #55acee; } .btn { @extend %button-structure; @extend %button-skip; }
重点来了,一个业务类名解决:
<button></button>
Less 的继承是通过伪类来实现的 :extend 具体参考 Less Extend,这个就自己去想吧。
总结
今天,我们先是了解了面向对象编程的思想,然后根据其核心思想学习了在 CSS 中如何使用 OOP,还知道了这种组织代码的方式就叫 OOCSS,OOCSS 有两个核心思想,容器与内容、结构和皮肤分离,同时总结了 OOCSS 的优缺点,并针对 OOCSS 无语义化这个重大缺点,我们结合 CSS 预处理器 SASS 给了一个解。
(学习视频分享:css视频教程)
The above is the detailed content of An in-depth analysis of CSS architecture OOCSS. For more information, please follow other related articles on the PHP Chinese website!