Both are perfectly valid, and which one you use depends on what you want to do.
If you are creating a generic class that you want to be able to use throughout your entire site regardless of element and where the element is, you should only use .class
. A good example for this is something like .icon
which you may want to use on links, list items, headings etc. And you want to be able to use them everywhere.
If you're creating a class that is specific to/only works on one certain type element, it's best to use the element in the selector as well. An example of this would be a bullet list you want to display on one line, since this class requires the HTML to be a <ul>
you should specify it in the CSS as well; ul.inline
. This way you can use the "inline" class name for other elements as well, without the styling affecting both.
If you're only using the class in order to select the element but it shouldn't have any generic styling you should be specific. For example, you may want the first paragraph in your #blog-post
element to be larger, then you should specify both #blog-post
and the class; #blog-post p.first
(note that these types of classes are rarely needed anymore thanks to advanced selectors like :first-of-type
, h2 + p
etc).
Saying that ".link
is the best, a.link
is second best and a long selector is bad" is just wrong. It all depends on the situation.