对齐方式不适用于所有子菜单项

时间:2019-04-10 13:42:19

标签: html css css3 flexbox

除了“中国日历”选项卡以外,所有菜单中的项目均等地隔开以弯曲容器,我不明白为什么。 它们完美对齐,但是当我将链接添加到子菜单项时,它会随机散布。我几乎可以肯定,我必须在{}中添加一些内容来纠正此问题,但是我尝试了以下操作,但没有解决方法:

  • 尝试删除链接。无法修复。
  • 试图将子菜单中的代码复制到CSS中的{}中,使问题变得更糟。 enter image description here
* {
    font-family: arial, sans-serif; 
    box-sizing: border-box;}

html, body {
    margin: 0;
    padding: 0;
}

a {
    text-decoration: none;
    color: black;
}

.nav {
    position:fixed; 
    top:0; 
    left:0;
    background-color: rgba(255,255,255,.8);
    border-radius: 0px;
    border: none;
    width: 100%;
    margin: 0;
    padding: 25px 0;
    flex-direction: row;
    display: flex;
    align-items: center;
    justify-content: space-evenly;
}

.item {
    color: black;
    font-weight: bold;
    text-transform: uppercase;
}

.submenu {
    display: none;
    flex-wrap: wrap;
    align-items: center;
    align-text: center;
    position: absolute;
    padding-top: 107px;
    padding: 10px;
    left: 0;
    right: 0;
    text-transform: uppercase;
    z-index: 1;
    background-color: #2F4F4F;
    color: white;
    justify-content: space-evenly;
}

.submenu li {
    margin-left: 6%;
    width: 19%;
    padding: 5px;

}

.item.has-children:hover .submenu {
    display: flex;
    align-items: center;
    flex-direction: row;
    justify-content: space-evenly;
    flex-wrap: wrap;
    flex: 1 1 calc(25% - 80px);
    color: black;
    background-color: rgba(255,255,255,.8);
}

ul {
    list-style: none;
    padding: 0;
}
<nav>
    <ul class="nav">
        <li class="item">
            <a href="../index.html">
                <img src="../Images/Navigation/Intak Logo 25px High.png" alt="Home" />
            </a>
        </li>
        <li class="item has-children" style="color:#4D4D4D;">Printing
            <ul class="submenu">
                <li>Labels & Stickers</li>
                <li>Banners</li>
                <li>A-Frame</li>
                <li>Menu Boards</li>
                <li>Takeout Menus</li>
                <li>Business Cards</li>
                <li>Dine-In Menus</li>
                <li>Posters</li>
                <li>Envelopes</li>
                <li>Chinese Wedding Cards</li>
                <li>Flyers</li>
                <li>Letterheads</li>
                <li>Brochures</li>
                <li>Vinyl</li>
                <li>NCR Forms</li>
                <li>Catalogues</li>
            </ul>
        </li>
        <li class="item has-children">Graphic Design
            <ul class="submenu">
                <li>Logo Design</li>
                <li>Ads/Flyers/Promotions</li>
                <li style="text-align: center;">Menu Boards<br>
                    (Digital & Boards)</li>
                <li style="text-align: center;">Restaurant Menus<br>
                    (Takeout & Dine-In)</li>
            </ul>
        </li>
        <li class="item has-children">Chinese Calendars
            <ul class="submenu">
                <li><a href="Calendars/Cane Wallscroll Calendars.html">Cane Wallscroll Calendars</a></li>
                <li><a href="Calendars/Wall Calendars.html">Wall Calendars</a></li>
                <li><a href="Calendars/Mini Calendars.html">Mini Calendars</a></li>
                <li><a href="Calendars/Desk Calendars.html">Desk Calendars</a></li>
                <li><a href="Calendars/Special Desk Calendars.html">Special Desk Calendars</a></li>
                <li><a href="Calendars/Red Packet.html">Packet Calendars</a></li>
                <li><a href="Calendars/More.html">More Calendars</a></li>
            </ul>
        </li>
        <li class="item">FAQS</li>
        <li class="item">Contact Us</li>
    </ul>
</nav>

预期结果是所有导航子菜单项都将对齐

1 个答案:

答案 0 :(得分:1)

您只需将justify-content: space-evenly;的样式从justify-content: left;更改为.item.has-children:hover .submenu

子菜单样式应如下所示:

.item.has-children:hover .submenu {
    display: flex;
    align-items: center;
    flex-direction: row;
    justify-content: left;
    flex-wrap: wrap;
    flex: 1 1 calc(25% - 80px);
    color: black;
    background-color: rgba(255,255,255,.8);
}

关于中国日历的子菜单,只有7个项目,这导致它在第一行中有4个,在第二行中有3个,因此导致它们看起来好像没有对齐,因为{{1} }样式使它们以均匀的空间居中。

它与链接无关。

通过将它们设置为左侧,可以防止左侧的空白与第二行的位置混淆。

之前和之后(带有红色边框的方框可见)

space-evenly before

justify-content: space-evenly; after