2017-04-24 99 views
0

我有以下菜单。我想将所有菜单标题对齐到页面中心。我尝试了对齐文本并且将内容与运气对齐。 我甚至把菜单里面的td与中心对齐,它不工作。 有帮助吗?将菜单对齐到页面中央

<ul id="menu" style="width:200%; text-align:center; margin:0 auto;"> 
        <li> 
         <a href=" #"> 
          Home 
         </a> 
        </li> 
        <li> 
         <a href="#">About &#65516;</a> 
         <ul class="hidden"> 
          <li><a href="#">Who We Are</a></li> 
          <li><a href="#">What We Do</a></li> 
         </ul> 
        </li> 
        <li> 
         <a href="#">Portfolio &#65516;</a> 
         <ul class="hidden"> 
          <li><a href="#">Photography</a></li> 
          <li><a href="#">Web &amp; User Interface Design</a></li> 
          <li><a href="#">Illustration</a></li> 
         </ul> 
        </li> 
        <li><a href="#">News</a></li> 
        <li><a href="#">Contact</a></li> 
       </ul> 

这里是CSS:

ul { 
    background-repeat: repeat-x; 
     list-style-type: none; 
     margin: 0; 
     padding: 0; 
     position: absolute; 

    } 

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

    } 

     /*Style for menu links*/ 
     li a { 
     background-image:url("../image_menu/bg.jpg"); 
     background-repeat: repeat-x; 
      display: block; 
      min-width: 140px; 
      width:100%; 
      height: 85px; 
      text-align: center; 
      line-height: 50px; 
      font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
      color: #fff; 

      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; 
    } 

    /*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: #19c589; 
     text-align: center; 
     padding: 10px 0; 
     display: none; 
       background:url("../image_menu/bg.png"); 
    background-repeat: repeat-x; 
    } 

    /*Hide checkbox*/ 
    input[type=checkbox] { 
     display: none; 
     -webkit-appearance: 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 { 
        background:url("../image_menu/bg.jpg"); 
    background-repeat: repeat-x; 
      display: block; 
     } 
    } 

感谢所有帮助

+0

对齐垂直或水平? – Sam

+0

对齐水平是想我正在努力与 –

+0

试试这个http://stackoverflow.com/questions/1708054/center-ul-li-into-div – Benera

回答

0

如果你想在导航菜单栏中的中心。您只需要将#menu更改为width:100%而不是width:200%,然后在li上删除float:left;。这是一个JSFiddle。希望这可以帮助。

https://jsfiddle.net/h1v85zuf/

+0

是的,它做到了。谢谢。我花了2小时在这个 –

+0

Np。很高兴我能帮上忙。 – Jorden1337

0

更改为ul至100%的在线width,清除浮动的菜单项,并添加margin: 0机体:

Additon:你可以切换段,以“整页“模式来查看解决方案。

body { 
 
background: #333; 
 
margin: 0; 
 
} 
 
ul { 
 
    background-repeat: repeat-x; 
 
    list-style-type: none; 
 
    margin: 0; 
 
    padding: 0; 
 
    position: absolute; 
 
} 
 

 

 
/*Create a horizontal list with spacing*/ 
 

 
li { 
 
    display: inline-block; 
 
    margin-right: 0px; 
 
} 
 

 

 
/*Style for menu links*/ 
 

 
li a { 
 
    background-image: url("../image_menu/bg.jpg"); 
 
    background-repeat: repeat-x; 
 
    display: block; 
 
    min-width: 140px; 
 
    width: 100%; 
 
    height: 85px; 
 
    text-align: center; 
 
    line-height: 50px; 
 
    font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; 
 
    color: #fff; 
 
    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; 
 
} 
 

 

 
/*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: #19c589; 
 
    text-align: center; 
 
    padding: 10px 0; 
 
    display: none; 
 
    background: url("../image_menu/bg.png"); 
 
    background-repeat: repeat-x; 
 
} 
 

 

 
/*Hide checkbox*/ 
 

 
input[type=checkbox] { 
 
    display: none; 
 
    -webkit-appearance: 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 { 
 
    background: url("../image_menu/bg.jpg"); 
 
    background-repeat: repeat-x; 
 
    display: block; 
 
    } 
 
}
<ul id="menu" style="width:100%; text-align:center; margin:0 auto;"> 
 
    <li> 
 
    <a href=" #"> 
 
          Home 
 
         </a> 
 
    </li> 
 
    <li> 
 
    <a href="#">About &#65516;</a> 
 
    <ul class="hidden"> 
 
     <li><a href="#">Who We Are</a></li> 
 
     <li><a href="#">What We Do</a></li> 
 
    </ul> 
 
    </li> 
 
    <li> 
 
    <a href="#">Portfolio &#65516;</a> 
 
    <ul class="hidden"> 
 
     <li><a href="#">Photography</a></li> 
 
     <li><a href="#">Web &amp; User Interface Design</a></li> 
 
     <li><a href="#">Illustration</a></li> 
 
    </ul> 
 
    </li> 
 
    <li><a href="#">News</a></li> 
 
    <li><a href="#">Contact</a></li> 
 
</ul>