如果使用CSS激活子级或孙级链接,则将父级链接设置为活动状态

时间:2015-06-28 11:52:46

标签: html css drop-down-menu menu css-selectors

我有一个带有此CSS的HTML / CSS菜单,用于活动链接:

 $lienphoto = "adresse sur serveur".$nomimage;

$data = str_replace('data:image/jpg;base64,', '', $image);

$data = str_replace(' ', '+', $data);

$data = base64_decode($data);

$success = file_put_contents($lienphoto, $data);

小提琴:http://jsfiddle.net/kyvbzssd/

如果顶级父链接中的任何子链接或其子链接具有活动类,如何将其设置为活动链接?

5 个答案:

答案 0 :(得分:11)

将样式添加到li元素:http://jsfiddle.net/kyvbzssd/3/

.navigation ul li{
    padding: 80px 9px 10px;
}
.navigation ul li a,.navigation ul li{
    font-size: 13px;
    font-weight: 700;
    color: #4d4d4d;
    text-decoration: none;
    display: inline-block;
    margin-bottom: 0;
    text-transform: uppercase;
    border-bottom: 2px solid #fff;
    -webkit-transition: background-color .2s linear, padding-top .2s linear;
    -moz-transition: background-color .2s linear, padding-top .2s linear;
    -o-transition: background-color .2s linear, padding-top .2s linear;
    -ms-transition: background-color .2s linear, padding-top .2s linear;
    transition: background-color .2s linear, padding-top .2s linear;
}
.navigation ul li a:hover,.navigation ul li:hover,.navigation .active-nav {
    background-color: #F36F25; /*#373737;*/
    text-shadow: none;
    color: #f7f7f7!important;
    -webkit-box-shadow: 0 3px 0 #ff8833;
    box-shadow: 0 3px 0 #ff8833;
}

答案 1 :(得分:6)

这是一个纯CSS解决方案,使用:hover状态并重新使用它。

  • 在这里,你应用你想要的风格:hover状态:
li:hover a {
  color: orange;
  font-weight: 700
}
  • 现在,您可以阻止li儿童徘徊在
li:hover li a {
  color: none;
  font-weight: 400
}

最后,您为:hover孩子

重新执行li
li:hover li:hover a {
  color: orange;
  font-weight: 700
}

这是一个片段:(这只是一个工作演示,我只使用了您的HTML代码)



li:hover a {
  color: orange;
  font-weight: 700
}
li:hover li a {
  color: none;
  font-weight: 400
}
li:hover li:hover a {
  color: orange;
  font-weight: 700
}

<nav id="main-navigation" class="navigation cf">
  <ul>
    <li><a href="#">Homepage</a>
    </li>
    <li class="">
      <a href="#">About Us</a>
      <ul>
        <li><a href="#">The Company</a>
        </li>
        <li><a href="#">Testimonials</a>
        </li>
        <li><a href="#">Careers</a>
        </li>
        <li><a href="#">Meet The Team</a>
        </li>
      </ul>
    </li>
    <li>
      <a href="#">Services</a>
      <ul>
        <li><a href="#">Broadband</a>
        </li>
        <li>
          <a href="#">Telecoms</a>
          <ul>
            <li><a href="#" class="active-nav">Hosted IP PBX</a>
            </li>
            <li><a href="#">Telephone Numbers</a>
            </li>
            <li><a href="#">Traditional Lines</a>
            </li>
            <li><a href="#">Telephone Systems</a>
            </li>
          </ul>
        </li>
        <li><a href="#">I.T. Support</a>
        </li>
        <li><a href="#">Offsite Backup</a>
        </li>
        <li><a href="#">Web Hosting</a>
        </li>
        <li><a href="#">Hosted Exchange</a>
        </li>
        <li><a href="#">Networking</a>
        </li>
        <li>
          <a href="#">Security</a>
          <ul>
            <li><a href="#">CCTV Systems</a>
            </li>
            <li><a href="#">Access Control</a>
            </li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="#">Login</a>
    </li>
    <li>
      <a href="#">Online Shop</a>
      <ul>
        <li><a href="#">Shopping Cart</a>
        </li>
      </ul>
    </li>
    <li><a href="#">Support</a>
    </li>
    <li><a href="#">Contact Us</a>
    </li>
  </ul>
</nav>
&#13;
&#13;
&#13;

与OP讨论的更新(在聊天室中),这是一个包含OP代码的代码(由我工作):

&#13;
&#13;
/*OP' code simplified */

/*1st menu*/

.navigation ul {
  float: right;
  position: relative;
  z-index: 1200;
  margin-right: 15px;
}
.navigation ul li {
  display: inline-block;
  position: relative;
}
.navigation ul li a {
  font-size: 13px;
  font-weight: 700;
  color: #4d4d4d;
  text-decoration: none;
  display: inline-block;
  margin-bottom: 0;
  text-transform: uppercase;
  border-bottom: 2px solid #fff;
  transition: background-color .2s linear, padding-top .2s linear;
  padding: 80px 9px 10px;
}
.navigation ul li a:hover {
  background-color: #F36F25;
  /*#373737;*/
  text-shadow: none;
  color: #f7f7f7!important;
  box-shadow: 0 3px 0 #ff8833;
}
.navigation ul li a span {
  font-family: Arial, sans-serif;
  text-transform: uppercase;
  font-size: 9px;
  display: block;
  line-height: 14px;
  color: #9f9f9f;
}
/*2nd menu */

