Home > Web Front-end > CSS Tutorial > How to Effectively Hide Text While Displaying an Image Using CSS?

How to Effectively Hide Text While Displaying an Image Using CSS?

Linda Hamilton
Release: 2024-11-19 19:20:02
Original
997 people have browsed it

How to Effectively Hide Text While Displaying an Image Using CSS?

Invisible Text Using CSS

Hiding text elements using CSS can be useful for various design purposes. One common scenario is replacing text with an image as a logo. This article addresses a specific problem: how to effectively remove the original text while displaying the image.

Solutions for Hiding Text

There are multiple approaches to make text invisible while preserving the element's dimensions for image placement.

Method 1: Text Indentation

One technique involves pushing the text off-screen using text-indent:

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 */
}
Copy after login

Method 2: Text Hiding

Another solution avoids the large invisible box created by negative indentation:

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

Both methods achieve the desired result by either pushing the text off-screen or hiding it within the element.

The above is the detailed content of How to Effectively Hide Text While Displaying 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