tags:

views:

37

answers:

1

Hi,

I'm trying to decide the best approach to implementing the majority of CSS styles on my page. The 2 approaches I have recognized are:

1) To attach a class attribute to most elements and style each class like so:

fieldset.MyForm { }
legend.MyForm { }

2) To attach class attributes to container elements and style nested elements like so:

.MyForm fieldset { }
.MyForm legend { }

From my experimentation with both approaches:

1) seems more accurate, with less chance of unwanted inheritances or having to overrule existing classes to get a style working but since styles aren't shared I need to continually redefine them.

2) is more elegant from a backend perspective, as I don't need to define class attributes everywhere, but sometimes requires me to define .Classes .Of .Increasing .Depth to overrule existing styles due to CSS specificity.

What is the better approach in terms of code readability/scalability/maintainability? Is there another, better approach I haven't identified?

Thanks

A: 

To attach class attributes to container elements.

Do it naturally, write less. It's best that you define the selector by the hierarchy, not by each attribute.

Alexander
Do you mean I should define the class/element path to each style, like .MyForm .MyInput fieldset legend { }?
Tom
@Tom Do you have legends outside of fieldsets? Do you have legends inside of .MyInput but not under .MyForm? I'm having a hard time seeing why your rule isn't just .MyInput legend {}.
robertc
@Tom, It is best if you can select your elements in CSS by using hierarchy selectors. It is much more readable, understandable, and your HTML is edited easier.
Alexander