html - CSS/HTML 导航栏 - 事件/当前链接

标签 html css

我正在尝试在我的导航栏上创建一个事件/当前链接。

在 HTML 中,有一个元素在其中一个元素上具有 class="current" 属性。

如果类是事件的/当前的,我希望该元素有背景

<ul id="trans-nav">
    <li  class="current">
        <a href="#">About Us</a>
        <ul>
            <li><a href="/contact">Contact Us</a></li>
            <li><a href="/login">Login</a></li>
        </ul>
    </li>
</ul>

我的 CSS 需要什么?

#trans-nav {
    list-style-type: none; 
    height: 40px; 
    padding: 0; 
    margin: 0;
    position:relative;
    z-index:999;
}
#trans-nav {
    list-style-type: none; 
    height: 40px; 
    padding: 0; 
    margin: 0;
}
#trans-nav li {
    float: left; 
    position: relative; 
    padding: 0; 
    line-height: 40px;
}
#trans-nav li:hover {
    background-position: 0 -40px;
}
#trans-nav li a {
    display: block; 
    padding: 0 15px; 
    color: #666666; 
    text-decoration: none;
}
#trans-nav li a:hover {
    background-color:#F36F25; 
    color: #eeeeee;
}
#trans-nav li active {
    background-color:#F36F25; 
    color: #eeeeee;
}
#trans-nav li ul {
    opacity: 0;
    position: absolute;
    left: 0;
    width: 200px;
    background: #EEEEEE;
    list-style-type: none;
    padding: 0;
    margin: 0; 
}
#trans-nav li:hover ul {
    opacity: 1; 
}
#trans-nav li ul li {
    float: none; 
    position: static; 
    height: 0; 
    line-height: 0; 
    background: none;
}
#trans-nav li:hover ul li {
    height: 30px;
    line-height: 30px;
}
#trans-nav li ul li a {
    background: #EEEEEE;
}
#trans-nav li ul li a:hover {
    background: #666666;
    color:#EEEEEE;
}

#trans-nav li { -webkit-transition: all 0.2s; }
#trans-nav li a { -webkit-transition: all 0.5s; }
#trans-nav li ul { -webkit-transition: all 1s; }
#trans-nav li ul li { -webkit-transition: height 0.5s; }

我已经试过了,但是没用:

#trans-nav li active {
    background-color:#F36F25; 
    color: #eeeeee;
}

最佳答案

你离得太近了!

#trans-nav li .active {
background-color:#F36F25; 
color: #eeeeee;
}

你只是忘记了 . 上的 .active

Working Fiddle

关于html - CSS/HTML 导航栏 - 事件/当前链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15751464/

相关文章:

html - Grails如何支持HTML5离线模式?

html - 为什么子菜单在 firefox 中显示正确但在 chrome 中显示不正确

html - 应该使用什么语义 HTML 标记来创建面包屑?

javascript - DOM 渲染后在 React 中执行 Vanilla JS 脚本

html - CSS:Chrome 和 Safari 忽略表格单元格高度

javascript - 快速悬停时 jQuery animate() 序列乱序

html - 溢出和列表样式类型不起作用

css - Bootstrap 4下拉菜单在移动设备中具有全宽

css - 我的下拉 div 的定位

css - 为什么我的菜单项在小屏幕上重叠?