gpt4 book ai didi

css -
  • 没有占据侧边栏的整个宽度
  • 转载 作者:行者123 更新时间:2023-11-27 23:06:28 25 4
    gpt4 key购买 nike

    我遇到了 <li> 的问题不占用侧边栏的整个宽度,即左侧有一些未着色的空间(带有 background-color: red; )。我知道空格来自这一行:

    padding: 0.5em 0 0.5em 3em;

    但我不确定如何在不使用填充的情况下实现相同的效果。我想要这样填充列表项,但我也想要我的内部嵌套 <li> s 占据了侧边栏的整个宽度。

    点击 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>

    最佳答案

    试试这个答案。我在样式应用于嵌套列表和嵌套项的两个地方更改了 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>

    关于css - <li> 没有占据侧边栏的整个宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58716941/

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