views:

165

answers:

3

Is it necessary or bad practice to repeat properties that aren't changing in each link type? Like this:

a:link {display: block; width: 50px;}
a:visited {display: block; width: 50px; color: #000;}
a:hover {display: block; width: 50px; color: #FFF}
a:active {display: block; width: 50px; color: #FFF}

Does display block need to be in each? Or is that a bad practice? Also, can you chain link states? Like this:

a:hover, a:active {display: block; width: 50px; color: #FFF}

Additionally, when you have an added id/class on the links, should they inherit the default styles first, then change for the specific id/class? Like this:

a:link {display: block; width: 50px; color: #000;}
....(etc)
a.menu:link {color: #FFF;}
....(etc)

Would the .menu link get display and width from a:link and then just change the color?

THANK YOU SO MUCH for any help clearing all this up!

+2  A: 

No need to repeat the properties, not sure if it's "bad practice" per se but it's definitely something you could optimize.

You can set a { display:block; width:50px } and this will cover all states (unless one is set differently elsewhere. And yes, you should also be able to chain the states.

And you're exactly right, they'll inherit the style assigned to the element type but id/class name variables will take precedence if they are set.

Ben
Thank you! Very helpful!
McFly88
+1  A: 

You can chain link states.

:link and :visited are the most basic definitions for links. Statements made on them will be on every link on the page, even when a link has classes or id.

Said that, :hover and :active don't need display:block, if you declared it on :link and :visited.

Mauricio
Thank you! I wasn't sure if which ones also needed to be set if the properties were the same.
McFly88
+1  A: 

Following Ben's answer:

a{ display:block; width: 50px }
a:hover, a:active{ color: #fff; }
a:visited{color: #000}
Adam Bard
Thank you! Although, isn't that technically the wrong order? (lvha) Does that matter?
McFly88
No it does not. Specificity is more important than order, and a:hover is more specific than a.The approximate rule of thumb is that #id selectors are worth 100 points, .class selectors 10, attribute selectors 1 and :state selectors 2 or something. Add up the points and whichever rule is used is decided by the highest.
Adam Bard