0
我正在構建一個只有css而沒有jQuery的下拉菜單的響應菜單。 新增flexbox並努力使下拉列表正確對齊。下面的代碼工作除了下拉的東西,將會失調 - 嘗試與像position:relative這樣的標準東西。使用css flexbox和沒有jQuery的響應下拉導航
我正在使用複選框(.toggle
)顯示/隱藏漢堡包。 希望得到一些指點。
這裏是我的代碼(也JSFiddle here)
nav {
margin:0 auto;
max-width:700px;
width:100%;
}
.menu {background:#28303a;padding:0;margin:0;max-width:700px;height:50px;
display: flex;
flex-direction: row;
justify-content: space-around;
align-items: center;
list-style-type: none;
}
.menu li a {padding:10px;border-radius:5px;align-self:center;}
.menu li a:hover { background: #0093bb;}
#checkbox1,.toggle { display: none;}
/* handle smaller screens here */
@media screen and (max-width: 550px) {
.menu {
flex-direction: column;
height: 0px;
}
.menu li {
display: flex;
align-self: center;
width:100%;
/* hide until user clicks on hamburger */
opacity: 0;
visibility: hidden;
}
.menu li a {
width: 100%;
text-align: center;
align-self: center;
align-content: center;
}
.toggle {
color:#fff;
clear: both;
display: block;
text-align: center;
line-height: 40px;
cursor: pointer;
width: 100%;
height: 40px;
}
.toggle:hover {
background: #0093bb;
}
#checkbox1:checked + label .menu li {
/* show menu - user clicked on hamburger */
opacity: 1;
visibility: visible;
}
#checkbox1:checked + label .menu {
height: 200px;
}
}
/* standard */
body {font-family: 'Open Sans';font-size:15px}
a{text-decoration:none;color:#fff}
.container {background:#28303a;}
<div class="container">
<nav>
<input type="checkbox" id="checkbox1" />
<label for="checkbox1">
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="#">Sub items 1</a>
<!-- sub items below/this won't work:
<ul class="menu">
<li><a href="#">Sub 1a</a></li>
<li><a href="#">Sub 1b</a></li>
</ul>
-->
<li><a href="#">Sub items 2</a></li>
<li><a href="#">Contact</a></li>
</ul>
<span class="toggle">☰</span>
</label>
</nav>
</div>
UPDATE:這是我終於工作的代碼。測試邊緣,Chrome(PC /手機),Firefox,IE 10-11(IE10以下 - >祝你好運:)。 除了更新的CSS之外,我還將整個<ul class="menu">
內容移動到<label for...>
之外,因爲舊代碼不符合W3C標準。
/* standard */
body {font-family: 'Open Sans';font-size:15px}
a {text-decoration:none;color:#fff}
/* code for responsive css dropdown starts here: */
.container {
background: #28303a;
padding-bottom: 10px;
display: flex;
justify-content: center;
}
nav {
box-sizing: border-box;
background: #28303a;
margin: 0;
padding: 0;
}
nav label {
padding: 0;
margin: 0
}
nav ul {
padding: 0;
margin: 0;
display: flex;
flex-direction: row;
}
nav li {
list-style-type: none;
padding:0 10px;
margin:0;
font-size:16px;
line-height:16px;
}
nav a, #checkbox1, .toggle {
font-size: 16px;
color: #fff;
width: 100%;
display: block;
line-height: 16px;
}
/* hide sub menu initially */
nav > ul > li ul {
display: none;
}
/* show sub menu on parent hover */
nav > ul > li:hover ul {
display: block;
background: #28303a
}
/* position sub menu */
ul ul {
position: absolute;
}
ul ul > li {
padding: 5px 8px;
}
/* toggle button hidden initially */
#checkbox1, .toggle {
display:none;
}
@media screen and (max-width:768px) {
nav {
width: 100%
}
nav ul {
flex-direction: column;
}
nav > ul > li {
justify-content: center;
text-align: center;
align-items: center;
align-content: center;
width: 100%;
padding:0;
height:0; /* set height to 0 so the menu won't take up space, until toggle=open*/
opacity:0;
visibility: hidden;
}
/* position sub menu */
ul ul {
position: relative
}
/* remove padding from sub menu items */
ul ul > li {
padding:0;
}
/* set padding for all a elements */
nav a {
padding: 3px 0;
}
nav a:hover {
background: #0093bb;
}
.toggle {
color: #fff;
clear: both;
display: block;
text-align: center;
cursor: pointer;
width: 100%;
padding: 0;
margin: 0;
}
.toggle:hover {
background: #0093bb;
}
/* show menu - user clicked on hamburger */
#checkbox1:checked + label + ul.menu > li {
opacity: 1;
visibility: visible;
height: auto;
}
}
<div class="container">
<nav>
<input type="checkbox" id="checkbox1" />
<label for="checkbox1"><span class="toggle">≡</span></label>
<ul class="menu">
<li><a href="#">Home</a></li>
<li><a href="#">Sub items 1</a>
<ul>
<li><a href="#">Sub 1a</a></li>
<li><a href="#">Sub 1b</a></li>
</ul>
<li><a href="#">Sub items 2</a>
<ul>
<li><a href="#">Sub 2a</a></li>
<li><a href="#">Sub 2b</a></li>
<li><a href="#">Sub 3c</a></li>
</ul>
</li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
</div>
感謝發佈。然而,正如你寫的那樣,它沒有反應(我需要)。在無響應莊園中創建子菜單我已經計算好了,但是在任何情況下都要感謝您的輸入:) – Sha