我想渲染菜单,使其在多种平台(例如台式机浏览器,移动设备和平板电脑)上响应。如何使用CSS完成此操作?
这是我当前的CSS:
ul {
top:0px;
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
z-index:99;
cursor: pointer;
background-color:white;
text-align:center;
}
li {
top:0px;
width:20%;
font-family:odin rounded;
font-size:2.5vw;
float: left;
height:55px;
background-color:rgb(240,240,240);
border-right:1px solid black;
transition:color 1s, background-color 1s;
}
li:hover {
color:white;
background-color:black;
}
li a {
display: flex;
display: -webkit-flex;
color: #000;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
img.logo {
height:55px;
width:auto;
position:fixed;
top:0px;
z-index:99;
}
.text {
display:flex;
align-items:center;
text-align:center;
justify-content: center;
}
a.princip:hover {
background: transparent url('main.html');
cursor: pointer;
}
#barre {
overflow: hidden;
background-color: #333;
z-index:99;
}
#barre a {
float: left;
display: block;
color: #f2f2f2;
text-align: center;
padding: 14px 16px;
text-decoration: none;
font-size: 17px;
}
#barre a:hover {
background-color: #ddd;
color: black;
}
.sticky {
position: fixed;
top: 0;
width: 100%;
}
My HTML:
<div id="barre">
<ul >
<li class="text"><a href="main.html"><img class="logo" src="https://cdn1.imggmi.com/uploads/2018/7/18/97bc4958d2a5baf34ad21071a994bb0f-full.png" alt="problem"></a><pre style="font-family:odin rounded"> Team NoMaD</pre></li>
<li class="text" onclick="location.href = 'main.html';">Menu</li>
<li class="text" onclick="location.href = 'members.html';">Membres</li>
<li class="text" onclick="location.href = 'calender.html';">Calendrier</li>
<li class="text" onclick="location.href = 'contact.html';">Contact</li>
</ul>
</div>
<script>
window.onscroll = function() {myFunction()};
var sticky = barre.offsetTop;
function myFunction() {
if (window.pageYOffset >= sticky) {
barre.classList.add("sticky")
} else {
barre.classList.remove("sticky");
}
}
</script>
<div class="carte">
理想的结果(在移动设备上)是菜单下拉菜单显示为一行和一个正方形。我在全屏菜单中遇到的问题之一不是一行。
答案 0 :(得分:0)
将此添加到您的样式
@media only screen and (max-width: 600px){
li{
width: 100%;
}
}
使用此代码,您可以仅使用纯CSS来实现响应式设计,但我强烈建议Bootstrap4和SASS