gpt4 book ai didi

html - 小媒体上的移动菜单回到前面

转载 作者:行者123 更新时间:2023-11-28 04:45:11 25 4
gpt4 key购买 nike

一个简单的问题。

我的列表 (li) 向右浮动,但是当媒体尺寸适用于小屏幕时,它会显示在前面。我确信这是一个简单的修复,但我没有成功。任何帮助都会很棒,提前谢谢你。

enter image description here

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>title</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<meta name="robots" content="index,follow" />
<link rel="stylesheet" type="text/css" href="nav bar 1 w3s.css" />
</head>

<body>

<ul class="topnav" id="myTopnav">
<li><a href="#Contact Us">Contact Us</a></li>
<li><a href="#Extra">Extra</a></li>
<li><a href="#Writings">Writings</a></li>
<li><a href="#About">About</a></li>
<li><a href="#Home">Home</a></li>
<li class="icon">
<a href="javascript:void(0);" onclick="myFunction()">&#9776;</a></li>
</ul>

<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>

</body>
</html>


body{
margin:0;
padding:0;
}

*{
margin:0;
padding:0;
}

/*remove margins and padding from the LIST, and add a black background color*/
ul.topnav{
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
background-color:#333;
}

/*float the LIST (li) items side by side*/
ul.topnav li{
float:right;
}

/*style the (a)links inside the LI items*/
ul.topnav li a{
display:inline-block;
color:#f2f2f2;
text-align:center;
padding:14px 16px;
text-decoration:none;
transition: 0.3s;
font-size:17px;
}


/*change backround color of the LINKS (a) on hover*/
ul.topnav li a:hover{
background-color:#555;
}


/*hide the list (li) items that contains the link that
should open and close-quotethe topnav on small screens*/
ul.topnav li.icon{
display:none;
}


/*when the screen is less than 680px hide,hide all list (li)items,
except the first one("Home"). Show the list(li) item that contains
the link (a) open and close the topnav (li.icon)*/


@media screen and (max-width:680px){

ul.topnav li:not(:first-child){
display:none;
}

ul.topnav li.icon{
float:right;
display:inline-block;
}

/*float the LIST (li) items side by side*/
ul.topnav li{
float:left;
}
}

/*the "responsive" class is added to the topnav with Javascript when the uses
clicks on the (icon). This class makes the topnav look good on small screens*/
@media screen and (max-width:680px){
ul.topnav.responsive {
position:relative;
}

ul.topnav.responsive li.icon{
position:absolute;
right:0;
top:0;
}

ul.topnav.responsive li {
float:none;
display:inline;
}

ul.topnav.responsive li a{
display:block;
text-align:left;
}

}

最佳答案

好的,所以在您的情况下,我会建议最好的推荐解决方案 - Using Flexbox

Codepen here

由于您粘贴的代码非常简陋,直接来自您的实验书,我可以说,我花了一些时间来识别不必要的代码。我将逐步说明如何从当前代码中实现您想要的。

1) 首先以正确的方式排列 HTML( <Home> ..to.. <Contact Us> )

2) 制作你的 <ul>使用

将其子项( <li>) float 到右侧
ul.topnav{
list-style-type:none;
margin:0;
padding:0;
overflow:hidden;
background-color:#333;
display: flex;
justify-content: flex-end; /*float its children to right*/
}

3) 现在在移动设备上将这些子级 float 到左侧并使用列(垂直)排列菜单

@media screen and (max-width:680px){
ul.topnav {
justify-content: flex-start; /*float its children to left*/
flex-direction: column;
}

...other styles...
}

4) 现在使汉堡包图标 ( .icon ) 绝对位置始终在移动模式下,而不仅仅是在菜单展开时。

为此删除 ul.topnav.responsive li.icon款式来自 @media screen and (max-width:680px .将那些与 ul.topnav li.icon 合并

新的合并代码看起来像

  ul.topnav li.icon{
display:inline-block;
position: absolute;
right: 0;
top: 0;
}

除此之外,其余代码应保持不变,当然除了缩进和冗余媒体查询调用等。

关于html - 小媒体上的移动菜单回到前面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41001061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com