【问题标题】:CSS selector: applying :focus on navbarCSS 选择器:应用 :focus 导航栏
【发布时间】:2023-04-03 22:36:02
【问题描述】:

遵循 ADA 标准,当用户的鼠标悬停在导航栏上(使用 :hover)或用户的鼠标聚焦在导航栏中的某个项目上时,我需要使导航栏展开。

虽然我能够理解第一部分,但我无法实现第二个要求。

这是一个代码 sn-p 来演示我当前的实现:

https://codepen.io/neotriz/pen/MoBMvz?editors=1100

CSS:

.nav-template {
    height: 100%;
    position: absolute;
    transition: width 0.3s linear;
    display: flex;
    overflow: hidden;
    background: black;
    width: 70px;
    &:hover{
        width: 150px;
    }

    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;

        li {
            margin: 0;
            padding: 0;
            cursor: pointer;
            &:hover {
                background-color: rgba(255, 255, 255, 0.12);
            }
            a {
                color: white;
                height: 40px;
                margin-left: 12px;
                margin-right: 35px;
                padding-bottom: 10px;
                display: flex;
                align-items: center;
                text-decoration: none;
            }
        }
    }
}

如果我在 &:hover 所在的位置应用 &:focus ,它似乎不起作用,因为它认为需要选择整个导航栏才能扩展它,假设专注于每个单独的项目。

我当前的实现是否需要修改?

【问题讨论】:

  • :focus css 仅适用于输入(例如按钮/文本框),不幸的是不能在 div 上工作
  • 我明白了。关于如何在不使用:focus 的情况下满足第二个要求的任何建议?
  • 如果你在你的 .nav-template 类中添加一个 :active 状态,将其设置为与你的 :hover 状态相同的宽度呢?
  • @HenryVisotski,如果可能的话,你能提供一个示例代码吗?
  • 您可以使用tabindex 属性使任何元素成为焦点。例如 <div tabindex="0"></div> 将使 div 能够采用 :focus psuedoclass

标签: html css css-selectors less


【解决方案1】:

试试这样的东西

HTML

<div tabindex="1" class="nav-template">
  <ul>
    <li><a href="#">Home</a></li>
    <li><a href="#">About</a></li>
    <li><a href="#">Contact</a></li>
  </ul>
</div>

.nav-template {
    height: 100%;
    position: absolute;
    display: flex;
    overflow: hidden;
    background: black;
    ul {
        list-style-type: none;
        margin: 0;
        padding: 0;
    transition: width 0.3s linear;
        li {
            margin: 0;
            padding: 0;
            cursor: pointer;
            a {
                color: white;
                height: 40px;
                margin-left: 12px;
                margin-right: 35px;
                padding-bottom: 10px;
                display: flex;
                align-items: center;
                text-decoration: none;
                width:70px;
                transition:all ease 0.5s;
        &:focus,&:hover {
                width: 100px;
        background-color: rgba(255, 255, 255, 0.12);
            }
            }
        }
    }
}

CSS

.nav-template {
  height: 100%;
  position: absolute;
  display: flex;
  overflow: hidden;
  background: black;
}
.nav-template ul {
  list-style-type: none;
  margin: 0;
  padding: 0;
  transition: width 0.3s linear;
}
.nav-template ul li {
  margin: 0;
  padding: 0;
  cursor: pointer;
}
.nav-template ul li a {
  color: white;
  height: 40px;
  margin-left: 12px;
  margin-right: 35px;
  padding-bottom: 10px;
  display: flex;
  align-items: center;
  text-decoration: none;
  width:70px;
  transition:all ease 0.5s;
}
.nav-template ul li a:focus,
.nav-template ul li a:hover {
  width: 100px;
  background-color: rgba(255, 255, 255, 0.12);
}

在锚标签上使用 :focus 会增加它的宽度, :focus 适用于 div 但这里的问题是当你聚焦时它直接聚焦在锚标签上而不是 div 或 li 或 ul 上

为过渡添加这些规则

.nav-template ul li a{
width:70px;
transition:all ease 0.5s;
}

Link For reference

试试看

希望这会有所帮助...

【讨论】:

  • 感谢您的回复!这似乎是一个好的开始。我在为这个示例放置 transition 属性时遇到了麻烦
  • @Alejandro 刚刚更新了代码,小提琴也试试看。希望这会有所帮助...
猜你喜欢
  • 2020-07-04
  • 1970-01-01
  • 2019-02-28
  • 1970-01-01
  • 2020-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
相关资源
最近更新 更多