2012-12-03 37 views
-2

我想橫向居中在一個標籤內。問題是我無法設置跨度的高度和寬度。我的標記如下:水平中心<span>裏面<a>

<div class="footer"> 
    <a href="#"> 
      <span>Show/Hide</span> 
    </a> 
</div> 

而CSS:

.footer {position:absolute; bottom:0; width:100%;} 
.footer a {display:block; background:orange; text-align:center;} 
.footer a span {background:#FFF; font-size:12px; width:100px;} 

我用盡了一切,但覺得我需要某種形式的浮動的地方,但不知道。非常感謝幫助! :-)

+3

以我爲中心http://jsfiddle.net/j08691/tqwRp/ – j08691

回答

1

你不能設置display: inline元件的尺寸。

您可以同時擁有一個和跨度有display: block,如果你要明確設置寬度:

.footer {position:absolute; bottom:0; width:100%;} 
.footer a {display:block; background:orange;} /* no need to center text, here */ 
.footer a span { 
    display: block; /* give it a box layout */ 
    margin: 0px auto; /* center it horizontally */ 
    width:100px; /* give it explicit absolute size */ 
    text-align: center; /* you might center the content here, though */ 
    background:#FFF; font-size:12px;} 

或只使用text-align: center因爲你已經這樣做:

.footer {position:absolute; bottom:0; width:100%;} 
.footer a {display:block; background:orange; text-align:center;} 
.footer a span {background:#FFF; font-size:12px; 
    padding: 0px 30px;} /* if you only want some padding, and not a fixed width 
+0

非常感謝!這解決了我的問題。 – Eric

-4

你可以做

<center> </center> 

所以這樣,

<div class="footer"> 
<center> 
<a href="#"> 
     <span>Show/Hide</span> 
</a> 
</center> 
</div> 
+9

'

'元素在克林頓政府周圍棄用。 – j08691

+0

此外,'a'標籤被設置爲'display:block',這意味着它將是100%的寬度,除非另有規定。 – Jasper

0

請嘗試以下操作: -

.footer a span {background:#FFF;字體大小:12像素;寬度:50%;顯示:塊; margin:0 auto;}

'內部跨度'需要寬度小於'外部'div(或頁腳)以及餘量:0設置爲自動對齊。