html - Horizontally center list items that have different background image widths -


i having trouble centering list items horizontally within container div. have tried use display:inline on li , li did not help. tried text-align center on ul , li did not give me results. solution there if remove 2 arrow li's html breaks , looks second image posted here instead of automatically centering social media icons. have borders around right troubleshooting purposes.

ideally, if remove arrow or facebook list item, others should continue center within container. bit confused on how achieve want after trying float , display:inline-block , inline method , having little results. able achieve want plain text using display:inline, these need background images , arrows have different width social media icons. (could turned sprite suppose instead of individual ones)

html:

 <div id="socialbox">     <ul id="social">       <li id="leftarrow"><a></a></li>       <li id="facebook"><a href=""></a></li>       <li id="twitter"><a href=""></a></li>       <li id="youtube"><a href=""></a></li>       <li id="rightarrow"><a></a></li>     </ul> </div> 

css:

#socialbox {width:400px; border:1px solid black; margin:0 auto; margin-top:0px; } #social{overflow:auto; border:1px solid blue;} #social ul {list-style:none; margin:0 auto;} #social li{float:left; margin-right:15px;border:1px solid red;} #social li, #social a{height:53px;display:block;}  #leftarrow{width:30px; border:0px solid black; background:url('../img/leftarrow.png')no-repeat 0 0px;} #leftarrow a:hover{background: url('../img/leftarrow-hover.png')no-repeat 0 0;}  #rightarrow{left:0px;width:30px; border:0px solid black;background:url('../img/rightarrow.png')no-repeat 0 0px;} #rightarrow a:hover{background: url('../img/rightarrow-hover.png')no-repeat 0 0;}  #facebook{width:62px; border:0px solid black; background:url('../img/facebook.png') 0 0} #facebook a:hover{background: url('../img/facebook-on.png') 0 0;} 

the image below soluton looks like, actual 2 pixels on left side little, isntead of centered.

there 2 issues, can see

  • you have selector #social ul, should #social or ul. otherwise doesn't apply <ul id="social">
  • ul block level element, extends whole width - can seen blue border - , margin: auto has no effect. work, can set width, encloses children , adjust padding accordingly
#social {     width: 240px;     list-style:none;     margin:0 auto;     padding-left: 20px; } 

jsfiddle testing.

as alternative, can increase margin of leftmost child, e.g. facebook.

update:

if want avoid setting width, can use @ajgiv's suggestion , add text-align: center outer div socialbox. then, must make social inline element setting display: inline-block

#socialbox {     text-align: center; }  #social {     display: inline-block;     padding-left: 15px; } 

see jsfiddle


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -