是否有可能使HTML中的元素不遵循其父元素的CSS ID?

时间:2018-09-27 12:36:01

标签: javascript html css

例如,我希望导航中的每个元素都遵循定义的css规则,但最后一个元素是按钮。据我了解,我需要使id =“ primary-menu”不适用于最后一个li元素。

 <nav class="collapse navbar-collapse" id="primary-menu">
                <ul class="nav navbar-nav navbar-right">
                    <li class="active"><a href="#home-page">Home</a></li>
                    <li><a href="#feature-page">Features</a></li>
                    <li><a href="#team-page">Team</a></li>
                    <li><a href="#faq-page">FAQ</a></li>
                    <li><a href="#contact-page">Contact</a></li>
                    <li><a href="Test_Medium.mp4" class="button white">Watch video</a></li>
                </ul>
            </nav>

这是我要使用的按钮元素的CSS。         / **     *默认按钮     ** /

.button {
    display: inline-block;
    padding: 8px 30px;
    border-radius: 50px;
    color: #ffffff;
    background-color: #2f60c3;
    -webkit-transition: 0.3s;
    transition: 0.3s;
    border: none;
    -webkit-box-shadow: 2px 5px 20px -5px rgba(0, 0, 0, 0.1);
    box-shadow: 2px 5px 20px -5px rgba(0, 0, 0, 0.1);
}

.button.white,
.button:hover {
    background-color: #ffffff;
    color: #138fc2;
}

.button.white:hover {
    background-color: #2f60c3;
    color: #ffffff;
}

.button i {
    margin-left: 5px;
}

Here is the css being applied to the element curranty!
    .mainmenu-area {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    z-index: 9999999;
    -webkit-transition: 0.3s;
    transition: 0.3s;
}

.overlay,
.overlay a,
.mainmenu-area a {
    color: #ffffff;
}

.mainmenu-area .logo {
    text-transform: uppercase;
    letter-spacing: 2px;
    height: inherit;
    margin-top: 8px;
    padding-left: 0;
}

.mainmenu-area .logo h2 {
    margin-bottom: 0;
}

.mainmenu-area #primary-menu > ul > li > a:not(.button) {
    background: none;
    padding: 30px 15px;
    position: relative;
    border-top: 3px solid transparent;
    text-align: center;
}

.mainmenu-area #primary-menu > ul > li > a:not(.button):before {
    content: "";
    position: absolute;
    left: 50%;
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
    top: -10px;
    width: 0;
    height: 0;
    border-right: 10px solid transparent;
    border-left: 10px solid transparent;
    border-top: 10px solid #ffffff;
    opacity: 0;
    -webkit-transition: 0.3s;
    transition: 0.3s;
}

.mainmenu-area #primary-menu > ul > li > a:not(.button):hover:before,
.mainmenu-area #primary-menu > ul > li.active > a:not(.button):before {
    opacity: 1;
    top: 0;
}

.mainmenu-area #primary-menu ul li.active a:not(.button),
.mainmenu-area #primary-menu ul li a:not(.button):hover {
    border-top: 3px solid #ffffff;
}

.mainmenu-area.affix {
    background-color: #138fc2;
}

2 个答案:

答案 0 :(得分:5)

您可以使用CSS排除列表中的最后一个元素

ul li:not(:last-child) {
  background-color: red;
}
<nav class="collapse navbar-collapse" id="primary-menu">
  <ul class="nav navbar-nav navbar-right">
    <li class="active"><a href="#home-page">Home</a></li>
    <li><a href="#feature-page">Features</a></li>
    <li><a href="#team-page">Team</a></li>
    <li><a href="#faq-page">FAQ</a></li>
    <li><a href="#contact-page">Contact</a></li>
    <li><a href="Test_Medium.mp4" class="button white">Watch video</a></li>
  </ul>
</nav>

答案 1 :(得分:1)

当然,您可以这样做:

.nav li a:not(.button) { 
  color: red;
}
<nav class="collapse navbar-collapse" id="primary-menu">
      <ul class="nav navbar-nav navbar-right">
          <li class="active"><a href="#home-page">Home</a></li>
          <li><a href="#feature-page">Features</a></li>
          <li><a href="#team-page">Team</a></li>
          <li><a href="#faq-page">FAQ</a></li>
          <li><a href="#contact-page">Contact</a></li>
          <li><a href="Test_Medium.mp4" class="button white">Watch video</a></li>
      </ul>
  </nav>

例如,它会忽略使用类按钮的元素。