【问题标题】:centering a menu居中菜单
【发布时间】:2012-12-15 22:27:40
【问题描述】:

我不明白为什么我的菜单没有居中。我尝试了从内联元素到边距的所有方法:0 auto;对齐 =“中心”,我无法让菜单居中。你可以在这里看到它http://jeremyspence.net78.net 你必须一直向下滚动才能看到它,它只有在它经过主菜单时才会出现。这是一些css

.scrollmenu {
    display:none;
    position: fixed;
    top: 0;
    text-align:center;
    margin: 0px auto;
    width: 1020px;
    z-index: 10000;
    padding:0;
}
.scrollmenu li{
    width: 200px;
    height: 75px;
    overflow: hidden;
    position: relative;
    float:left;
    background: #fff;
    -webkit-box-shadow: 1px 1px 2px rgba(0,0,0,0.2);
    -moz-box-shadow: 1px 1px 2px rgba(0,0,0,0.2);
    box-shadow: 1px 1px 2px rgba(0,0,0,0.2);
    margin-right: 4px;
    -webkit-transition: all 300ms linear;
    -moz-transition: all 300ms linear;
    -o-transition: all 300ms linear;
    -ms-transition: all 300ms linear;
    transition: all 300ms linear;
}
.scrollmenu li:last-child{
    margin-right: 0px;
}
.scrollmenu li a{
    text-align: left;
    width: 100%;
    height: 100%;
    display: block;
    color: #000;
    position: relative;
}
.scroll-icon{
    font-family: 'Symbols';
    font-size: 60px;
    color: #333;
    text-shadow: 0px 0px 1px #333;
    line-height: 80px;
    position: absolute;
    width: 100%;
    height: 50%;
    left: 0px;
    top: 0px;
    text-align: center;
}
.scroll-home{
    font-size: 30px;
    opacity: 0.8;
    text-align: center;
    position: absolute;
    left: 0px;
    width: 100%;
    height: 50%;
    top: 30%;
}
.scrollmenu li:nth-child(2):hover{
    background-color: #CEFECE;
}
.scrollmenu li:nth-child(3):hover{
    background-color: #CEFEFE;
}
.scrollmenu li:nth-child(4):hover{
    background-color: #CECEFE;
}
.scrollmenu li:last-child:hover{
    background-color: #FECEFE;
}

【问题讨论】:

标签: css list centering


【解决方案1】:

看起来您正在使 div 居中? <div> 默认是块元素。它们通过使用边距居中。你已经接近了——你需要把左边距和右边距都设为自动。 margin-left:auto;margin-right:auto;。这将使它在其父元素内居中,该元素需要为 100% 宽度(默认情况下,块元素将扩展到父元素的最大宽度)。如果它是内联元素,您可以在其父元素上使用 text-align:center;(父元素仍需要 100% 宽度),这样就可以了。

【讨论】:

    【解决方案2】:

    在链接的 HTML 中,您似乎需要在ul 菜单上方的div 中设置position:fixed。看起来您同时设置了position:fixed 并试图居中。将父div定位在固定位置,然后它的子divul应该可以通过margin-left: auto; margin-right: auto;居中。

    【讨论】:

      【解决方案3】:

      您是否尝试过为内容 div 指定宽度?

      .content {
      position: relative;
      width: 1024px;
      margin: 0 auto;
      }
      

      这似乎解决了 Chrome 中的问题。

      通过设置position: fixed; 而不为封闭容器设置大小,菜单使用容器的窗口边框,然后所有项目都向左浮动......好吧,它们都在左边。

      【讨论】:

        【解决方案4】:

        只需将其添加到您的 <ul class="scrollmenu"></ul>

        left: 50%;
        margin-left: -510px;
        

        或:

        left: 0;
        right: 0;
        

        【讨论】:

          猜你喜欢
          • 2014-03-08
          • 1970-01-01
          • 1970-01-01
          • 2021-09-21
          • 2014-10-26
          • 2012-04-11
          • 1970-01-01
          • 1970-01-01
          • 2017-04-22
          相关资源
          最近更新 更多