css 如何使用伪元素来更改哪个类获得样式?

093gszye  于 2023-10-21  发布在  其他
关注(0)|答案(1)|浏览(126)

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的方式。

cetgtptt

cetgtptt1#

编辑:

如果您需要在单击其他位置时保持焦点,using a hidden check box可以完成这项工作。因为你只想关注一个,你可以把它改为半径框。

.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: lightgrey;
    width: 18.8em;
    height: 3em;
    
}

.nav-anchor {
    text-decoration: none;
    color: black;
    cursor: pointer;
    
}

.text-radius {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 5.8em;
    height: 2.5em;
    border-radius: 5em;
}

.home-radius:focus-within{
    background-color: white;
}

.profile-radius:focus-within{
    background-color: white;
}

.contact-radius:focus-within{
    background-color: white;
}

.navbar-wrapper input[type=radio]{
    display: none;
}

.navbar-wrapper input[type=radio]:checked + .text-radius{
    background-color: white;
}
<div class="navbar-container">
    <div class="navbar-wrapper">
        <input type="radio" name="rGroup" value="1" id="r1" checked="checked" />
        <label class="text-radius contact-radius nav-anchor" for="r1" onclick='window.location.assign("#")'> Home </label>
        <input type="radio" name="rGroup" value="2" id="r2" />
        <label class="text-radius contact-radius nav-anchor" for="r2" onclick='window.location.assign("#Profile")'>Profile</label>
        <input type="radio" name="rGroup" value="3" id="r3" />
        <label class="text-radius contact-radius nav-anchor" for="r3" onclick='window.location.assign("#Contact")'>Contact</label>
    </div>
</div>

要使div可聚焦,您可以在其上添加tabindex,并使用focus选择器。请注意,在你的结构中,点击<a>不会聚焦在div上,要解决这个问题,你可以使用focus-within选择器:

.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: lightgrey;
    width: 18.8em;
    height: 3em;
    
}

.nav-anchor {
    text-decoration: none;
    color: black;
    
}

.text-radius {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 5.8em;
    height: 2.5em;
    border-radius: 5em;
}

.home-radius:focus-within{
    background-color: white;
}

.profile-radius:focus-within{
    background-color: white;
}

.contact-radius:focus-within{
    background-color: white;
}
<div class="navbar-container">
    <div class="navbar-wrapper">
        <div class="text-radius home-radius" tabindex="0">
            <p><a href="#" class="nav-anchor">Home</a></p>
        </div>
        <div class="text-radius profile-radius" tabindex="1">
            <p><a href="#profile" class="nav-anchor">Profile</a></p>
        </div>
        <div class="text-radius contact-radius" tabindex="2">
            <p><a href="#contact" class="nav-anchor">Contact</a></p>
        </div>
    </div>
</div>

相关问题