Answer the question
In order to leave comments, you need to log in
Why doesn't the media query work after running the js code?
There is a code after the launch of which the media query does not work.
document.querySelector('.search').onclick = function() {
var clientWidth = document.body.clientWidth;
var mnu = document.querySelector('.mnu');
var sandwichMnu = document.querySelector('.sandwich-mnu');
var searchBtn = document.querySelector('.search-button');
if(searchBtn.classList.contains('search-button-active')) {
searchBtn.classList.remove('search-button-active');
if(clientWidth > 1620) {
mnu.style.display = 'flex';
sandwichMnu.style.display = 'none';
}
} else {
if(clientWidth <= 1850) {
mnu.style.display = 'none';
sandwichMnu.style.display = 'flex';
}
searchBtn.classList.add('search-button-active');
}
}
@media only screen and (max-width: 1620px)
.head-navigation .mnu
display: none
.sandwich-mnu
display: flex
Answer the question
In order to leave comments, you need to log in
To avoid problems with specificity (and inline styles have more specificity), it is better to add and remove classes to elements using classList https://developer.mozilla.org/en/docs/Web/API/Elem...
And for classes in styles write the necessary css rules. Take it as a rule and get rid of such problems in the future.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question