开发者

Why would you have a css class that only modifies a single property?

I've often seen css with classes that only modify a single property. For example,

.ns-center-text{
    text-align: center;
}

What are the advantages and dis开发者_开发百科advantages of this technique?


Advantages

  • You can add a class in the HTML for quick and easy presentational things.
  • You only have to add this declaration once in your CSS, instead of many times for each selector (or grouping a whole bunch of them).

Disadvantages

  • You tie presentation to the information layer.
  • You may end up with many classes on your elements.
  • Classes should be descriptive of content, not their presentation.
  • Have to touch the HTML to affect the website's presentation.

I'd recommend against using this.


You can combine them.

<div class="center block red one_third">
   ...
</div>

So you can mix and match properties for elements that have similar properties.


Separation and consistency.

I prefer to have all CSS styles separated in a .css file, independent from the pages that they provide styles for, and this is a pattern I apply consistently.

It can get a bit messy when you start having a mixture of CSS specified in both .css files and as style attributes within pages.


Maintenance in future and ease to change/add.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