html - float :Right on one list item not working

标签 html css nav

The Error

因此,我尝试 float:right; 时收到上述错误Login/Signup关联。我似乎无法让它与我当前的设置一起工作。我尝试将其向左浮动,但最终只是将 li按 100% 宽度的标准顺序。我想知道我是不是个白痴 css并且不能做这样简单的事情。目标是最后一个<li>坐在右边

代码:HTML

    <div id="mainNav">
    <nav class="myNav">
        <ul class="myNavBar">
            <li>
                <a href="index.html">Home</a>
            </li>
            <li>
                <a href="user.html">Sample User</a>
            </li>
            <li>
                <a href="jobs.html">Sample Jobs</a>
            </li>
            <li>
                <a href="findcom.html">Sample Find</a>
            </li>
            <li class="right-align">
                <a href="login.html">Login/Signup</a>
            </li>
            <br class="clearBoth"/>
        </ul>
    </nav>
</div>

代码:CSS

.myNav {
    padding: 15px;
    margin: 0;
    margin-bottom: 5px;
    background-color: #81A7ED;
    padding-bottom: 5px;
}

.myNavBar {
    list-style-type: none;
    width: 100%;
    margin-right: 15px;
    margin-left:15px;
}

.myNavBar > li {
    display: inline;
    width: 100%;
}

.myNavBar > li > a {
    color: #0C3A8E;
    box-sizing: border-box;
    border-bottom: solid 2px #81A7ED;
    padding-left: 1%;
    padding-right: 1%;
    padding-top: 7px;
    padding-bottom: 7px;
    font-size: small;
    transition: border-bottom .25s;
}

.myNavBar > li > a:hover {
    border-bottom: solid 2px #0C3A8E;
    text-decoration: none;
    transition: border-bottom .25s;
}
.myNavBar > .right-align {
    float:right;
}

非常感谢您的帮助!

最佳答案

由于某种原因您设置了width: 100%;到您的 LI 元素

还有<br class="clearBoth"/>不是 UL 的有效子级:)

<强> jsBin

修改后的 HTML:

<ul class="myNavBar cf">

您编辑的 CSS 和注释:

*{padding:0; margin:0;} /* Do you use some CSS reset? */
a{  text-decoration: none; }
/* Clearfix   http://nicolasgallagher.com/micro-clearfix-hack/ */
.cf:before,
.cf:after {
    content: " "; /* 1 */
    display: table; /* 2 */
}
.cf:after { clear: both; }
/* Used to clear floats */


/* Now... */
nav.myNav {
    padding: 15px;
    margin-bottom: 5px;
    background-color: #81A7ED;
}

ul.myNavBar {
    list-style: none;
    /*width: 100%; it's already "100%" by defalt*/
    /* ??? 100% and than margin? 
    margin-right: 15px; 
    margin-left:15px;
    */
}
ul.myNavBar > li {
    float:left;
    /*width: 100%; why 100? */
}
ul.myNavBar > li > a {
    display: inline-block; /* Note this !*/
    color: #0C3A8E;
    box-sizing: border-box;
    border-bottom: solid 2px #81A7ED;
    padding: 7px 15%; /* Note this */
    font-size: small;
    transition: border-bottom .25s;
}
ul.myNavBar > li > a:hover {
    border-bottom: solid 2px #0C3A8E;
    /*transition: border-bottom .25s;  is already defined! */
}
.myNavBar > .right-align {
    float:right;
}

关于html - float :Right on one list item not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24093336/

相关文章:

html - Bootstrap 3 侧导航环绕

html - 导航栏中的文本居中问题

用于过滤的 jQuery 同位素不起作用

javascript - 需要 div 悬停然后在 ipad/iphone 上通过两次触摸转到其他页面

javascript - 喜欢按钮不发布在我的墙上

javascript - 谷歌图表工具提示中的 DIV 定位

css - Chrome 3D CSS 转换交叉点无法正确呈现

css - Twitter Bootstrap 带导航 - 响应问题

html - 为复选框旁边的标签设置边距

html - 奇怪的(?)歌剧漂浮