觀看金魚都能懂網頁設計入門 23
https://codepen.io/mikeyam/pen/ZEWPgGg?editors=1100 客製化選單
在開始製作有RWD的客製化選單前,我們要清楚知道我們要由手機開始製作到桌機,還是由桌機開始製作到手機?
<input type="checkbox" name="" id="menu_control">
<!-- label可以控制input的checkbox -->
<div class="Navbar">
<h1 class="logo">
<a href="#">
<img src="https://picsum.photos/60/40?random-1" alt="">
</a>
</h1>
<label for="menu_control" class="menu_btn">
<span>選單</span>
</label>
<nav>
<a href="#">link</a>
<a href="#">link</a>
<a href="#">link</a>
<a href="#">link</a>
<a href="#">link</a>
<a href="#">link</a>
</nav>
</div>
我們可以透過Css猜解來理解css設定原因
.Navbar{
height: 50px;
background-color: #000;
position: relative;
}
.logo{
width: 60px;
height: 40px;
margin: 0 10px;
}
.logo img{
display: block;
}
.logo a{
display: block;
height: 40px;
}
.menu_btn{
display: block;
width: 40px;
height: 40px;
background-color: blue;
position: absolute;
top: 5px;
right: 10px;
color: white;
}
/* 使選單文字消失
兩種方法:
1. 使用opacity:0;
2. 超出範圍隱藏起來
*/
.menu_btn span{
opacity: 0;
width: 1px;
height: 1px;
display: block;
overflow: hidden;
}
.menu_btn::before{
content: '';
position: absolute;
height: 2px;
left: 2px;
width: 36px;
background-color: whitesmoke;
top: 0;
bottom: 0;
margin: auto;
box-shadow: 0px 8px 0px whitesmoke,0px -8px 0px whitesmoke;
}
#menu_control:checked ~ .Navbar nav{
left: 0;
#menu_control{
position: absolute;
z-index: -2;
opacity: 0;
}
@media screen and (max-width: 767px){
nav{
height: calc(100vh - 50px);
width: 80%;
background-color: #000;
position: absolute;
top: 50px;
left: -100%;
transition: .5s;
}
}
@media screen and (min-width: 768px){
.menu_btn{
display: none;
}
.Navbar{
display: flex;
justify-content: space-between;
align-items: center;
}
nav{
display: flex;
position: relative;
left: 0;
width: auto;
height: auto;
top: 0;
background-color: transparent;
}
nav a{
border-bottom: none;
}
}