Use CSS to replace text with images

Source: Internet
Author: User
Document directory
  • Fahrner image replacement (FIR)
  • Phark Method
  • Gilder/Levin Method
Abstract: Both browser and search engine are the best ways to display page content. However, due to font limitations, the display of pure text is gradually unable to meet the requirements of beautiful designers.

As a result, CSS is used to replace text with images. This method not only achieves various effects on the page, but also meets the needs of search engine optimization. Therefore, they are favored by web designers.

Whether it is for viewers or for search engines, text is the best way to display the page content. However, due to the limitations of fonts and other reasons, the display of pure text is gradually unable to meet the requirements of beauty designers.

As a result, CSS is used to replace text with images. This method not only achieves various effects on the page, but also meets the needs of search engine optimization. Therefore, this article introduces several common text and text Replacement Technologies.

  • Fahrner image replacement (FIR)
  • Phark Method
  • Gilder/Levin method (recommended)
Fahrner image replacement (FIR)

This is the earliest text-and-text Replacement Technology proposed by Todd fahrner. It is easy to understand:

HTML code:
HTML:

 
 
  1. <span>Hello World</span>
  2.  

CSS code
CSS:

 
 
  1.  
  2. <style type="text/css">
  3. h2 {
  4. background:url(hello_world.gif) no-repeat;
  5. width: 150px;
  6. height: 35px;
  7. }
  8. span {
  9. display: none;
  10. }
  11. </style>
  12.  

The code is very clear: first apply the image to the H2 background, and then hide the span tag. However, there is a problem with this method, that is, when the image cannot be displayed, there will be no content in this area. At the same time, the hidden content using the display: None method will be ignored by many mainstream screen readers, resulting in availability problems. Therefore, we should avoid using it whenever possible.


Phark Method

This technique was proposed by Mike Rundle, with the advantage that no additional labels are needed:

HTML code:
HTML:

 
 
  1.  
  2. Hello World
  3.  

CSS code
CSS:

 
 
  1.  
  2. <style type="text/css">
  3. h2 {
  4. text-indent: -5000px;
  5. background:url(hello_world.gif) no-repeat;
  6. width: 150px;
  7. height:35px;
  8. }
  9. </style>
  10.  

The Code is also very simple. Text is indented to hide the text. However, when the image cannot be displayed, the FIR problem still exists.

Gilder/Levin Method

This technology was proposed by Tom Gilder and Levin Alexander. It may be the most complete text and text Replacement Technology:
HTML code: HTML:

 
 
  1.  
  2. <span></span>Hello World
  3.  

CSS code
CSS:

 
 
  1.  
  2. <style type="text/css">
  3. h2 {
  4. width: 150px;
  5. height: 35px;
  6. position: relative;
  7. }
  8. h2 span {
  9. background: url(hello_world.gif) no-repeat;
  10. position: absolute;
  11. width: 100%;
  12. height: 100%;
  13. }
  14. </style>
  15.  

First, set the position of H2 to relative, so that the element positioning in H2 takes H2 as a reference, and then absolutely positions the span element to fill the entire H2 region, at the same time, the background image is applied to the span label. The principle of this method is to overwrite the span label on the text content. Once the background image in the span cannot be displayed, the lower text content is displayed, normal use is not affected. However, this method also has a defect, that is, the background image cannot be transparent, otherwise the following text will be revealed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.