2013-03-06 32 views
1

我真的很喜歡用font: 0/0 a殺死圖片替換的文本,但據我所知,它並不是真的可以在任何地方工作。圖片替換字體大小:0

所以我們還需要

text-indent: -9999px; 
overflow: hidden; 

這個?

+0

有很多方法可以隱藏文本...'display:none'是最徹底的,但你也有'visibility:hidden','opacity:0','position:absolute;右:100%','身高:0;溢出:隱藏「等等。在某些情況下,甚至改變'z-index'的作品。所以這取決於你的情況和你的需求。 – 2013-03-06 12:38:04

+0

噢,以像素爲單位的文本縮進不是一個好主意,因爲在未來的某個時刻,用戶的屏幕將會大於9999像素。 '-100%'更好。 – 2013-03-06 12:39:13

回答

1

由於李斯特先生表示,還有很多,很多不同方式做形象更換。它們都有上下兩面。有一種技術唯一真正的缺點是它需要支持psuedo元素(所以IE8 +)。

  • 沒有額外的標記
  • 工程禁用
  • 作品與透明圖像
  • 不需要字體大小的調整
  • 不需要與定位

http://nicolasgallagher.com/css-image-replacement-with-pseudo-elements/搞亂圖像

.nir { 
    height:100px; /* height of replacement image */ 
    width:400px; /* width of replacement image */ 
    padding:0; 
    margin:0; 
    overflow:hidden; 
} 

.nir:before { 
    content:url(image.gif); 
    display:inline-block; 
    font-size:0; 
    line-height:0; 
} 
+0

當然,僞元素是好的。但有時候我不想用,而且真的沒有必要。 – 2013-03-06 13:21:43

1

更新

您可以用背景替換圖像,並使用填充移動實際的圖像了。這只是衆多解決方案之一。

http://jsfiddle.net/gj5F2/2/

的CSS

div 
{ 
    width: 550px; 
    height: 190px; 
    overflow: hidden; 
} 
.img2 
{ 
    background: url('http://www.google.com/logos/2013/ghana_independence_day_2013-1202005-hp.jpg') no-repeat left top; 
    padding-left: 550px; 
} 

HTML

<img class="img1" src="http://www.google.dk/images/srpr/logo4w.png" width="550" height="190" /> 

<div> 
    <img class="img2" src="http://www.google.dk/images/srpr/logo4w.png" width="550" height="190" /> 
</div> 
+0

謝謝,但我問的是圖像替換,而不是文本隱藏。 – 2013-03-06 13:20:48

+0

我已更新我的回答 – 2013-03-07 08:31:09