gpt4 book ai didi

html - 无序列表中子菜单列表项的继承问题

转载 作者:行者123 更新时间:2023-11-28 05:36:35 25 4
gpt4 key购买 nike

我正在调整一个响应式移动菜单,使其显示为跨各种移动设备屏幕尺寸的全屏布局,因此我使用链接宽度的百分比 (85%) 而不是固定值我在更大的屏幕上。

我将 li 设置为 width:100% 并将 a 标签设置为 width:85%。在从属列表项上,我在左侧添加了 14px 的填充,以使它们在视觉上脱颖而出。问题在于,在左侧填充的情况下,列表项会随着菜单的每一级而变宽。

我找到的仅有的两种解决此问题的方法导致了新问题。例如,我尝试将 lia 标签设为 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;
}

最佳答案

我已经检查了您的菜单,它有一些问题。我决定像您的引用网站一样创建纯 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%;
}
}
<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>

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');
});

});
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;
}
<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>

关于html - 无序列表中子菜单列表项的继承问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38171486/

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