使用CSS将导航栏移动到右侧

时间:2015-08-04 04:32:53

标签: html css

This is what I've done so far。我遇到的问题是我的<li>堆积在左角。如何让它们在右上角内嵌?

HTML

<div id="page">
    <div class="header Fixed"> <a href="#menu"></a>Demo
        <ul id="account">
            <li><a href="#">Username</a>

            </li>
            <li><a href="#">Logout</a>

            </li>
        </ul>
    </div>
    <div class="content" id="content">
        <div id="first">
            <p><strong>This is the first section.</strong>

                <br />Notice how the fixed header and footer slide out along with the page.</p>
            <p><a href="#menu">Open the menu.</a>

            </p>
        </div>
    </div>
    <div class="footer Fixed">Copyright @ 2015 , All rights reserved.</div>
    <nav id="menu" th:include="${filename}"></nav>
</div>

CSS

html, body {
    padding: 0;
    margin: 0;
}
body {
     background-color: #fff;
     font-family:'Lucida Grande', Tahoma, Verdana, sans-serif;
     font-size: 14px;
     line-height: 22px;
     color: #666;
     position: relative;
    -webkit-text-size-adjust: none;
}
body * {
     text-shadow: none;
}
h1, h2, h3, h4, h5, h6 {
     line-height: 1;
     font-weight: bold;
     margin: 20px 0 10px 0;
}
h1, h2, h3 {
    font-size: 18px;
}
h4, h5, h6 {
     font-size: 16px;
}
p {
    margin: 0 0 10px 0;
}
a, a:link, a:active, a:visited, a:hover {
    color: inherit;
    text-decoration: underline;
}
nav:not(.mm-menu) {
     display: none;
}
.header, .content, .footer {
     text-align: center;
}
.header, .footer {
     background: #0ca3d2;
     color: #fff;
     line-height: 40px;
     -moz-box-sizing: border-box;
     box-sizing: border-box;
     width: 100%;
    height: 40px;
    padding: 0 50px;
}
.header.fixed {
    position: fixed;
    top: 0;
    left: 0;
}
.footer.fixed {
    position: fixed;
    bottom: 0;
    left: 0;
}
.header a {
    background: center center no-repeat transparent;
    background-image: url(data:image/png;
base64, iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAYAAADE6YVjAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADhJREFUeNpi/P//PwOtARMDHQBdLGFBYtMq3BiHT3DRPU4YR4NrNAmPJuHRJDyahEeT8Ii3BCDAAF0WBj5Er5idAAAAAElFTkSuQmCC);
    font-size: 16px;
    font-weight: bold;
    display: block;
    width: 40px;
    height: 40px;
    position: absolute;
    top: 0;
    left: 10px;
}
.footer a {
    font-size: 12px;
    font-weight: bold;
    display: block;
    bottom: 0;
}
.content {
    padding: 150px 50px 50px 50px;
}
#account li {
    list-style-type: none;
    top: 0;
    display: inline;
    padding: 0px 5px;
    float: right;
}
#account li a {
    text-align: right;
    text-decoration: none;
}
#account li a:hover {
    color: #f0ad4e;
}
#intro, #first {
    height: 400px;
}
#intro {
    padding-top: 0;
}
#first {
    border-top: 1px solid #ccc;
    padding-top: 150px;
}

3 个答案:

答案 0 :(得分:0)

尝试使用float:right;如果显示代码会更容易......

答案 1 :(得分:0)

而不是:

.header a

使用

.header > a

以及更多样式更改:

#account {
    float: right;
}
#account li {
    list-style-type: none;
    padding: 0px 5px;
    display: inline;
}

您在代码中犯了一些错误。检查您编写的浮动属性。研究:https://developer.mozilla.org/en-US/docs/Web/CSS/float

此外,您使用position:absolute作为.header a,这将适用于标题下的所有锚点。这是主要问题。

希望它对你有所帮助。

答案 2 :(得分:0)

  

position:absolute ---元素相对于第一个元素定位   定位(非静态)祖先元素

问题是你有这种风格:

.header a{
    position: absolute;
    top: 0;
    left: 10px;
}

因此,您的所有<a>都定位于topleft,因为您没有任何定位(非静态)祖先元素。

position: relative设置为ul#account li,然后就可以了。

这是您想要实现的最小示例(评论img链接)

.header {
    background: #0ca3d2;
    color: #fff;
    line-height: 40px;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
    width: 100%;
    height: 40px;
    padding: 0 50px;
}

.fixed{
    position: fixed;
    top: 0;
    left: 0;
}

#account{
    margin: 0;
    float: right;
    text-align: center;
}

#account li{
    float: left;   
    list-style: none;    
}

#account li a{
    display: inline-block;
    line-height: 40px;
    color: #fff;
    padding: 0 15px;
    text-decoration: none;
}
#account li a:hover{
    background: #216A9D;
}
<div class="header fixed"> <a href="#menu"></a>Brand Name
    <ul id="account">
        <li><a href="#">Username</a>

        </li>
        <li><a href="#">Logout</a>

        </li>
    </ul>
</div>