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(http://i.stack.imgur.com/5c3zk.png);
}

Is there a way to do the same without span?

j08691
  • 197,815
  • 30
  • 248
  • 265
Artem Svirskyi
  • 6,915
  • 7
  • 30
  • 42

4 Answers4

3

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

text-indent:-5000px;
Tom Walters
  • 14,668
  • 6
  • 56
  • 73
2

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

<a href="#"><img src="http://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
  • 46,262
  • 29
  • 163
  • 256
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(http://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,496
  • 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
  • 18,396
  • 6
  • 32
  • 52