Semantically, which tag should I use for icons?

Asked

Viewed 1,002 times

7

I have a navigation menu with some icons using @font-face. Each item in this menu has:

  • the icon (with ::before);
  • text, explaining the function of that link.

.It turns out that when the page is loaded on a smaller device, I want the text to "disappear" and only the icon shown. I have the following structure:

<nav>
  <ul>
    <li><a class='ico-foo' href='#'>Visitar Foo</a></li>
    <li><a class='ico-bar' href='#'>Visitar Bar</a></li>
    <li><a class='ico-foobar' href='#'>Visitar FooBar</a></li>
  </ul>
</nav>

Normal, like most structures. It turns out, if I try to hide the contents of the tag <a> the icon in ::before will not appear. For example, I tried to use text-indent: -999px; and it didn’t work because the icon accompanies this rule and "some".

Well, the solution I found to hide the text was to put it inside a <span>, so when the page opens on a smaller device I give a display:none at that tag <span> and so is shown only the icons as I would like. The structure then:

<nav>
  <ul>
    <li><a class='ico-foo' href='#'><span>Visitar Foo</span></a></li>
    <li><a class='ico-bar' href='#'><span>Visitar Bar</span></a></li>
    <li><a class='ico-foobar' href='#'><span>Visitar FooBar</span></a></li>
  </ul>
</nav>

All right. My question is: Is that the best tag to do this? I’m worrying about little?

Some websites (mainly those created using bootstrap) use the tag for icons, but according to this question use is only to save characters.

Any suggestions for doing this in a better way?

  • 3

    Renang, my idea doesn’t work as well as I thought. The icon is also caught. I think the span is indeed the best option. Let’s see what others say.

  • 2

    And a high line-height with overflow Hidden, someone tried?

1 answer

7


The tag <span/> is in fact the most commonly used tag to deal with problems like what you refer to.

The HTML element <span> is a container Online Generic for phrasing content , which does not represent anything by nature.

It is an element without any added meaning associated with its use, unlike other elements like <em/> and <i/> they have a meaning behind their use, that is, they were created with a specific purpose, whereas the element <span/> was created to allow attributing formatting via CSS, referencing language, among others, to certain content without this content being influenced by its use.

In your particular case you want to manipulate the text via CSS, nothing better than to involve it with a <span> to abstract the impact of CSS from the rest of your Markup and focus the same solely on the text.

About the way you’re acting, the display:none; seems to me the best course to take.

Other methods require some care:

  • Negative indentation

    Can be used in some scenarios, for example text-indent:-999px but very careful because the visitor’s screen can be larger than the text-indent given and the text will appear in an inappropriate location.

  • Font size zero

    It can be used in some scenarios, but the crawlers (English), as is the case with Google find this incorrect and against SEO rules what will leave a flag on the negative page.

  • 1

    The suggestion to indent the text does not work, the icon is being inserted in the ::before of the tag. So, my solution is the most suitable? The use of the <span tag> ?

  • 1

    Yeah, I’d say what you’re doing is the best that can be done given what you want. You’re not elevating the complexity of Markup or taking meaning away from HTML elements. It’s all OK :)

  • If the way you have it works, changing it won’t get you anything but "wasting time". You need a <span> whether for the icon or to manipulate the visibility of the text. Whether you use it for one or the other is irrelevant as to the code in your question. Keep thinking you’re fine the way you are :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.