1

How can I target one element on hover and then effect an other element?

Here's the HTML:

        <header>
            <h1 style="float: left; margin-top: 2%;">&nbsp;&nbsp;&nbsp;Naughty Mama.</h1>
            <nav>
                <ul>
                    <li><a href="profile.php">My Profile</a></li>
                    <li><a href="inbox.php">Inbox</a></li>
                    <li><a href="notifications.php">Notifications</a></li>
                </ul>
            </nav>
        </header>

Here's the CSS:

@CHARSET "ISO-8859-1";

header {
    background-color: #ddd;
}

nav {
    float: right;
    margin-right: 5%;
    margin-top: 2%;
}

nav ul li{
    display: inline;
    padding: 15px;
    background-color: #eee;
}

nav ul li a {
    color: #fff;
    text-decoration: none;
}

nav ul li a:hover {
    color: #000;
}

I want to effect the nav ul li when I hover on nav ul li a.

Hope I am clear.

5
  • 3
    There is no parent selector in CSS. Commented Jun 30, 2013 at 19:39
  • 1
    you cannot untill CSS level 4 will be usable . Commented Jun 30, 2013 at 19:39
  • possible duplicate of On a CSS hover event, can I change another div's styling? Commented Jun 30, 2013 at 19:39
  • This is easily done with JS though. Commented Jun 30, 2013 at 19:39
  • I know how to do that with JS but I wanted a CSS solution! :) Thanks. Anyone post the answer so I can accept it :) The first one will be accepted! Commented Jun 30, 2013 at 19:41

1 Answer 1

2

You may style <a> so it covers <li>. apply hover effect as background to <a> :) http://codepen.io/anon/pen/zyDLA

header {
  background-color: #ddd;
}

nav {
  float: right;
  margin-right: 5%;
  margin-top: 2%;
}

nav ul li{
  display: inline-block;
  background-color: #eee;
  vertical-align:top;
}

nav ul li a {
  color: #fff;
  text-decoration: none;
  display:block;
  padding:15px;
}

nav ul li a:hover {
  color: #000;
  background:#48a
}
Sign up to request clarification or add additional context in comments.

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.