html - Text not aligning to the center in the li ul -


in following code, through trying understand drop down menu. if see output, can notice li texts first second , fourth not equally horizontally aligned equal spaces between each other.for instance there more horizontal space on right side of fourth. whats best possible way align text in middle (horizontally) without manually giving values of margin, padding etc. there should way using text-align:center or margin:auto auto can align text in center automatically irrespective of length of text or font size.

    ul{      width:350px;      height:50px;      padding-left:0;      margin:0;      background:#cccccc;      }      ul > li{      list-style: none;      display: inline;      font-size: 24px;      float: left;      width: 106.66px;      margin-right: auto;      text-align: center;      margin-left: auto;      }      ul > li > ul{      margin:10px 0px;      padding-left:0;      width:80px;      height:40px;      visibility: hidden;      }      ul > li > ul > li{      display:block;      list-style-type:none;      padding-left:10px;      }      ul > li:hover ul{      visibility:visible;      }
<body>   <ul>  <li>first</li>  <li>  second  <ul>  <li>third</li>  </ul>  </li>  <li>fourth</li>  </ul>  </body>

it because of hidden ul. don't use vissibility:hidden that, make ur hidden, right still take space make second li bigger normal should - inspect element , see - , fourth li in wrong place.

prevent this, can use display:none position:absolute demo below; display:none make third ul "disappear", make ul right, , position:absolute, prevent fourth li run around (try delete attribute position:absolute , can see.

ul{   width:350px;   height:50px;   padding-left:0;   margin:0;   background:#cccccc; } ul > li{   list-style:none;   display:inline;   padding:10px 20px;   font-size:24px;   float:left;   position: relative; } ul > li > ul{   margin:10px 0px;   padding-left:0;   width:80px;   height:40px;   display:none;   position:absolute; } ul > li > ul > li{   display:block;   list-style-type:none;   padding-left:10px; } ul > li:hover ul{   display:block } 

demo


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo