[英]How to make mobile menu responsive?
I used W3Schools to code a sidenav overlay menu https://www.w3schools.com/howto/howto_js_sidenav.asp with an accordion https://www.w3schools.com/howto/howto_js_accordion.asp . 我用W3Schools的编写一个sidenav覆盖菜单https://www.w3schools.com/howto/howto_js_sidenav.asp与手风琴https://www.w3schools.com/howto/howto_js_accordion.asp 。 I am trying to figure out how to make it responsive, but I am having no luck. 我想弄清楚如何让它响应,但我没有运气。
I have tried using #menu to fix it but the entire menu disappears. 我尝试使用#menu来修复它,但整个菜单都消失了。 I do not know if it is because I am not using ul or li for my links or not. 我不知道是不是因为我没有使用ul或li作为我的链接。
<header>
<div id="mySidenav" class="sidenav">
<a href="javascript:void(0)" class="closebtn" onclick="closeNav()">×</a>
<a href="index.html">Home</a>
<a class="accordion" href="#">Design ▿</a>
<div class="panel">
<a href="mobile.html">Mobile</a>
<a href="#">Web</a>
</div>
<a href="photography.html">Photography</a>
<a href="branding.html">Branding</a>
<a href="about.html">About | Contact</a>
</div>
<span class="opening" onclick="openNav()"><i class="fas fa-stream"></i></span>
</header>
.sidenav {
position: fixed;
width: 0;
height: 100%;
top: 0;
left: 0;
padding-top: 4rem;
z-index: 1;
transition: 0.5s;
overflow-x: hidden;
background-color: ghostwhite;
float: left;
}
.sidenav a {
display: block;
padding: 10px 10px 10px 32px;
transition: 0.3s;
font-size: 1rem;
font-family: mr-eaves-xl-modern,sans-serif;
font-weight: 300;
font-style: normal;
letter-spacing: 1.5px;
text-decoration: none;
text-align: left;
color: #504E4E;
}
.sidenav a:hover {
font-style: italic;
}
.opening {
position: absolute;
top: 1.5rem;
left: 5%;
font-size: 1.3rem;
color: #504E4E;
float: left;
}
.sidenav .closebtn {
position: absolute;
top: 0;
right: 5%;
margin-left: 20%;
float: left;
}
.accordion {
width: 100%;
padding: 18px;
border: none;
outline: none;
transition: 0.4s;
font-size: 1rem;
text-align: left;
color: #504E4E;
}
.active .accordion:hover{
font-style: italic;
}
.panel {
max-height: 0;
padding: 0 18px;
overflow: hidden;
transition: max-height 0.2s ease-out;
background-color: ghostwhite;
font-size: 1rem;
font-family: mr-eaves-xl-modern,sans-serif;
font-weight: 300;
font-style: normal;
letter-spacing: 1.5px;
text-decoration: none;
color: #504E4E;
}
I would like for it to be a horizontal menu when scaled to desktop. 我希望它在缩放到桌面时成为水平菜单。
Using ul
and li
shouldn't have an effect. 使用ul
和li
不应该有效果。
Use media queries to apply a new style that overwrites your current style when your screen is at a certain size. 使用媒体查询应用新样式,当您的屏幕处于特定大小时,该样式将覆盖当前样式。
To target all screens that have a width greater than or equal to 1024 pixels use @media ( min-width: 1024px )
. 要定位宽度大于或等于1024像素的所有屏幕,请使用@media ( min-width: 1024px )
。 To target screens with width less than or equal to 1024 pixels use @media ( max-width: 1024px )
. 要定位宽度小于或等于1024像素的屏幕,请使用@media ( max-width: 1024px )
。
One reason your menu isn't displaying is because you've set the width to 0 in the .sidenav
selector. 菜单未显示的一个原因是因为您在.sidenav
选择器中将宽度设置为0。 Remove that line. 删除该行。 To make the menu display full width on desktop you might add the following code to your CSS file: 要使菜单在桌面上显示全宽,您可以将以下代码添加到CSS文件中:
@media ( min-width: 1000px ) {
.sidenav {
width: 100%;
height: auto;
display: flex;
justify-content: flex-end;
}
.sidenav > a {
width: auto;
}
}
This will not display the accordion well; 这不会很好地展示手风琴; you may need to restructure your HTML. 您可能需要重新构建HTML。 However this should get you started with media queries and using different styles at different screen sizes. 但是,这应该让您开始使用媒体查询并使用不同屏幕尺寸的不同样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.