我有一個導航欄,我想要得到每個<li>
元素的寬度,然後將固定寬度放在它上面。爲每個LI元素設置一個固定寬度
原因:IE 7需要父級(此例中爲<li>
元素)爲固定寬度,子元素<div>
元素使用margin: 0 auto
居中。由於其寬度不是固定的,因此auto
正在將<li>
標籤推到100%的寬度;
HTML
<div class="subnav">
<ul>
<li class="apparel">
<a href="apparel.html">
<div class="image"></div>
<div class="title">APPAREL</div>
</a>
</li>
<li class="pads">
<a href="pads-girdles.html">
<div class="image"></div>
<div class="title">PADS/GIRDLES</div>
</a>
</li>
</ul>
</div>
CSS的時間
.subnav { margin:20px auto; }
.subnav ul { float:left; }
.subnav li { display: block;
cursor:pointer;
position:relative;
float:left;
padding:5px 8px 0;
border-top:1px solid #FFF;
border-left:1px solid #FFF;
border-right:1px solid #FFF; }
.subnav li.last-link { margin-right:0; }
.subnav li div.title { font-family: 'Cuprum', sans-serif;
font-size:15px; font-weight:500; text-align:center; margin-top:5px; color:#214592; }
.subnav li div.image { background-repeat:no-repeat; overflow:hidden; margin:0 auto; background-position:center center; height:60px; }
.subnav li.apparel div.image { background-image:url(../images/football_apparel_icon.jpg); width:58px; }
.subnav li.pads div.image { background-image:url(../images/football_pads_icon.jpg); width:50px; }
.subnav li.gloves div.image { background-image:url(../images/football_gloves_icon.jpg); width:33px; }
.subnav li.accessories div.image { background-image:url(../images/football_pads_icon.jpg); width:50px; }
.subnav li.protective div.image { background-image:url(../images/football_protective_icon.jpg); width:64px; }
.subnav li.cleats div.image { background-image:url(../images/football_cleats_icon.jpg); width:64px; }
.subnav li.accessories div.image { background-image:url(../images/football_accessories_icon.jpg); width:47px; }
.subnav li.footballs div.image { background-image:url(../images/football_football_icon.jpg); width:55px; }
.subnav li.sport div.image { background-image:url(../images/football_sport-bag_icon.jpg); width:58px; }
.subnav li.hydration div.image { background-image:url(../images/football_hydration_icon.jpg); width:22px; }
.subnav li.performance div.image { background-image:url(../images/football_performance_icon.jpg); width:88px; }
我想你可能會問錯誤的問題。什麼是你的相關CSS?我敢肯定,你不需要javascript就可以將div集中到中心,即使在IE6/7中 - 你只需要另一種技術。 –
哪個部分你想要居中? – Sparkup
我不想通過每個li標籤,併爲IE7放置固定寬度。 – Buildingbrick