2
nav ul {
}

nav ul li {
    margin-left: 7px;
}

nav.ul li a, a:link, a:visited {
    float: left;
    padding: 7px;
    margin-left: 15px;
    color: #ffffff;
    text-decoration: none;
    font-weight: bold;
}

nav ul li a:hover {
}

I only want the code above to style the elements within the <nav></nav>

Now however it does as well style the <a href=""> outside the nav element. How can I make sure it does what I want?

3
  • 1
    This: nav.ul li a, a:link, a:visited doesn't inherit. Should be: nav.ul li a, nav.ul li a:link, nav.ul li a:visited. Commented Jul 9, 2012 at 1:31
  • 1
    Ok, instead of adding this to all the answers I'll just put it here: There's a period (.) between nav and ul (it's nav.ul instead of nav ul) so that should be removed too, otherwise it's looking for a <nav class="ul"> when the earlier code sample has those as separate elements. Commented Jul 9, 2012 at 1:35
  • @nbsp - You are correct, the ` ` (space) selector is correct, the . (or subclass) is wrong and being perpetrated in most of the answers. Commented Jul 9, 2012 at 1:37

5 Answers 5

5

The rules as you have them, will only apply within a nav element, except this:

nav.ul li a, a:link, a:visited {

This rule applies to a:link and a:visited. You want this:

nav.ul li a, nav.ul li a:link, nav.ul li a:visited {
Sign up to request clarification or add additional context in comments.

Comments

3

It's because this line is incorrect:

nav.ul li a, a:link, a:visited {

It should be:

nav ul li a, nav ul li a:link, nav ul li a:visited {

After the comma (,) the CSS is applying to all a tags as there's not preceding selectors specified. Also you've got a period (.) in the nav ul in the first part.

Comments

1

Change your rule:

nav.ul li a, a:link, a:visited

to:

nav.ul li a, nav.ul li a:link, nav.ul li a:visited

By omitting the nav.ul li part between the comma separation, you're effectively applying to to links outside of the nav.ul li.

Comments

1

Your code is targeting a:link and a:visited without using descendent selectors. Using the following should fix your problem:

nav ul li a, nav ul li a:link, nav ul li a:visited {
    float: left;
    padding: 7px;
    margin-left: 15px;
    color: #ffffff;
    text-decoration: none;
    font-weight: bold;
}

1 Comment

There's a period (.) between nav and ul (it's nav.ul instead of nav ul) so that should be removed too, otherwise it's looking for a <nav class="ul"> when the earlier code sample has those as separate elements.
0

Make sure that all your selectors are preceded with nav ul li.

nav ul li a, nav ul li a:link, nav ul li a:visited {
    float: left;
    padding: 7px;
    margin-left: 15px;
    color: #ffffff;
    text-decoration: none;
    font-weight: bold;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.