如何使文本在导航栏中居中对齐

时间:2014-03-01 05:21:20

标签: html css

我正在使用纯css测试下拉导航栏。文本对齐不起作用它只对齐下拉文本,左:50%和右:50%是的它使文本中心但导航栏不会覆盖我的页面的宽度。请不要使用溢出:隐藏它会使下拉列表不可见

JSFiddle

我想将文字设为this,但由于溢出我无法看到下拉列表

Css:

ul.dropdown, ul.dropdown li, ul.dropdown ul {
    list-style: none;
    margin: 0;
    padding: 0;
}
ul.dropdown {
    position: relative;
    z-index: 597;
    background-color: black;
}
ul.dropdown li {
    float: left;
    min-height: 1px;
    line-height: 1.3em;
    vertical-align: middle;
}
ul.dropdown li.hover, ul.dropdown li:hover {
    position: relative;
    z-index: 599;
    cursor: default;
}
ul.dropdown ul {
    visibility: hidden;
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 598;
    width: 100%;
}
ul.dropdown ul li {
    float: none;
}
ul.dropdown ul ul {
    top: 1px;
    left: 99%;
}
ul.dropdown li:hover > ul {
    visibility: visible;
}
/* -- Base drop-down styling -- */
 ul.dropdown {
    font: normal 16px"Square", Arial, Helvetica, sans-serif;
    text-transform: uppercase;
    width: 100%;
}
ul.dropdown li {
    padding: 7px 0;
    background-color: #000;
    color: #fff;
    line-height: normal;
}
ul.dropdown a:link, ul.dropdown a:visited {
    color: #fff;
    text-decoration: none;
}
ul.dropdown a:hover {
    color: #005CE6;
    text-decoration: none;
}
ul.dropdown a:active {
    color: #fff;
}
/* -- level mark -- */
 ul.dropdown ul {
    width: 170px;
    background-color: #333;
    color: #fff;
    font-size: 12px;
    text-transform: none;
    filter: alpha(opacity=90);
    -moz-opacity: .9;
    KhtmlOpacity: .9;
    opacity: .9;
}
ul.dropdown ul li {
    background-color: transparent;
    color: #000;
    filter: none;
}
ul.dropdown ul li.hover, ul.dropdown ul li:hover {
    background-color: transparent;
}
ul.dropdown ul a:link, ul.dropdown ul a:visited {
    color: #fff;
}
ul.dropdown ul a:hover {
    color: #fff;
    text-decoration: none;
}
ul.dropdown ul a:active {
    color: #fff;
}
/* -- Supporting class `dir` -- */
 ul.dropdown *.dir {
    padding-right: 12px;
    background-image: none;
    background-position: 100% 50%;
    background-repeat: no-repeat;
}
/* -- Base style extension -- */
 ul.dropdown li a {
    display: block;
    padding: 7px 14px;
}
/* -- Base style override -- */
 ul.dropdown li {
    padding: 0;
}
/* -- Base style reinitiate: post-override activities -- */
 ul.dropdown li.dir {
    padding: 7px 20px 7px 14px;
}
ul.dropdown ul li.dir {
    padding-right: 15px;
}
/* -- Custom -- */
 ul.dropdown ul a {
    padding: 4px 5px 4px 14px;
    width: 151px;
    /* Especially for IE */
}
ul.dropdown ul a:hover {
    background-color: #005CE6;
}
/* -- Drop-down open -- */
 ul.dropdown li:hover > a.dir {
    background-color: #2e2e2e;
    color: #005CE6;
}
ul.dropdown ul li:hover > a.dir {
    background-color: #76b900;
    color: #fff;
}
html {
    *overflow-x: hidden;
}
body {
    padding: 0;
}
body, html {
    height: 100%;
    padding: 0;
    margin: 0;
}
.wrapper {
    min-height: 100%;
    width: 100%
    overflow: hidden;
}
.container {
    padding: 50px;
}
.horizontal-centering {
    position: relative;
}
.horizontal-centering > * > * {
    float: left;
    width: 100%;
    position: relative;
    margin: 0;
    padding: 0;
}
.horizontal-centering > * > * > * {
    float: left;
    position: relative;
}

1 个答案:

答案 0 :(得分:1)

删除float: left;中的ul.dropdown li。在text-align: center;ul.dropdowndisplay: inline-block;添加ul.dropdown li

Fiddle Demo

ul.dropdown {
    position: relative;
    z-index: 597;
    background-color: black;
    text-align: center;
}
ul.dropdown li {
    display: inline-block;
    min-height: 1px;
    line-height: 1.3em;
    vertical-align: middle;
}