【问题标题】:Float:Right on one list item not working浮动:一个列表项的右侧不起作用
【发布时间】:2014-06-07 03:23:00
【问题描述】:

所以我通过尝试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;
}

感谢您的帮助!

【问题讨论】:

    标签: html css nav


    【解决方案1】:

    出于某种原因,您已将 width: 100%; 设置为您的 LI 元素

    &lt;br class="clearBoth"/&gt; 也不是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;
    }
    

    【讨论】:

    • 我想说我以你的名义面对并感谢我在这个世界上得到的一切。谢谢。
    • 最后一个
    • 与其余部分之间仍有很大差距。
  • 对,我想把最后一个
  • 对齐到右边,这样我就可以和其他部分分开了
  • @user1449940 我已经完全编辑了你的 CSS。注意我的cmets,希望有用。
  • @AliGajani OP 的代码中有很多错误,以至于我需要比你想象的更多的时间来写我的答案。感谢您的评论
  • 【解决方案2】:

    只需删除类

    class="right-align"
    

    来自说 li 元素。您的代码将按您的意愿运行。

    【讨论】:

    • 我不够具体,抱歉。这是回答原始问题的赞成票。
    • OP 需要解决的问题远不止简单地删除一个 class
    【解决方案3】:

    您需要做的就是删除: .myNavBar > .right-align { 浮动:对; }

    【讨论】:

      【解决方案4】:

      只需删除 CSS 文件中的这些行:

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

      另外,最好将浮动的&lt;li&gt; 作为&lt;li&gt; 的第一个元素放在&lt;ul&gt;

      <ul class="myNavBar">
          <li class="right-align">
              <a href="login.html">Login/Signup</a>
          </li>
          <li>...</li>
      </ul>
      

      【讨论】:

        猜你喜欢
        相关资源
        最近更新 更多
        热门标签