自适应导航栏将不会集中在DIV

时间:2014-12-04 09:47:25

标签: html css menu navigation

我已经对可能的解决方案进行了大量的搜索和测试,但无法将此响应式菜单放在中央位置。

  /**********HEADER ***************************/

.headerbar {
  display:inline-block;
   width:100%;
    background:#007700;
    margin-bottom:0px;
}
/******************/


/*********** NAVBACKER *********************/
.navback {
    display:block;
    height:32px;
    border:2px solid grey;
    background:grey;
    margin:auto;
}
/***************/



/* START OF NAV STYLES ****************************/
/*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: left;
    margin-right: 1px;

}

/*Style for menu links*/
li a {
    display:block;
    min-width:140px;
    height: 30px;
    text-align: center;
    line-height: 30px;
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    color: #fff;
    background: #003300;
    text-decoration: none;
    border:1px solid green;

}

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

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

/*Hover state for dropdown links*/
li:hover ul a:hover {
    background: #006600;
    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;
}

/*Style 'show menu' label button and hide it by default*/
.show-menu {
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
    text-decoration: none;
    color: #fff;
    background: #003300;
    text-align: center;
    padding: 10px 0;
    display: none;

}

/*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;
        height:12px;
    }
}
/* END OF NAV STYLES ****************

    <div class="headerbar">
    <h1>Logo and some text</h1>
</div>

<div class="navback">
<!-- START OF NAVIGATION MENU -->
    <label for="show-menu" class="show-menu">Show Menu</label>
    <input type="checkbox" id="show-menu" role="button">
        <ul id="menu">
        <li><a href="#">Home</a></li>
        <li>
            <a href="#">Latest ↓</a>
            <ul class="hidden">
                <li><a href="#">Pictures</a></li>
                <li><a href="#">Comments</a></li>
                <li><a href="#">Threads</a></li>
            </ul>
        </li>
        <li>
            <a href="#">Explore ↓</a>
            <ul class="hidden">
                <li><a href="#">Counties</a></li>
                <li><a href="#">Towns</a></li>
                <li><a href="#">Attractions</a></li>
                <li><a href='#'>Picture Tours</a></li> 
            </ul>
        </li>
        <li><a href="#">Upload</a></li>
            <li><a href="#">Login</a></li>
    </ul>

    </div>
<!-- END OF NAVIGATION MENU -->`enter code here` 

以下是我一直试图让它发挥作用的地方:http://jsfiddle.net/

每当我更改位置:绝对时,悬停时的菜单格式错误。我还尝试了保证金:0自动; 等,但我尝试过的任何内容都无法使用,我尝试删除浮动。任何帮助都非常感谢。 树

1 个答案:

答案 0 :(得分:0)

display: table;
margin: 0 auto;

在ul标签中,如下所示:

ul {
    list-style-type:none;
    margin: 0;
    padding:0;
    display: table;
    margin: 0 auto;
}

这是小提琴 - http://jsfiddle.net/p5oypc72/70/

相关问题