"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Effectively Hide Text While Displaying an Image Using CSS?

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

Published on 2024-11-19
Browse:513

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 */
}

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;
}

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

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3