[ACCEPTED]-add a class to the li element or the a?-css

Accepted answer
Score: 12

If you're styling the list item, such as 12 in creating a menu, then add the class to 11 the list item. If you want to change the 10 appearance of the link, add it to the anchor 9 tag.

There's really no one right answer. You 8 should style the one that makes the most 7 sense semantically. In many cases you may 6 need to style both the list item and the anchor, in 5 which case you should add the class to the 4 li tag and define your styles for both.

li.myclass {
    /* li styles */

li.myclass a {
    /* link styles */

This 3 is pretty much what alex suggested. The second 2 style targets only those links wrapped in 1 list items of your class.

Score: 5

I generally would add it to the list item.. this 6 gives you the flexibility to target with 5 CSS the list item or the anchor tag it contains

li.class-name {
    /* target li class */


li.class-name a {
    /*target it's anchor */

In 4 production though, you're better off leaving 3 the initial li off the class name when referencing 2 it. It is unnecessary (unless you have a 1 really bad memory).

Score: 1

What is it that you want to style, the list 2 item or the anchor. You probably want to 1 style the anchor, so add it to that

Score: 0

If you want to just assign a style to one 3 of the links in your list then use an id 2 instead of a class. Like so.

<li id="new_style"><a href="">one</a></li>

And style it 1 like so:

#new_style { margin: 1em; font-weight: 600; }

More Related questions