我正在调整自适应移动菜单,使其在各种移动设备屏幕尺寸上显示为全屏布局,因此我使用百分比表示链接宽度(85%)而非固定因为我在大屏幕上的价值。
我将li
设置为width:100%
,将a
标记设置为width:85%
。在从属列表项目中,我在左侧添加了14px填充,以使它们在视觉上突出。问题是,使用左侧的填充,列表项随着菜单的每个级别变宽。
我发现解决这个问题的唯一两种方法都会引发新的问题。例如,我尝试将li
和a
标记同时设置为100%宽度,但这会导致底部的1px边框丢失任一边的边距,从而使边框全宽。
其次,我尝试将列表项设置为85%宽度并将a
设置为100%,但这会切断子菜单按钮上的某些填充,这意味着您无法单击整个按钮
我在这里错过了一些简单的东西吗?任何见解都会很棒。
https://jsfiddle.net/8nj5y4t1/48/
HTML:
<nav class="main-menu" id="mobile">
<div class="menu-header-menu-container">
<ul id="menu-header-menu-1" class="menu">
<li class="hide-desktop menu-item menu-item-type-post_type menu-item-object-page menu-item-1386"><a href="http://www.mywebsite.com">Link 1</a></li>
<li class="open menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-463"><a href="http://www.mywebsite.comwork/">Link 2</a>
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-584"><a href="http://www.mywebsite.comwork/landscapes/">Child-link 1</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-473"><a href="http://www.mywebsite.comwork/seascapes/">Child-link 2</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-478"><a href="http://www.mywebsite.comwork/macro/">Child-link 3</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-477"><a href="http://www.mywebsite.comwork/cities/">Child-link 4</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-475"><a href="http://www.mywebsite.comwork/long-exposure/">Child-link 5</a>
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-480"><a href="http://www.mywebsite.comwork/miscellaneous/">Grandchild-link 1</a></li>
</ul>
</li>
</ul>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-2 current_page_item menu-item-has-children menu-item-10"><a href="http://www.lucieaverillphotography.co.uk/about/">Link 3</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-464"><a href="http://www.lucieaverillphotography.co.uk/shop/">Link 4</a></li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-14"><a href="http://www.lucieaverillphotography.co.uk/contact/">Link 5</a></li>
</ul>
</div>
</nav>
CSS:
nav.main - menu# mobile {
width: 400 px;
}
nav.main - menu# mobile ul {
position: relative;
overflow: auto;
margin: 0;
padding: 0;
}
nav.main - menu# mobile ul li {
position: relative;
display: inline - block;
float: left;
width: 100 % ;
margin: 0 px;
background - color: grey;
}
nav.main - menu# mobile ul li a {
display: inline - block;
height: auto;
width: 85 % ;
margin: 0 px 7.5 % 0 px 7.5 % ;
padding: 15 px 0 px 15 px 0 px;
background - color: pink;
color: #888888;
border-bottom: 1px solid # F1F1F1;
text - decoration: none!important;
}
.submenu - button {
position: absolute;
top: 0;
right: 8 px;
float: right;
width: 15 px;
height: 19 px;
padding: 17 px;
background - color: purple;
cursor: pointer;
}
nav.main - menu# mobile ul ul {
max - height: 0;
transition: all 1.2 s;
}
nav.main - menu# mobile ul ul.open {
max - height: 1000 px;
}
nav.main - menu# mobile ul ul li {
margin: 0;
transition: all 1 s;
opacity: 0;
transition: opacity 1.5 s!important;
}
nav.main - menu# mobile ul ul li.open {
opacity: 1;
transition: opacity 1 s!important;
}
nav.main - menu# mobile ul ul li a {
padding: 15 px 0 15 px 14 px;
}
nav.main - menu# mobile ul ul ul li a {
padding: 15 px 0 15 px 28 px;
}
答案 0 :(得分:0)
我检查了你的菜单有一些问题。我决定创建纯CSS菜单的最佳方式,如您的参考网站。它简单而优雅。
请查看演示
CSS - 响应式菜单
@import url(http://fonts.googleapis.com/css?family=roboto);
body {
background: #212121;
font-size: 22px;
line-height: 32px;
color: #ffffff;
margin: 0;
padding: 0;
word-wrap: break-word !important;
font-family: 'roboto', sans-serif;
}
h1 {
font-size: 60px;
text-align: center;
color: #FFF;
}
h3 {
font-size: 30px;
line-height: 34px;
text-align: center;
color: #FFF;
}
h3 a { color: #FFF; }
a { color: #FFF; }
h1 {
margin-top: 100px;
text-align: center;
font-size: 60px;
line-height: 70px;
font-family: 'roboto', sans-serif;
}
#container {
margin: 0 auto;
max-width: 890px;
}
p { text-align: center; }
.toggle, [id^=drop] {
display: none;
}
nav {
margin: 0;
padding: 0;
background-color: #254441;
}
#logo {
display: block;
padding: 0 30px;
float: left;
font-size: 20px;
line-height: 60px;
}
nav:after {
content: "";
display: table;
clear: both;
}
nav ul {
float: right;
padding: 0;
margin: 0;
list-style: none;
position: relative;
}
nav ul li {
position: relative;
margin: 0px;
display: inline-block;
float: left;
background-color: #254441;
}
nav a {
display: block;
padding: 0 20px;
color: #FFF;
font-size: 20px;
line-height: 60px;
text-decoration: none;
-webkit-transition: all .3s ease-out;
-moz-transition: all .3s ease-out;
-ms-transition: all .3s ease-out;
-o-transition: all .3s ease-out;
transition: all .3s ease-out;
}
nav ul li ul li:hover { background: #000000; }
nav a:hover { background-color: #000000; }
nav ul ul {
display: none;
position: absolute;
top: 60px;
}
nav ul li:hover > ul { display: inherit; }
nav ul ul li {
width: 170px;
float: none;
display: list-item;
position: relative;
}
nav ul ul ul li {
position: relative;
top: -60px;
left: 170px;
}
li > a:after { content: ' +'; }
li > a:only-child:after { content: ''; }
/* Media Queries
--------------------------------------------- */
@media all and (max-width : 959px) {
#logo { display: none; }
nav { margin: 0; }
.toggle + a,
.menu { display: none; }
.toggle {
display: block;
background-color: #254441;
padding: 0 20px;
color: #FFF;
font-size: 20px;
line-height: 60px;
text-decoration: none;
border: none;
}
.toggle:hover { background-color: #000000; }
[id^=drop]:checked + ul { display: block; }
nav ul li {
display: block;
width: 100%;
border-bottom: 1px solid #1f1f1f;
}
nav ul ul .toggle,
nav ul ul a { padding: 0 40px; }
nav ul ul ul a { padding: 0 80px; }
nav a:hover,
nav ul ul ul a { background-color: #000000; }
nav ul li ul li .toggle,
nav ul ul a { background-color: #212121; }
nav ul ul {
float: none;
position: static;
color: #ffffff;
}
nav ul ul li:hover > ul,
nav ul li:hover > ul { display: none; }
nav ul ul li {
display: block;
width: 100%;
border-bottom: 1px solid #2b2929;
}
nav ul ul ul li { position: static; }
.toggle span {
position: absolute;
right: 0;
background-color: #888888;
padding: 0 20px;
}
}
@media all and (max-width : 330px) {
nav ul li {
display: block;
width: 100%;
}
}
&#13;
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<nav>
<div id="logo">Responsive Menu</div>
<label for="drop" class="toggle">Menu <span><i class="fa fa-bars fa-1"></i></span></label>
<input type="checkbox" id="drop" />
<ul class="menu">
<li><a href="#">Home</a></li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-1" class="toggle">Service <span>+</span></label>
<a href="#">Service</a>
<input type="checkbox" id="drop-1"/>
<ul>
<li><a href="#">Service 1</a></li>
<li><a href="#">Service 2</a></li>
<li><a href="#">Service 3</a></li>
</ul>
</li>
<li>
<!-- First Tier Drop Down -->
<label for="drop-2" class="toggle">Portfolio <span>+</span></label>
<a href="#">Portfolio</a>
<input type="checkbox" id="drop-2"/>
<ul>
<li><a href="#">Portfolio 1</a></li>
<li><a href="#">Portfolio 2</a></li>
<li>
<!-- Second Tier Drop Down -->
<label for="drop-3" class="toggle">Works <span>+<span></label>
<a href="#">Works</a>
<input type="checkbox" id="drop-3"/>
<ul>
<li><a href="#">HTML/CSS</a></li>
<li><a href="#">jQuery</a></li>
<li><a href="#">Python</a></li>
</ul>
</li>
</ul>
</li>
<li><a href="#">Blog</a></li>
<li><a href="#">Submit</a></li>
<li><a href="#">Contact</a></li>
<li><a href="#">About</a></li>
</ul>
</nav>
&#13;
JS + CSS - 演示(原创)
jQuery(document).ready(function($) {
$('<span class="submenu-button">+</span>').insertBefore('nav.main-menu#mobile ul li:not(.hide-mobile) ul').parent('li');
$('.submenu-button').click(function() {
$(this).toggleClass('open');
$(this).next().toggleClass('open').children('li').toggleClass('open').find('ul, .submenu-button').removeClass('open');
});
});
&#13;
nav.main-menu#mobile {
width: 400px;
}
nav.main-menu#mobile ul {
position: relative;
overflow: hidden;
margin: 0;
padding: 0 13px 0 .5px;
/*this line was edited*/
}
nav.main-menu#mobile ul li {
position: relative;
display: inline-block;
float: left;
width: 100%;
margin: 0px;
background-color: grey;
overflow: hidden;
/*this line is new*/
}
nav.main-menu#mobile ul li a {
display: block;
width: 85%;
margin: 0px 7.5% 0px 7.5%;
padding: 15px 0px 15px 0px;
background-color: pink;
color: #888888;
border-bottom: 1px solid #F1F1F1;
text-decoration: none !important;
}
.submenu-button {
position: absolute;
top: 0;
right: 29px;
float: right;
height: 15px;
padding: 17px 20px;
background-color: purple;
cursor: pointer;
z-index: 1;
}
.sub-menu .submenu-button{
right: 16px;
}
nav.main-menu#mobile ul ul {
max-height: 0;
transition: all 1.2s;
}
nav.main-menu#mobile ul ul.open {
max-height: 1000px;
}
nav.main-menu#mobile ul ul li {
margin: 0;
transition: all 1s;
opacity: 0;
transition: opacity 1.5s !important;
}
nav.main-menu#mobile ul ul li.open {
opacity: 1;
transition: opacity 1s !important;
}
nav.main-menu#mobile ul ul li a {
padding: 15px 0 15px 14px;
}
nav.main-menu#mobile ul ul ul li a {
padding: 15px 0 15px 23px;
/*this line was edited*/
}
/*----Below this line is new----*/
li.menu-item.menu-item-type-post_type.menu-item-object-page.menu-item-480.open {
padding-left: 1px;
}
nav.main-menu#mobile ul ul li a {
padding: 15px 0 15px 12px;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<nav class="main-menu" id="mobile">
<div class="menu-header-menu-container">
<ul id="menu-header-menu-1" class="menu">
<li class="hide-desktop menu-item menu-item-type-post_type menu-item-object-page menu-item-1386">
<a href="http://www.mywebsite.com">Link 1
</a>
</li>
<li class="open menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-463">
<a href="http://www.mywebsite.comwork/">Link 2
</a>
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-584">
<a href="http://www.mywebsite.comwork/landscapes/">Child-link 1
</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-473">
<a href="http://www.mywebsite.comwork/seascapes/">Child-link 2
</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-478">
<a href="http://www.mywebsite.comwork/macro/">Child-link 3
</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-477">
<a href="http://www.mywebsite.comwork/cities/">Child-link 4
</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-475">
<a href="http://www.mywebsite.comwork/long-exposure/">Child-link 5
</a>
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-480">
<a href="http://www.mywebsite.comwork/miscellaneous/">Grandchild-link 1
</a>
</li>
</ul>
</li>
</ul>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-2 current_page_item menu-item-has-children menu-item-10">
<a href="http://www.lucieaverillphotography.co.uk/about/">Link 3
</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-464">
<a href="http://www.lucieaverillphotography.co.uk/shop/">Link 4
</a>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-14">
<a href="http://www.lucieaverillphotography.co.uk/contact/">Link 5
</a>
</li>
</ul>
</div>
</nav>
&#13;
答案 1 :(得分:0)
如果我理解你想要做什么,下面的代码可能会有效。我只修改了JSfiddle链接中包含的CSS。
<强> CSS:强>
nav.main-menu#mobile {
width: 400px;
}
nav.main-menu#mobile ul {
position: relative;
overflow: auto;
margin: 0;
padding: 0 13px 0 .5px; /*this line was edited*/
}
nav.main-menu#mobile ul li {
position: relative;
display: inline-block;
float: left;
width: 100%;
margin: 0px;
background-color: grey;
overflow: hidden; /*this line is new*/
}
nav.main-menu#mobile ul li a {
display: inline-block;
height: auto;
width: 85%;
margin: 0px 7.5% 0px 7.5%;
padding: 15px 0px 15px 0px;
background-color: pink;
color: #888888;
border-bottom: 1px solid #F1F1F1;
text-decoration: none !important;
}
.submenu-button {
position: absolute;
top: 0;
right: 8px;
float: right;
width: 15px;
height: 19px;
padding: 17px;
background-color: purple;
cursor: pointer;
z-index: 1; /*this line is new*/
}
nav.main-menu#mobile ul ul {
max-height: 0;
transition: all 1.2s;
}
nav.main-menu#mobile ul ul.open {
max-height: 1000px;
}
nav.main-menu#mobile ul ul li {
margin: 0;
transition: all 1s;
opacity: 0;
transition: opacity 1.5s !important;
}
nav.main-menu#mobile ul ul li.open {
opacity: 1;
transition: opacity 1s !important;
}
nav.main-menu#mobile ul ul li a {
padding: 15px 0 15px 14px;
}
nav.main-menu#mobile ul ul ul li a {
padding: 15px 0 15px 23px; /*this line was edited*/
}
/*----Below this line is new----*/
li.menu-item.menu-item-type-post_type.menu-item-object-page.menu-item-480.open {
padding-left: 1px;
}
nav.main-menu#mobile ul ul li a {
padding: 15px 0 15px 12px;
}