gpt4 book ai didi

html - 菜单列表没有按预期出现在 div 中

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

我想用这个侧边栏创建一个导航菜单。

它应该有链接但是 ul 列表没有显示在 div 元素上

我只想使用 CSS 来做到这一点。

需要纯 CSS,因为我没有任何脚本语言知识

<!DOCTYPE html>
<html lang="en">
<head>
<title>SideBar</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons
"/>
<style type="text/css">
/*
#button {
display: block;
text-decoration: none;
margin-top: 20px;
margin-left: 50px;
color: white;
width: 20px;
height:20px;
font-size: 30px;
transition: width 0.5s;
background-color: black;
text-align: center;
padding-bottom: 30px;
box-sizing: border-box;
}*/

#button {
margin-top: 20px;
margin-left: 50px;
font-size: 30px;
cursor: hand;
}

#sidebar {
position: absolute;
background-color: black;
left: 0px;
top: 0px;
width: 0px;
height: 100%;
transition: width 0.5s;
opacity: 0.7;
}

#screen {
position: fixed;
background-color: tan;
height: 100%;
width: 100%;
left: 0px;
top: 0px;
}

#button:hover + #sidebar {
width: 200px;
}

#sidebar:hover {
width: 200px;
}

</style>
</head>

<body>

<div id="screen">

<i class="material-icons" id="button">menu</i>

<div id="sidebar">
<ul>
<li><a href="#1">Nav 1</a></li>
<li><a href="#2">Nav 2</a></li>
<li><a href="#3">Nav 3</a></li>
</ul>
</div>

<hr />
<div>
Hello
</div>
</div>

</body>
</html>

最佳答案

因为在您的 CSS 中您有 #sidebar{ width: 0px;} ul 不能包含在 div 中,而是使用left 值而不是 width JSFiddle

#button {
margin-top: 20px;
margin-left: 50px;
font-size: 30px;
cursor: hand;
}
#sidebar {
position: absolute;
background-color: black;
left: -200px;
top: 0px;
width: 200px;
height: 100%;
transition: width 0.5s;
opacity: 0.7;
padding-top:50px;
-webkit-transition: left 1s;
transition: left 1s;
}
#sidebar ul {
list-style-type:none;
}
#screen {
position: fixed;
background-color: tan;
height: 100%;
width: 100%;
left: 0px;
top: 0px;
}
#sidebar:hover, #button:hover + #sidebar {
left: 0;
-webkit-transition: left 1s;
transition: left 1s;
}
<div id="screen"><i class="material-icons" id="button">menu</i>

<div id="sidebar">
<ul>
<li><a href="#1">Nav 1</a></li>
<li><a href="#2">Nav 2</a></li>
<li><a href="#3">Nav 3</a></li>
</ul>
</div>
<hr />
<div>Hello</div>
</div>

注意:添加了transition: left 1s; 以配合-webkit-支持问题实现滑入滑出的过渡

关于html - 菜单列表没有按预期出现在 div 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33374403/

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