Home > Web Front-end > CSS Tutorial > How Can I Replace HTML Text with an Image Using CSS?

How Can I Replace HTML Text with an Image Using CSS?

Susan Sarandon
Release: 2024-12-03 01:27:11
Original
625 people have browsed it

How Can I Replace HTML Text with an Image Using CSS?

Replacing HTML Text with an Image Using CSS

In HTML, it is common to use tags to create headings and display text within a web page. However, there are scenarios where you may want to replace the text with an image. This article addresses this challenge by exploring two effective CSS solutions to hide text and display an image instead.

Solution 1: Pushing Text Off-Screen

This method involves indenting the text a significant distance off the screen, effectively making it invisible to users. Simultaneously, a background image is applied to the tag to display the desired image.

h1 {
    text-indent: -9999px;                 /* sends the text off-screen */
    background-image: url(/the_img.png);  /* shows image */
    height: 100px;                        /* be sure to set height & width */
    width: 600px;
    white-space: nowrap;            /* because only the first line is indented */
}

h1 a {
    outline: none;  /* prevents dotted line when link is active */
}
Copy after login

Solution 2: Hiding Text with Overflow

This approach uses a combination of text indenting, white-space control, and overflow to hide the text.

h1 {
    background-image: url(/the_img.png);  /* shows image */
    height: 100px;                        /* be sure to set height & width */
    width:  600px;

    /* Hide the text. */
    text-indent: 100%;
    white-space: nowrap;
    overflow: hidden;
}
Copy after login

By implementing either of these solutions, you can effectively hide the text within the HTML tag and display the desired image instead.

The above is the detailed content of How Can I Replace HTML Text with an Image Using CSS?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template