将导航栏对齐到右侧

时间:2015-01-27 02:36:34

标签: html css alignment navbar

嗨,我有这个导航栏,并想知道如何将它向右下方。 我试过漂浮:对,但不起作用。也试过div对齐但不工作 同样。我虽然是css的新手。

<div align="right">
        <ul id="menu">
            <li><a href="#">Home</a></li>
            <li>
              <a href="#">About ↓</a>
              <ul class="hidden">
                <li><a href="#">Who We Are</a></li>
                <li><a href="#">What We Do</a></li>
              </ul>
            </li>
        </ul>
</div>

CSS

/*
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
*/
/* 
    Created on : 01 27, 15, 9:58:38 AM
    Author     : jefloresca
*/

/*Strip the ul of padding and list styling*/
ul {
        list-style-type:none;
        margin:0;
        padding:0;
        position: absolute;
}

/*Create a horizontal list with spacing*/
li {
        display:inline-block;
        float: right;
        margin-right: 1px;
}

/*Style for menu links*/
li a {
        display:block;
        min-width:140px;
        height: 50px;
        text-align: center;
        line-height: 50px;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        color: #fff;
        background: #2f3036;
        text-decoration: none;
}

/*Hover state for top level links*/
li:hover a {
        background: #19c589;
}

/*Style for dropdown links*/
li:hover ul a {
        background: #f3f3f3;
        color: #2f3036;
        height: 40px;
        line-height: 40px;
}

/*Hover state for dropdown links*/
li:hover ul a:hover {
        background: #19c589;
        color: #fff;
}

/*Hide dropdown links until they are needed*/
li ul {
        display: none;
}

/*Make dropdown links vertical*/
li ul li {
        display: block;
        float: none;
}

/*Prevent text wrapping*/
li ul li a {
        width: auto;
        min-width: 100px;
        padding: 0 20px;
}

/*Display the dropdown on hover*/
ul li a:hover + .hidden, .hidden:hover {
        display: block;
}


/*Hide checkbox*/
input[type=checkbox]{
    display: none;
}

/*Show menu when invisible checkbox is checked*/
input[type=checkbox]:checked ~ #menu{
    display: block;
}


/*Responsive Styles*/

@media screen and (max-width : 760px){
        /*Make dropdown links appear inline*/
        ul {
                position: static;
                display: none;
        }
        /*Create vertical spacing*/
        li {
                margin-bottom: 1px;
        }
        /*Make all menu links full width*/
        ul li, li a {
                width: 100%;
        }
        /*Display 'show menu' link*/
        .show-menu {
                display:block;
        }
}

这是指向codepen的链接:http://codepen.io/anon/pen/JoJqMZ

3 个答案:

答案 0 :(得分:2)

align="right" attribute is deprecated。不要用它!

删除ul元素上的绝对定位,然后将元素向右浮动or add right: 0

Updated Example

<div>
  <ul id="menu">
    <li><a href="#">Home</a></li>
    <li>
      <a href="#">About ↓</a>
      <ul class="hidden">
        <li><a href="#">Who We Are</a></li>
        <li><a href="#">What We Do</a></li>
      </ul>
    </li>
  </ul>
</div>
ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    float: right;
}
li {
    display: inline-block;
    float: right;
    margin-right: 1px;
}
li a {
    display: block;
    min-width: 140px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    color: #fff;
    background: #2f3036;
    text-decoration: none;
}
li:hover a {
    background: #19c589;
}
li:hover ul a {
    background: #f3f3f3;
    color: #2f3036;
    height: 40px;
    line-height: 40px;
}
li:hover ul a:hover {
    background: #19c589;
    color: #fff;
}
li ul {
    display: none;
}
li ul li {
    display: block;
    float: none;
}
li ul li a {
    width: auto;
    min-width: 100px;
    padding: 0 20px;
}
ul li a:hover + .hidden, .hidden:hover {
    display: block;
}
input[type=checkbox] {
    display: none;
}
input[type=checkbox]:checked ~ #menu {
    display: block;
}
@media screen and (max-width : 760px) {
    ul {
        position: static;
        display: none;
    }
    li {
        margin-bottom: 1px;
    }
    ul li, li a {
        width: 100%;
    }
    .show-menu {
        display: block;
    }
}

答案 1 :(得分:0)

找到我的立场答案:相对而非绝对

答案 2 :(得分:0)

你需要添加&#34;宽度:100%和#34;在文件css中。 代码:

ul {
        list-style-type:none;
        margin:0;
        padding:0;
        position: absolute;
        width: 100%;
}