.navigation ul ul {
  margin-top: 60px;
  position: absolute;
  top: -999em;
  width: 180px;
  background: #fff;
  border-top: 3px solid #ff8833;
  box-shadow: 0 8px 6px -6px rgba(0, 0, 0, 0.4);
}
.navigation ul ul li {
  width: 100%;
  margin-left: 0;
  padding: 0;
}
.navigation ul ul li a:hover {
  background-color: #F36F25;
  /*#373737;*/
  box-shadow: none;
}
.navigation ul ul li a {
  color: #707070;
  font-size: 11px;
  margin-left: 0;
  border: none;
  width: 150px;
  transition: none;
  padding: 8px 15px;
}
.navigation ul li:hover ul {
  left: 0;
  top: 2.5em;
  z-index: 99;
}
/*3rd menu */

.navigation ul li:hover li ul {
  top: -999em;
  margin-top: 0;
}
.navigation ul li li:hover ul {
  left: 180px;
  top: 0;
}
.navigation ul li li:hover li ul {
  top: -999em;
}
.navigation ul ul li a {
  position: relative;
}
.navigation ul ul li a i {
  position: absolute;
  right: 15px;
  line-height: 23px;
  font-size: 14px;
}
/* my code */

.navigation ul li:hover a {
  background: #F36F25;
  font-weight: 700
}
.navigation li:hover li a {
  background: none;
  font-weight: 400
}
.navigation ul li:hover li:hover a {
  background: #F36F25;
  font-weight: 700
}
/*2nd sub-level menu only */
.navigation ul li:hover li:hover > ul li a {
  background: none;
  font-weight: 400
}
.navigation ul li:hover li:hover > ul li:hover a {
  background: #F36F25;
  font-weight: 700
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/prefixfree/1.0.7/prefixfree.min.js"></script>
<nav id="main-navigation" class="navigation cf">
  <ul>
    <li><a href="#">Homepage</a>

    </li>
    <li> <a href="#">About Us</a>

      <ul>
        <li><a href="#">The Company</a>

        </li>
        <li><a href="#">Testimonials</a>

        </li>
        <li><a href="#">Careers</a>

        </li>
        <li><a href="#">Meet The Team</a>

        </li>
      </ul>
    </li>
    <li> <a href="#">Services</a>

      <ul>
        <li><a href="#">Broadband</a>

        </li>
        <li> <a href="#">Telecoms</a>

          <ul>
            <li><a href="#">Hosted IP PBX</a>

            </li>
            <li><a href="#">Telephone Numbers</a>

            </li>
            <li><a href="#">Traditional Lines</a>

            </li>
            <li><a href="#">Telephone Systems</a>

            </li>
          </ul>
        </li>
        <li><a href="#">I.T. Support</a>

        </li>
        <li><a href="#">Offsite Backup</a>

        </li>
        <li><a href="#">Web Hosting</a>

        </li>
        <li><a href="#">Hosted Exchange</a>

        </li>
        <li><a href="#">Networking</a>

        </li>
        <li> <a href="#">Security</a>

          <ul>
            <li><a href="#">CCTV Systems</a>

            </li>
            <li><a href="#">Access Control</a>

            </li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="#">Login</a>

    </li>
    <li> <a href="#">Online Shop</a>

      <ul>
        <li><a href="#">Shopping Cart</a>

        </li>
      </ul>
    </li>
    <li><a href="#">Support</a>

    </li>
    <li><a href="#">Contact Us</a>

    </li>
  </ul>
</nav>
&#13;
&#13;
&#13;

以下是

FIDDLE

答案 2 :(得分:6)

您可以使用选择器.navigation li:hover > a设置当前处于活动状态的菜单项 AND 其所有祖先菜单项(父级,祖父级,...)。

我将此添加到您的演示中,删除了未使用的样式,优化了选择器并进行了其他一些改进。

您可以在下面的JSFiddle和代码段中找到结果。

小提琴:

<强> http://jsfiddle.net/py72ms93/1/

代码:

* {
    box-sizing: border-box;
}

body {
    background: #fff;
    margin: 0;
    padding: 0;
}

main, footer {
    margin: 0;
    padding: 20px;
    float: left;
    width: 100%;
}

p, h1 {
    margin-left: auto;
    margin-right: auto;
    width: 80%;
}

main {
    border: 0 solid #d9d9d9;
    border-width: 3px 0;
    background: #eee;
}

header, footer {
    background: #fff;
    text-align: right;
}

.navigation {
    background: #fff;
    float: right;
    width: 100%;
}

.navigation ul {
    background: #fff;
    border: 0 solid #ff8833;
    margin: 0 15px 0 0;
    padding: 0;
    float: right;
    position: relative;
    z-index: 1200;
}

.navigation li ul {
    position: absolute;
    left: 0;
    top: -999em;
    z-index: 99;
    width: 210px;
    border-width: 3px 0 0 0;
    -webkit-box-shadow: 0 8px 6px -6px rgba(0, 0, 0, 0.4);
    -moz-box-shadow: 0 8px 6px -6px rgba(0, 0, 0, 0.4);
    box-shadow: 0 8px 6px -6px rgba(0, 0, 0, 0.4);
}

.navigation li li ul {
    border-width: 0;
    left: 100%;
}

.navigation li:hover > ul {
    top: 6.75em;
}

.navigation li li:hover > ul {
    top: 0;
}

.navigation li {
    display: block;
    float: left;
    position: relative;
}

.navigation li li {
    width: 100%;
}

.navigation li a {
    text-align: left;
    font-size: 13px;
    font-weight: 700;
    color: #4d4d4d;
    text-decoration: none;
    display: inline-block;
    text-transform: uppercase;
    border-bottom: 2px solid #fff;
    -webkit-transition: background-color .2s linear, padding-top .2s linear;
    -moz-transition: background-color .2s linear, padding-top .2s linear;
    -o-transition: background-color .2s linear, padding-top .2s linear;
    -ms-transition: background-color .2s linear, padding-top .2s linear;
    transition: background-color .2s linear, padding-top .2s linear;
    padding: 80px 9px 10px;
}


.navigation li:hover > a,.navigation .active-nav {
    background-color: #F36F25;
    color: #f7f7f7;
    -webkit-box-shadow: 0 3px 0 #ff8833;
    box-shadow: 0 3px 0 #ff8833;
}

.navigation li li a {
    color: #707070;
    font-size: 11px;
    width: 100%;
    padding: 8px 30px;
    border: none;
    -webkit-transition: none;
    -moz-transition: none;
    -o-transition: none;
    -ms-transition: none;
    transition: none;
}

.navigation li li:hover > a {
    box-shadow: none;
}

.navigation li li > a:not(:last-child):after {
    content: '';
    position: absolute;
    display: block;
    right: 1em;
    width: 0;
    height: 0;
    z-index: 99;
    top: .7em;
    border: .6em solid transparent;
    border-right-width: 0;
    border-left-color: #707070;
}

.navigation li li:hover > a:not(:last-child):after {
    border-left-color: #f7f7f7;
}
<header>
    <nav id="main-navigation" class="navigation cf">
        <ul>
            <li><a href="/new/homepage" class="active-nav">Homepage</a></li>
            <li><a href="/new/about">About Us</a>
                <ul>
                    <li><a href="/new/the-company">The Company</a></li>
                    <li><a href="/new/testimonials">Testimonials</a></li>
                    <li><a href="/new/careers">Careers</a></li>
                    <li><a href="/new/the-company#meet-the-team">Meet The Team</a></li>
                </ul>
            </li>
            <li>
                <a href="/new/services">Services</a><ul>
                    <li><a href="/new/broadband">Broadband</a></li>
                    <li><a href="/new/telecoms">Telecoms</a>
                        <ul>
                            <li><a href="/new/hosted-ip-pbx">Hosted IP PBX</a>
                            <li><a href="/new/voip-telephone-numbers">Telephone Numbers</a>
                            <li><a href="/new/traditional-lines">Traditional Lines</a>
                            <li><a href="/new/telephone-systems">Telephone Systems</a>
                        </ul>
                    </li>
                    <li><a href="/new/it-support">I.T. Support</a></li>
                    <li><a href="/new/offsite-backup">Offsite Backup</a></li>
                    <li><a href="/new/web-hosting">Web Hosting</a></li>
                    <li><a href="/new/hosted-exchange">Hosted Exchange</a></li>
                    <li><a href="/new/networking">Networking</a></li>
                    <li><a href="/new/security">Security</a>
                        <ul>
                            <li><a href="/new/cctv-systems">CCTV Systems</a>
                            <li><a href="/new/access-control">Access Control</a>
                        </ul>
                    </li>
                </ul>
            </li>
            <li><a href="/new/login">Login</a></li>
            <li><a href="/new/online-shop">Online Shop</a>
                <ul>
                    <li><a href="/new/online-shop/cart">Shopping Cart</a></li>
                </ul>
            </li><li><a href="/new/support">Support</a></li>
            <li><a href="/new/contact">Contact Us</a></li>
        </ul>
    </nav>
</header>
<main class="cf">
    <h1>Page title</h1>
    <p>
        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet. Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus. Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo eget bibendum sodales, augue velit cursus nunc,
    </p>
</main>
<footer>
    Page footer
</footer>

答案 3 :(得分:5)

只有通过CSS才有可能。你应该使用一些javascript代码。

$('.child').on('click', function(){
   $(this).closest('.parent').toggleClass('active');
});

答案 4 :(得分:1)

截至目前(2015年),仅使用CSS无法做到这一点。 但是,您可以使用Jquery执行此操作。 $("a.active").parents('li').css("property", "value");