I have this navbar currently
I am trying to make it so that whenever you click on one of the navbar elements this happens
导航栏html:
<div class="navbar-container">
<div class="navbar-wrapper">
<div class="text-radius home-radius">
<p><a href="#" class="nav-anchor">Home</a></p>
</div>
<div class="text-radius profile-radius">
<p><a href="#profile" class="nav-anchor">Profile</a></p>
</div>
<div class="text-radius contact-radius">
<p><a href="#contact" class="nav-anchor">Contact</a></p>
</div>
</div>
</div>
导航栏CSS:
.navbar-container {
display: flex;
justify-content: center;
}
.navbar-wrapper {
font-size: 13px;
margin: 20px 20px;
display: flex;
justify-content: center;
align-items: center;
gap: 5px;
border-radius: 5em;
background-color: var(--lightgrey);
width: 18.8em;
height: 3em;
}
.nav-anchor {
text-decoration: none;
color: var(--black);
}
.text-radius {
display: flex;
align-items: center;
justify-content: center;
width: 5.8em;
height: 2.5em;
border-radius: 5em;
}
.home-radius:active {
background-color: var(--white);
}
.profile-radius:active {
background-color: var(--white);
}
.contact-radius:active {
background-color: var(--white);
}
我尝试过的:
- 我试过使用:用途:active,:focus &:target,它们似乎不起作用。我现在很困惑。
我正在努力做到这一点没有JS的方式。
1条答案
按热度按时间cetgtptt1#
编辑:
如果您需要在单击其他位置时保持焦点,using a hidden check box可以完成这项工作。因为你只想关注一个,你可以把它改为半径框。
要使div可聚焦,您可以在其上添加
tabindex
,并使用focus
选择器。请注意,在你的结构中,点击<a>
不会聚焦在div上,要解决这个问题,你可以使用focus-within
选择器: