使用Flex分割导航项目

时间:2019-03-05 18:38:24

标签: html css flexbox

基本上,我正在尝试将li中的其他两项与其他项分开,以放在.nav-content容器的flex-end位置。我意识到我的问题源于我目前将width: 50%;设置为ul class="nav-links"的事实,但这是我能够获得项目之间的间距以展示我正在尝试做的事情的唯一方法。理想情况下,我只想将最后两个项目.login-button first-login.login button second-login移动到容器.nav-content的末尾,同时保持项目之间的间隔与现在的状态相同。

我尝试在margin-left: auto类上设置.login-button,但是由于页边距设置为50%,因此它将尝试包装按钮并切断按钮。我还尝试将宽度设置为100%(.nav-content容器的宽度),但是项目之间的间距被丢弃了。

我知道可能有一个我看不到的简单解决方案,将非常感谢您的帮助!

.nav-content {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    min-height: 8vh;
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    margin-top: 30px;
}

.nav-links {
    display: flex;
    justify-content: space-between;
    width: 50%;
    margin-left: 50px;
}

.nav-links li {
    list-style: none;
}

.nav-links a {
    text-decoration: none;
    letter-spacing: 2px;
    font-weight: 400;
    font-size: 12px;
    color: black;
    text-transform: uppercase;
    position: relative;   
}

.nav-links a.item {
    text-decoration: none;
    letter-spacing: 2px;
    font-weight: 700;
    font-size: 12px;
    color: black;
    text-transform: uppercase;
    position: relative;
}

.nav-links a.item:hover {
    color: black;
}

.nav-links a.item:before {
    content: "";
    position: absolute;
    width: 100%;
    height: 2px;
    bottom: -5px;
    left: 0;
    background-color: blue;
    visibility: hidden;
    -webkit-transform: scaleX(0);
    transform: scaleX(0);
    -webkit-transition: all 0.3s ease-in-out 0s;
    transition: all 0.3s ease-in-out 0s;
}

.nav-links a.item:hover:before {
    visibility: visible;
    -webkit-transform: scaleX(1);
    transform: scaleX(1);
}

.nav-links li a.login-button {
    background: blue;
    padding: 10px 30px;
    color: white;
    box-shadow: 0 15px 10px -10px rgba($color: #000000, $alpha: 0.2);
    font-weight: 400;
    text-transform: capitalize;
}

.nav-links li a.login-button:hover {
    background: green;
}
   <header id="header">
        <section class="wrapper">
            <nav class="nav-content">
                <a href="#" class="logo">Logo</a>
                <ul class="nav-links">
                    <li><a href="#" class="item">Item 1</a></li>
                    <li><a href="#" class="item">Item 2</a></li>
                    <li><a href="#" class="item">Item 3</a></li>
                    <li><a href="#" class="item">Item 4</a></li>
                    <li><a href="#" target="_blank" class="login-button first-login">First Login</a></li>
                    <li><a href="#" target="_blank" class="login-button second-login">Second Login</a></li>
                </ul>
            </nav>
        </section>    
    </header>

1 个答案:

答案 0 :(得分:0)

像这样吗?

将另一个ul中的两个按钮包装到容器的末端... 喜欢:

<ul class="nav-links navbar-nav ml-auto">

检查下面的代码

.nav-content {
    display: flex;
    justify-content: flex-start;
    align-items: center;
    min-height: 8vh;
    font-family: 'Roboto', sans-serif;
    font-weight: 400;
    margin-top: 30px;
}

.nav-links {
    display: flex;
    justify-content: space-between;
    width: 50%;
    margin-left: 50px;
}

.nav-links li {
    list-style: none;
}

.nav-links a {
    text-decoration: none;
    letter-spacing: 2px;
    font-weight: 400;
    font-size: 12px;
    color: black;
    text-transform: uppercase;
    position: relative;   
}

.nav-links a.item {
    text-decoration: none;
    letter-spacing: 2px;
    font-weight: 700;
    font-size: 12px;
    color: black;
    text-transform: uppercase;
    position: relative;
}

.nav-links a.item:hover {
    color: black;
}

.nav-links a.item:before {
    content: "";
    position: absolute;
    width: 100%;
    height: 2px;
    bottom: -5px;
    left: 0;
    background-color: blue;
    visibility: hidden;
    -webkit-transform: scaleX(0);
    transform: scaleX(0);
    -webkit-transition: all 0.3s ease-in-out 0s;
    transition: all 0.3s ease-in-out 0s;
}

.nav-links a.item:hover:before {
    visibility: visible;
    -webkit-transform: scaleX(1);
    transform: scaleX(1);
}

.nav-links li a.login-button {
    background: blue;
    padding: 10px 30px;
    color: white;
    box-shadow: 0 15px 10px -10px rgba($color: #000000, $alpha: 0.2);
    font-weight: 400;
    text-transform: capitalize;
    float: right;
}

.nav-links li a.login-button:hover {
    background: green;
}
<header id="header">
        <section class="wrapper">
            <nav class="nav-content">
                <a href="#" class="logo">Logo</a>
                <ul class="nav-links">
                    <li><a href="#" class="item">Item 1</a></li>
                    <li><a href="#" class="item">Item 2</a></li>
                    <li><a href="#" class="item">Item 3</a></li>
                    <li><a href="#" class="item">Item 4</a></li>
                   
                </ul>
        <ul class="nav-links navbar-nav ml-auto">
 <li><a href="#" target="_blank" class="login-button first-login">First Login</a></li>
                    <li><a href="#" target="_blank" class="login-button second-login">Second Login</a></li>
        </ul>
            </nav>
        </section>    
    </header>