【问题标题】:Is it possible to have different alignments for list items within a navbar menu?导航栏菜单中的列表项是否可以有不同的对齐方式?
【发布时间】:2021-07-10 01:29:23
【问题描述】:

我有一个用 CSS 完成的响应式折叠导航栏菜单。我正在使用 bulma CSS 框架来构建我的页面。

我试图在左侧永久显示徽标,菜单列表项水平居中。我会使用相同的技术将徽标左对齐,就像将搜索框右对齐一样。

这是我目前的尝试:

body {
    font-family: Helvetica, Arial, sans-serif;
    background-color: rgb(236, 236, 236);
    margin: 0em;
    color: rgb(19, 51, 61);
}

p {
    font-size: 18px;
    line-height: 1.5;
    font-weight: bold;
    color: rgb(51, 51, 51);
    max-width: 55em;
}

p a {
    color: rgb(41, 183, 206);
    text-decoration: none;
}

strong {
    color: rgb(41, 183, 206);
}

nav {
    font-size: 12px;
    background-color: rgb(19, 51, 61);
    box-shadow: 0 1px 2px rgba(19, 51, 61, 0.5);
    padding: 0 1em;
    height: 44px;
    overflow: hidden;
    text-align: center;
}

nav img {
    text-align: left;
}

nav ul {
    margin: 0;
    padding: 0;
    list-style-type: none;
    max-height: 88px;
    position: relative;
}

nav li {
    display: inline-block;
}

nav a {
    display: inline-block;
    padding: 0 1em;
    color: rgb(236, 236, 236);
    font-weight: 700;
    letter-spacing: 0.1em;
    text-decoration: none;
    text-transform: uppercase;
    white-space: nowrap;
    line-height: 44px;
    height: 44px;
}

nav a:hover {
    background-color: rgba(255, 255, 255, 0.08);
}

nav li:last-child { 
    position: absolute; 
    right: 0;
    bottom: 44px; 
    background-image: linear-gradient(to right, rgba(19, 51, 61, 0) 0, rgba(19, 51, 61, 1) 2em);
    padding-left: 3em;
}

nav li:nth-last-child(2) { 
    display: none;
}

nav#menu:target {
    height: auto;
    padding: 0;
}

nav#menu:target ul {
    max-height: none;
}

nav#menu:target li {
    display: block;
}

nav#menu:target a {
    display: block;
    padding: 0 2em;
    background-color: rgba(255, 255, 255, 0.05);
}

nav#menu:target a:hover {
    background-color: rgba(255, 255, 255, 0.08);
}

nav#menu:target li:not(:first-child) {
    margin-top: 2px;
}

nav#menu:target li:last-child {
    display: none;
}

nav#menu:target li:nth-last-child(2) {
    display: inline-block;
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
    border-left: 2px solid rgb(19, 51, 61);
}
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.2/css/bulma.min.css">
  </head>
  <body>
<nav id="menu">
    <ul id="menu-closed">
        <li><img src="https://camo.githubusercontent.com/971caf673b2a29c7b6d6af6c74b9d7e649214eb1765bfef0848b68d3ce3a40fb/68747470733a2f2f63646e2e737667706f726e2e636f6d2f6c6f676f732f766974656a732e737667" style="max-height: 44px;"></li>
        <li><a href="#">Test1</a></li>
        <li><a href="#">Test2</a></li>
        <li><a href="#">Test3</a></li>
        <li><a href="#">Test4</a></li>
        <li><a href="#">Test5</a></li>
        <li><a href="#menu-closed">&#215; Close menu</a></li>
        <li><a href="#menu">&#9776; Menu</a></li>
    </ul>
</nav>

我尝试了各种方法,以及一些 bulmas 定位类,但无论我做什么,我似乎都无法让它粘在左边。

这里的正确方法是什么?

额外问题:如果图像被删除,我如何保持列表元素垂直居中?

