我遇到一个问题,就是<li>
没有占据边栏的全部宽度,即,左侧有一些未着色的空间(带有background-color: red;
)。我知道空间来自这一行:
padding: 0.5em 0 0.5em 3em;
但我不确定如何不使用填充来达到相同的效果。我希望这样填充列表项,但是我也希望内部嵌套的<li>
占据边栏的整个宽度。
点击demo中的“交易”,您可以看到我在说什么。 因此,再过一次,例如“新交易”应涂成红色,并占据侧边栏从左到右的整个宽度。
document.addEventListener('DOMContentLoaded', () => {
let listItems = document.querySelectorAll('nav > ul > li');
function onListItemClick() {
for (let listItem of listItems) {
listItem.classList.remove('active');
listItem.children[0].classList.remove('dot');
if (listItem.children.length > 2) {
listItem.children[2].style.display = 'none';
}
}
this.classList.add('active');
this.children[0].classList.add('dot');
if (this.children.length > 2) {
this.children[2].style.display = 'block';
}
}
listItems.forEach(function(listItem) {
listItem.addEventListener('click', onListItemClick);
});
});
.sidebar {
background-color: #F2F2F2;
width: 300px;
height: 100%;
position: fixed;
font-family: sans-serif;
}
.sidebar>nav>ul {
list-style: none;
line-height: 2em;
padding: 0;
margin: 10em 0 0 0;
}
.sidebar>nav>ul>li {
display: grid;
grid-template-columns: 25px 1fr;
color: #959595;
font-size: 18px;
padding: 0.5em 0 0.5em 3em;
}
.sidebar>nav>ul>li:hover,
.sidebar>nav>ul>li>ul>li:hover {
color: #757575;
font-weight: bold;
cursor: pointer;
}
.sidebar>nav>ul>li>ul {
list-style: none;
padding: 0;
margin: 0;
width: 100%;
}
.sidebar>nav>ul>li>ul>li {
display: block;
background-color: red;
color: #959595;
font-size: 14px;
width: 200px;
font-weight: normal;
padding: 0 0 0 2em;
}
.dot {
border-radius: 50%;
background-color: #33ad93;
width: 10px;
height: 10px;
display: inline-block;
margin: auto;
}
.active {
color: black !important;
font-weight: bold;
}
<div class="sidebar">
<nav>
<ul>
<li>
<span></span>
<span>Dashboard</span>
</li>
<li>
<span></span>
<span>Transactions</span>
<ul style="display:none;">
<li>New Transaction</li>
<li>View Transactions</li>
</ul>
</li>
<li>
<span></span>
<span>Budgets</span>
</li>
</ul>
</nav>
</div>
答案 0 :(得分:0)
尝试此答案。我在两个将样式应用于嵌套列表和嵌套项目的地方更改了CSS样式。
.sidebar > nav > ul > li > ul {
list-style: none;
padding: 0;
margin: 0;
margin-left: -3em;
width: 300px;
}
.sidebar > nav > ul > li > ul > li {
display: block;
background-color: red;
color: #959595;
font-size: 14px;
width: calc(100% - 5em);
font-weight: normal;
padding: 0 0 0 5em;
}
document.addEventListener('DOMContentLoaded', () => {
let listItems = document.querySelectorAll('nav > ul > li');
function onListItemClick() {
for (let listItem of listItems) {
listItem.classList.remove('active');
listItem.children[0].classList.remove('dot');
if (listItem.children.length > 2) {
listItem.children[2].style.display = 'none';
}
}
this.classList.add('active');
this.children[0].classList.add('dot');
if (this.children.length > 2) {
this.children[2].style.display = 'block';
}
}
listItems.forEach(function(listItem) {
listItem.addEventListener('click', onListItemClick);
});
});
.sidebar {
background-color: #F2F2F2;
width: 300px;
height: 100%;
position: fixed;
font-family: sans-serif;
}
.sidebar > nav > ul {
list-style: none;
line-height: 2em;
padding: 0;
margin: 10em 0 0 0;
}
.sidebar > nav > ul > li {
background-color: red
display: grid;
grid-template-columns: 25px 1fr;
color: #959595;
font-size: 18px;
padding: 0.5em 0 0.5em 3em;
}
.sidebar > nav > ul > li:hover, .sidebar > nav > ul > li > ul > li:hover {
color: #757575;
font-weight: bold;
cursor: pointer;
}
.sidebar > nav > ul > li > ul {
list-style: none;
padding: 0;
margin: 0;
margin-left: -3em;
width: 300px;
}
.sidebar > nav > ul > li > ul > li {
display: block;
background-color: red;
color: #959595;
font-size: 14px;
width: calc(100% - 5em);
font-weight: normal;
padding: 0 0 0 5em;
}
.dot {
border-radius: 50%;
background-color: #33ad93;
width: 10px;
height: 10px;
display: inline-block;
margin: auto;
}
.active {
color: black !important;
font-weight: bold;
}
<div class="sidebar">
<nav>
<ul>
<li>
<span></span>
<span>Dashboard</span>
</li>
<li>
<span></span>
<span>Transactions</span>
<ul style="display:none;">
<li>New Transaction</li>
<li>View Transactions</li>
</ul>
</li>
<li>
<span></span>
<span>Budgets</span>
</li>
</ul>
</nav>
</div>