2

Im trying to hide text (that I use for screenreaders) in an a tag and instead use a background icon.

HTML

<a href="#"><span>Search</span></a>

CSS

span {
    display: none;
}
a {
    display: inline-block;
    width: 17px;
    height: 16px;
    background: url(https://i.stack.imgur.com/5c3zk.png);
}

Is there a way to do the same without span?

j08691
  • 204,283
  • 31
  • 260
  • 272
Artem Svirskyi
  • 7,305
  • 7
  • 31
  • 43

4 Answers4

3

You can indent the text so it's completely off the screen:

text-indent:-5000px;
Tom Walters
  • 15,366
  • 7
  • 57
  • 74
2

I don't have a screenreader software but if I guess right this should work:

<a href="#"><img src="https://i.stack.imgur.com/5c3zk.png" alt="Search" /></a>

With this css rule:

a img {
    border:0;
}

Basically the alt attribute is the alternative text, which should be used in case of a screen reader.

rekire
  • 47,260
  • 30
  • 167
  • 264
2

You should check: Hide text using css

But it goes like this:

HTML:

<a href="#">Search</a>

CSS:

a {
    text-indent: -9999px;
    white-space: nowrap;  
    outline: none;

    display: inline-block;
    width: 17px;
    height: 16px;
    background: url(https://i.stack.imgur.com/5c3zk.png);
}

Here is the jsFiddle (i added background-color:red; because the image wasn't loading.)

Hope it helps.
Cheers.

Community
  • 1
  • 1
Lucas Lazaro
  • 1,516
  • 9
  • 13
1

html

<a class="my-title" href="#">Search</a>

css

a.my-title { text-indent:-9999px }

or

html

<a class="my-title" title="Search" href="#"></a>

css

a.my-title { display:block;width:123px; all-your-styling:...; }

Screen reader will catch the title attribute from your anchor.

Milche Patern
  • 19,632
  • 6
  • 35
  • 52