将CSS下拉菜单置于中心位置

时间:2014-01-28 13:10:11

标签: html css

这是我的CSS代码。我似乎无法让它居中。我尝试过使用display:inline-block和float:left。我也试过使用边距,但最终搞砸了下拉框。我已经尝试过编辑所有内容,但我无法做到正确。我对这种事情并不熟悉。我很感激帮助

body {
background-image:url('andromedagalaxy.jpg');
}
h1  {
text-align:center;
color: white;
}
p   {
text-align:center;
}
nav ul ul {
display: none;
}

nav ul li:hover > ul {
    display: block;
}
nav ul {
background: #0099CC; 
background: linear-gradient(top, #0099CC 0%, #bbbbbb 100%);  
background: -moz-linear-gradient(top, #0099CC 0%, #bbbbbb 100%); 
background: -webkit-linear-gradient(top, #0099CC 0%,#bbbbbb 100%); 
box-shadow: 0px 0px 9px rgba(0,0,0,0.15);
padding: 0 20px;
border-radius: 10px;  
list-style: none;
position: relative;
display: inline-block;
font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-serif;
text-align: center;
}
nav ul:after {
    content: ""; clear: both; display: block; text-align:center;
}
nav ul li {
float: left; 
}
nav ul li:hover {
    background: #000066;
    background: linear-gradient(top, #000066 0%, #000066 40%);
    background: -moz-linear-gradient(top, #000066 0%, #000066 40%);
    background: -webkit-linear-gradient(top, #000066 0%, #000066 40%);
}
    nav ul li:hover a {
        color: #fff;
    }

nav ul li a {
    display: block; padding: 15px 30px;
    color: #000099; text-decoration: none;
}
nav ul ul {
background: #000066; border-radius: 0px; padding: 0;
position: absolute; top: 100%;
}
nav ul ul li {
    float: none; 
    border-top: 1px solid #000066;
    border-bottom: 1px solid #000066;
    position: relative;
}
    nav ul ul li a {
        padding: 15px 20px;
        color: #fff;
    }   
        nav ul ul li a:hover {
            background: #006699;
        }
nav ul ul ul {
position: absolute; left: 100%; top:0;
}

5 个答案:

答案 0 :(得分:1)

因为你使用它:

nav ul {
 display: inline-block;
}

要将父母ul text-align放在中心位置,请尝试以下操作:

nav {
  text-align:center;
}

您可以查看此链接http://jsfiddle.net/eb2Zd/6/以查看演示文件。

答案 1 :(得分:0)

在向我们提供JsFiddle后,我有2个例子。

示例1:
您应该nav widthmargin:0 auto;就像在这里的例子一样。

nav { width:505px; margin: 0 auto}

<强> DEMO 1

示例2:

nav { text-align:center; }

<强> DEMO 2

答案 2 :(得分:0)

您可以在导航

上使用此功能
nav {
 text-align:center;
}

答案 3 :(得分:0)

body {
background-image:url('andromedagalaxy.jpg');
}
h1  {
text-align:center;
color: white;
}
p   {
text-align:center;
}
nav ul ul {
display: none;
}

nav ul li:hover > ul {
    display: block;
}
nav ul {
background: #0099CC; 
background: linear-gradient(top, #0099CC 0%);  
background: -moz-linear-gradient(top, #0099CC 0%); 
background: -webkit-linear-gradient(top, #0099CC 0%); 
box-shadow: 0px 0px 9px rgba(0,0,0,0.15);
padding: 0 20px;
border-radius: 10px;  
list-style: none;
position: relative;
display: inline-block;
font-family: "Avant Garde", Avantgarde, "Century Gothic", CenturyGothic, "AppleGothic", sans-   serif;
text-align: center;
   }
nav ul:after {
    content: ""; clear: both; display: block; text-align:center;
}
 nav ul li {
float: left; 
 }
nav ul li:hover {
    background: #000066;
    background: linear-gradient(top, #000066 0%);
    background: -moz-linear-gradient(top, #000066 0%;)
    background: -webkit-linear-gradient(top, #000066 0%);
}
    nav ul li:hover a {
        color: #fff;
    }

nav ul li a {
    display: block; padding: 15px 30px;
    color: #000099; text-decoration: none;
}
nav ul ul {
background: #000066; border-radius: 0px; padding: 0;
position: absolute; top: 100%;
}
nav ul ul li {
    float: none; 
    border-top: 1px solid #000066;
    border-bottom: 1px solid #000066;
    position: relative;
}
    nav ul ul li a {
        padding: 15px 20px;
        color: #fff;
    }   
        nav ul ul li a:hover {
            background: #006699;
        }
 nav ul ul ul {
position: absolute; left: 100%; top:0;
 }
 nav{
 text-align:center;
}
}




 http://jsfiddle.net/u8V9R/

答案 4 :(得分:-1)

在html上,放置一个必须居中的 CSS 文件中,放置此

.center {position: relative; 
 margin-left:auto; 
 margin-right:auto; 
 text-align:center;
 }