tags:

views:

48

answers:

2

What is the best-practice for those examples?

1)

<style type="text/css">
.block .title {color:red}
.anotherBlock .title {color:blue}
</style>
...
<div class="block"> 
   <h3 class="title">SomeTitle</h3>
</div>
<div class="anotherBlock">
   <h3 class="title">anotherTitle</h3> 
</div> 
...

2)

<style type="text/css">
.blockTitle {color:red}
.anotherBlockTitle {color:blue}
</style>
...
<div class="block"> 
   <h3 class="blockTitle">SomeTitle</h3>
</div>
<div class="anotherBlock">
   <h3 class="anotherBlockTitle">anotherTitle</h3> 
</div> 
...

The first code looks cleaner but it can let someone think that h3 tags will have the same style properties.

+6  A: 
<style type="text/css">
.block h3 {color:red}
.anotherBlock h3 {color:blue}
</style>
...
<div class="block"> 
   <h3>SomeTitle</h3>
</div>
<div class="anotherBlock">
   <h3>anotherTitle</h3> 
</div> 

Why ? because it is as readable as your examples but takes less code to write = less time to render in user's browser

eugeneK
thanks for attention to my question. but I asked for another thing.sorry for not enough accurate description.hope this'll make my question clearly:what is the best aproach in such a situation. choosing from those two realizations?
noxvile
(1)<style type="text/css">.block .showAllLinks {color:red}.anotherBlock .showAllLinks {color:blue}</style><div class="block"> <a href="#">SomeTitle</a> ... <a href="#" class="showAllLinks">one</a></div><div class="anotherBlock"> <a href="#">SomeTitle</a> ... <a href="#" class="showAllLinks">ALL</a></div>
noxvile
(2)<style type="text/css">.blockShowAllLinks {color:red}.anotherBlockShowAllLinks {color:blue}</style><div class="block"> <a href="#">SomeTitle</a> ... <a href="#" class="blockShowAllLinks">one</a></div><div class="anotherBlock"> <a href="#">SomeTitle</a> ... <a href="#" class="anotherBlockShowAllLinks">ALL</a></div>
noxvile
@noxvile, both are wrong compared to mine. To know which one is better depends what you do with those DOM elements further. If you intend to use similar CSS properties for let's say title elements then it would be better to leave them as .title inside of parent container so you can access .title only.It really depends, judging by your CSS both are the same for your use.Remember less code, faster to render for user. Another rule is flexibility. Combination of both is the key, with given examples it doesn't really matter how you write them.
eugeneK
+1  A: 

Only use classes when you need to separate from the default style of the html tags themselves (like Eugene's example). Use inheritance whenever possible because the more specific the selectors are, the less likelihood you will have errors.

Aaron Harun