HTML Navbar checkbox not clickable

Viewed 185

I have a problem... I created the following navbar:

.menu {
    position: fixed;
    height: 100vh;
    width: 150px;
    background-color: #3D5021;
    z-index: 0;
    left: 0px;
}

nav {
    margin-top: 50px;
}

nav ul {
    list-style: none;
    text-align: center;
}

nav ul li {
    display: block;
    margin: 5px 0;
    width: 60px;
    height: 60px;
}

input[type="checkbox"] {
    display: none;
}

.menuButtonIcon {
    color: #FFFFFF60;
}

.menuButtonIcon .fas {
    font-size: 25px;
}

nav ul li:hover i {
    color: #FFFFFF;
    cursor: pointer;
}

#btnDashboard:checked~.fa-tachometer-alt {
    color: #FF8A00FF;
}

#btnList:checked~.fa-list-ul {
    color: #FF8A00FF;
}

#btnHeatMap:checked~.fa-map {
    color: #FF8A00FF;
}






.container {
    position: absolute;
    width: calc(100vw - 100px);
    right: 0;
    border-top-left-radius: 50px;
    border-bottom-left-radius: 50px;
    background-color: #E1E1E1;
    min-height: 100vh;
}


.normal-content {
    margin-left: 50px;
    margin-top: 20px;
}

.full-content {
    margin: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div class="menu">
    <nav>
        <ul class="nav-links">
            <div class="selector"></div>
            <li>
                <label for="btnDashboard" class="menuButtonIcon" routerLink="/researcher/dashboard">
                    <i class="fas fa-tachometer-alt"></i>
                </label>
                <input type="checkbox" id="btnDashboard" checked>
            </li>

            <li>
                <label for="btnList" class="menuButtonIcon" routerLink="/researcher/list">
                    <i class="fas fa-list-ul"></i>
                </label>
                <input type="checkbox" id="btnList">
            </li>

            <li>
                {{checked}}
                <label for="btnHeatMap" class="menuButtonIcon" [(ngModel)]="checked">
                    <i class="fas fa-map"></i>
                </label>
                <input type="checkbox" id="btnHeatMap">
            </li>
        </ul>
    </nav>
</div>

<div class="container">
    <div id="content" class="normal-content">
        <router-outlet></router-outlet>
    </div>
</div>

Now when I run this code, the RouterLink mappings are working perfectly, because the content gets switched to the right page, but the colors of the icons in the navbar are not changing. I printed the checked property of the heatmap button, but that value stays always false. First I thought that the entire navbar gets reloaded when the Routerlink triggers, but when I turn that off, I still don't see any color changes... How can I change the color of the selected icon in the navbar???

1 Answers

First of all, if you want only 1 item selected at a time, then use <input type="radio" /> instead of <input type="checkbox" />.

You are using the general sibling combinator ~ :

The general sibling combinator (~) separates two selectors and matches all iterations of the second element, that are following the first element (though not necessarily immediately), and are children of the same parent element.

If you want to change the color of the label associated with a radio, you need to place the radio before its label.

It can be done with a single rule :

.menu input[type="radio"]:checked ~ .menuButtonIcon {
    color: #FF8A00FF;
}

When an <input type="radio" /> of the menu is checked, the following .menuButtonIcon element will have the color #FF8A00FF.

.menu {
    position: fixed;
    height: 100vh;
    width: 150px;
    background-color: #3D5021;
    z-index: 0;
    left: 0px;
}

nav {
    margin-top: 50px;
}

nav ul {
    list-style: none;
    text-align: center;
}

nav ul li {
    display: block;
    margin: 5px 0;
    width: 60px;
    height: 60px;
}

input[type="radio"] {
    display: none;
}

/* see above changed CSS
input[type="checkbox"] {
    display: none;
} */

.menuButtonIcon {
    color: #FFFFFF60;
}

.menuButtonIcon .fas {
    font-size: 25px;
}

nav ul li:hover i {
    color: #FFFFFF;
    cursor: pointer;
}

.menu input[type="radio"]:checked ~ .menuButtonIcon {
    color: #FF8A00FF;
}

/* see above changed CSS
#btnDashboard:checked~.fa-tachometer-alt {
    color: #FF8A00FF;
}

#btnList:checked~.fa-list-ul {
    color: #FF8A00FF;
}

#btnHeatMap:checked~.fa-map {
    color: #FF8A00FF;
}
*/

.container {
    position: absolute;
    width: calc(100% - 140px);
    right: 0;
    border-top-left-radius: 10px;
    border-bottom-left-radius: 10px;
    background-color: #E1E1E1;
    min-height: 100vh;
}


.normal-content {
    margin-left: 50px;
    margin-top: 20px;
}

.full-content {
    margin: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.2.0/css/all.min.css" integrity="sha512-6c4nX2tn5KbzeBJo9Ywpa0Gkt+mzCzJBrE1RB6fmpcsoN+b/w/euwIMuQKNyUoU/nToKN3a8SgNOtPrbW12fug==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<div class="menu">
    <nav>
        <ul class="nav-links">
            <div class="selector"></div>
            <li>
                <input type="radio" id="btnDashboard" name="menuIcon" checked>
                <label for="btnDashboard" class="menuButtonIcon" routerLink="/researcher/dashboard">
                    <i class="fas fa-tachometer-alt"></i>
                </label>
            </li>

            <li>
                <input type="radio" name="menuIcon" id="btnList">
                <label for="btnList" class="menuButtonIcon" routerLink="/researcher/list">
                    <i class="fas fa-list-ul"></i>
                </label>
            </li>

            <li>
                <input type="radio" name="menuIcon" id="btnHeatMap">
                <label for="btnHeatMap" class="menuButtonIcon" [(ngModel)]="checked">
                    <i class="fas fa-map"></i>
                </label>
            </li>
        </ul>
    </nav>
</div>

<div class="container">
    <div id="content" class="normal-content">
        <router-outlet></router-outlet>
    </div>
</div>

Related