Is it better to import a pre-written rule for each CSS style or to write your own rule (OOCSS)?

て烟熏妆下的殇ゞ 提交于 2019-12-09 03:36:25

The point of the OOCSS way isn't to primarily borrow CSS snippets from other libraries and only write new rules when you're doing something no one has done before. Obviously that would be impossible and probably not any better than rolling your own.

The point of OOCSS is to abstract your styles as much as possible. The more specific they are, the less reusable they are, and when you do that you end up writing much more CSS.

To put that in programming terms, instead of making a car class and then a truck class that share all sorts of common methods, you make a vehicle class and then car and truck extend from that.

The equivalent of that in CSS is to use classes as mixins. For example, if you're using a lot of <ul> elements and you find that you're constantly setting margin and padding to 0 and list style to none, you should probably write a .reset class that does that for you and apply the class to your <ul> instead of writing the same three rules over and over again for all your navigation and list items.

Another point is that you need to think about your abstractions from a visual standpoint, for example, if you find that most of the sections of your site live in boxes with borders around them, you could create a basic class like:

.bordered-box {
    border-style:solid;
    border-width:1px;
    margin-bottom:20px;
    padding:20px;
}

Then you'd create other classes to mix in styles that aren't defined on .bordered-box such as color, and width, etc.

Think of your css classes as a toolbox that you're going to use to build a site instead of thinking of it merely as a list of properties you can add to elements to make them look a certain way.

To your point about HTML like this: class="ital size-medium sans-ser txtJ med-height dark-blue", it's important to distinguish between creating CSS classes that define visual objects and CSS classes that simply define CSS properties. The latter is ridiculous. If you're going to do that you may as well just use inline styles.

I think using something like .txtC or .dark-blue is against the spirit of CSS, where you are supposed to be able to change the entire look and feel of your site simply by using a different stylesheet.

For example of an issue that could arise, say you decide you want all your headers to be red instead of blue. Rather than simply updating your CSS to .header { color: red }, you have to go through every page of your HTML and replace the dark-blue class with red. Or you could change your CSS to dark-blue { color: red; }, but then the class name doesn't make any sense.

If you add a class for every rule imaginable, you'll just end up back in the dark ages of inline styles and align="center" on every element.

If you are going to use "object-oriented" CSS, it should be used in a way more like, you have your .header class that affects all your headers, but then if you wanted a different font color for your home page header, you would add an additional .header.home { color: cyan; } CSS rule. You want your CSS IDs and class names to describe the content it's affecting, rather than the styles it's providing.

"Object-oriented CSS" is really just a design pattern for how to get most out of your CSS and is basicly the same approach Jonathan Snooks calls SMACSS.

Whether you call it OOCSS or SMACSS, the key to the approach is that you create generic UI elements like the nav abstraction. These UI elements can then be enhanced with more specific features by adding extra classes to the element and/or a container element. Or, as an alternative, you can add your own custom CSS rules using the element's ID or semantic classes.

Cascade Framework is a brand new CSS framework based on this approach. It gives you optimal performance, optimal flexibility and optimal modularity with just a tiny footprint.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!