简体   繁体   中英

How to make the text align to center in nav bar

I'm testing dropdown nav bar with pure css. Text-align doesn't work it only align the dropdown text, left: 50% and right: 50% yes it makes the text center but the navbar won't cover the width of my page. and please don't use overflow: hidden it will make the dropdown not visible

JSFiddle

I want to make the text like this but because of the overflow I can't see the dropdown

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

remove float: left; in your ul.dropdown li . Add text-align: center; in your ul.dropdown and display: inline-block; in your 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM