JavaScript
x
6
1
const toggleButton = document.getElementById("toggle-button");
2
const navList = document.getElementById("nav-list");
3
4
toggleButton.addEventListener('click', () => {
5
navList.classList.toggle('active');
6
})
JavaScript
1
76
76
1
* {
2
box-sizing: border-box;
3
padding: 0;
4
margin: 0;
5
}
6
7
body {
8
background-color: plum;
9
font-family: sans-serif;
10
}
11
12
header {
13
width: 100vw;
14
height: 100px;
15
}
16
17
.navbar {
18
background-color: darksalmon;
19
display: flex;
20
color: white;
21
justify-content: space-around;
22
height: 100px;
23
align-items: center;
24
}
25
26
.nav-list {
27
list-style: none;
28
}
29
30
.nav-list .list-item {
31
display: inline-block;
32
}
33
34
.menu {
35
display: none;
36
}
37
38
.imgLogo {
39
position: fixed;
40
left: 0;
41
top: 0;
42
height: 40px;
43
margin-top: 15px;
44
margin-left: 15px;
45
}
46
47
.active {
48
display: block;
49
}
50
51
52
@media only screen and (max-width: 490px) {
53
.navbar {
54
flex-direction: column;
55
}
56
.menu {
57
display: block;
58
position: absolute;
59
top: 20px;
60
right: 20px;
61
}
62
.nav-list {
63
width: 100%;
64
background-color: darksalmon;
65
padding-top: 240px;
66
display: none;
67
}
68
.nav-list .list-item {
69
display: block;
70
border-top: 1px solid white;
71
padding: 10px;
72
}
73
.nav-list .list-item:last-child {
74
border-bottom: 1px solid white;
75
}
76
}
JavaScript
1
17
17
1
<header>
2
<nav class="navbar">
3
<div class="menuLogo">
4
<img src="imgs/logo-desktop.svg" class="imgLogo">
5
</div>
6
<ul class="nav-list" id="nav-list">
7
<li class="list-item">Home</li>
8
<li class="list-item">Menu</li>
9
<li class="list-item">Rewards</li>
10
<li class="list-item">Gift Cards</li>
11
<li class="list-item">Stores</li>
12
</ul>
13
<div class="menu" id="toggle-button">
14
<img src="imgs/menu-burguer-open.svg">
15
</div>
16
</nav>
17
</header>
I’m building a responsive top navbar with a toggle button, and trying to change a CSS property from display:none
to display:block
, but I can’t get it to work.
The menu is receiving the class “show” with the display:block
property in it, from the toggle button (confirmed watching the code on dev tools of the browser), but the old display:none
is not being overriden by the new property.
I’m a newbie and I have no idea what I’m doing. Is there any obvious code-hierarchy I’m missing?
The “nav-list” UL is the one receiving the “active” property, but its display property won’t change.
Advertisement
Answer
Just use !important
in the .active
class:
JavaScript
1
4
1
.active {
2
display: block !important;
3
}
4
!important
overrides the previous styling rules for this element, it adds importance to the property.