【问题讨论】:

    标签: css menu navigation bulma


    【解决方案1】:

    将您的徽标设置在左侧的绝对位置,并将等效的相同填充添加到左侧和右侧的菜单列表中。

    body {
        font-family: Helvetica, Arial, sans-serif;
        background-color: rgb(236, 236, 236);
        margin: 0em;
        color: rgb(19, 51, 61);
    }
    
    p {
        font-size: 18px;
        line-height: 1.5;
        font-weight: bold;
        color: rgb(51, 51, 51);
        max-width: 55em;
    }
    
    p a {
        color: rgb(41, 183, 206);
        text-decoration: none;
    }
    
    strong {
        color: rgb(41, 183, 206);
    }
    
    nav {
        font-size: 12px;
        background-color: rgb(19, 51, 61);
        box-shadow: 0 1px 2px rgba(19, 51, 61, 0.5);
        padding: 0 1em;
        height: 44px;
        overflow: hidden;
        text-align: center;
    }
    
    nav img {
        text-align: left;
    }
    
    nav ul {
        margin: 0;
        padding: 0;
        list-style-type: none;
        max-height: 88px;
        position: relative;
    }
    
    nav li {
        display: inline-block;
    }
    
    nav a {
        display: inline-block;
        padding: 0 1em;
        color: rgb(236, 236, 236);
        font-weight: 700;
        letter-spacing: 0.1em;
        text-decoration: none;
        text-transform: uppercase;
        white-space: nowrap;
        line-height: 44px;
        height: 44px;
    }
    
    nav a:hover {
        background-color: rgba(255, 255, 255, 0.08);
    }
    
    nav li:last-child { 
        position: absolute; 
        right: 0;
        bottom: 44px; 
        background-image: linear-gradient(to right, rgba(19, 51, 61, 0) 0, rgba(19, 51, 61, 1) 2em);
        padding-left: 3em;
    }
    
    nav li:nth-last-child(2) { 
        display: none;
    }
    
    nav#menu:target {
        height: auto;
        padding: 0;
    }
    
    nav#menu:target ul {
        max-height: none;
    }
    
    nav#menu:target li {
        display: block;
    }
    
    nav#menu:target a {
        display: block;
        padding: 0 2em;
        background-color: rgba(255, 255, 255, 0.05);
    }
    
    nav#menu:target a:hover {
        background-color: rgba(255, 255, 255, 0.08);
    }
    
    nav#menu:target li:not(:first-child) {
        margin-top: 2px;
    }
    
    nav#menu:target li:last-child {
        display: none;
    }
    
    nav#menu:target li:nth-last-child(2) {
        display: inline-block;
        position: absolute;
        top: 0;
        right: 0;
        margin: 0;
        border-left: 2px solid rgb(19, 51, 61);
    }
    
    nav#menu > ul {
      padding-right: 50px;
      padding-left: 50px;
    }
    
    .logo {
      position: absolute;
      left: 0;
    }
    <html>
      <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.2/css/bulma.min.css">
      </head>
      <body>
    <nav id="menu">
        <ul id="menu-closed">
            <li class="logo"><img src="https://camo.githubusercontent.com/971caf673b2a29c7b6d6af6c74b9d7e649214eb1765bfef0848b68d3ce3a40fb/68747470733a2f2f63646e2e737667706f726e2e636f6d2f6c6f676f732f766974656a732e737667" style="max-height: 44px;"></li>
            <li><a href="#">Test1</a></li>
            <li><a href="#">Test2</a></li>
            <li><a href="#">Test3</a></li>
            <li><a href="#">Test4</a></li>
            <li><a href="#">Test5</a></li>
            <li><a href="#menu-closed">&#215; Close menu</a></li>
            <li><a href="#menu">&#9776; Menu</a></li>
        </ul>
    </nav>

    【讨论】:

    • 效果很好,谢谢。早该知道就这么简单。您还知道如何将徽标垂直居中吗?我在玩设计时使用了一个稍微不同的标志,它设置在顶部
    • top: 10px; 或任何足以使其居中的数字。 margin-top: 10px; 也应该可以工作。 top: 50%; transform: translateY(-50%) - 也应该工作。您还可以重写所有代码以在左侧(徽标)和右侧(搜索)具有相等宽度的块,只需使用align-items: center;。有很多方法可以实现你想要的,我建议你深入研究 CSS 并了解它是如何工作的 :)
    • 啊,我多年来一直在尝试深入研究 CSS,这不是我工作的主要领域,我在安全领域工作。但是,我继续得到它。非常感谢您的帮助:)
    猜你喜欢
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 1970-01-01
    相关资源
    最近更新 更多