Use "left: -9999px" instead of "display: none"? But why?

11

I began to wonder about this subject a short time ago. I read in some article, which I can not remember what it is, that by "visibility issue" instead of using display: none or visibility: hidden should be used left: -9999px .

I saw something similar in this article and in , which seems to refer to this use due to "screen readers".

I do not quite understand the meaning of this, and I've even seen some library or some website making use of left: -9999px instead of display:none .

What is the sense of using left: -9999px instead of display:none , visibility:hidden , and the like?

    
asked by anonymous 21.11.2017 / 18:03

2 answers

6

Wallace WebAIM has an excellent article on just the techniques to make content visible only to screen readers. Even if you give TAB on their site you will see that a Button appears at the top to skip part of the site content etc. In this button he uses such a technique to pull it off the screen. The left:-10000px leaves the element in the page flow, but off the screen.

About WebAIM Words

  

Display:none and / or visibility: hidden;   These styles will hide text from all users. The text is removed from the visual flow of the page and ignored by screen readers. Do not use this CSS if you want the content to be read by a screen reader. But DO use it for content you do not want to read by screen readers .

" display:none; and / or visibility: hidden; These styles will hide the text of all users. The text is removed from the visual stream of the page and ignored by the screen readers. Do not use this CSS if you want the content to be read by a screen reader. But you use it for content you do not want to read by screen readers. "

Source: link

Now according to this article you can see that there is an inconsistency of how the screen readers understand display:none; Some readers ignore everything with this display:none , others depending on the context even with display the content will be read .

  

I had noticed recently that JAWS announces span elements hidden with display:none in anchor elements, but that the display: none was not announced by Window-Eyes.

" I recently noticed that JAWS advertises elements of span hidden with span in anchor elements, but that display: none was not advertised by Window-Eyes."

Source: link

span

  • In supported browsers, content is not displayed to any user.
  • Focusable content is not included in the reading order.
  • Not included in accessibility tree (except for IE)
  • In IE, the content has an accessible (if supported) invisible MSAA state.

Source: link

Bootstrap Approach: Bootstrap has a specific class to hide content on the screen, but continue to show to the screen readers. Display:none

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

link

TIP: Do not use the sr-only property of HTML

  

The hidden attribute should not be used to hide content just from one   presentation -if something is marked hidden, it is hidden from all   presentations, including, for instance, screen readers.

" The hidden attribute should not be used to hide content only from a presentation - if something is marked as hidden , it will be hidden in all presentations, including, for example, screen readers. "

Official W3C font : link

    
21.11.2017 / 19:18
3

So I think of properties like:

  • left: -9999px used to retreat from the screen (or the box-model of an element) one or more elements
  • text-indent: -9999px used to indent the screen text (or box-model of an element)

They are used for only a few specific cases, where people want so-called screen readers (accessibility) to work for the visually impaired, of course not always good service from the developer in this or simply the intention is to hide something for another reason, for example a logo that uses background-image instead of <img alt=""> , this in case of text-indent

However, the most likely use case of left: -9999px is that it is still accessible via TAB, however the "technique" requires position: absolute; , which makes the space where the element is "seem" to be free, example:

.container {
    width: 400px;
    height: 200px;
    border: 1px #c0c0c0 solid;
}
.hide-left {
   position: absolute;
   left: -9999px;
   top: -9999px;
}
<div class="container">
    <p>Hello, Wallace!</p>
    <div class="hide-left">
         foo bar foo bar foo bar foo bar<br>
         foo bar foo bar foo bar foo bar<br>
         foo bar foo bar foo bar foo bar<br>
         foo bar foo bar foo bar foo bar<br>
         foo bar foo bar foo bar foo bar<br>
    </div>
    <p>Bye, Wallace!</p>
</div>

Now note how different it was with visibility: hidden;

.container {
    width: 400px;
    height: 200px;
    border: 1px #c0c0c0 solid;
}
.hide-visibility {
   visibility: hidden;
}
<div class="container">
    <p>Hello, Wallace!</p>
    <div class="hide-visibility">
         foo bar foo bar foo bar foo bar<br>
         foo bar foo bar foo bar foo bar<br>
         foo bar foo bar foo bar foo bar<br>
         foo bar foo bar foo bar foo bar<br>
         foo bar foo bar foo bar foo bar<br>
    </div>
    <p>Bye, Wallace!</p>
</div>

display: none; will not be accessible and will not take up space either.

Concluding on usage

You can not assert a specific use for left: -9999px; , but the reason is really this, even though the element does not appear on the screen, because it will be out of bounds even if it is accessible, also note that if a% is parent , position: relative; and left of the element you are trying to hide will actually be relative to this top

An example of a "need" we might have would be a tooltip without JavaScript , ie a tooltip with pure CSS , like this:

  

Note: I personally think that using only parent or just top is good size, both are not necessary

.tooltip {
    text-decoration: underline;
    position: relative;
    display: inline-block;
}
.tooltip > .tooltip-inner {
     position: absolute;
     top: -9999px;
     width: 100px;
     background: rgba(0,0,0,0.7);
     color: #fff;
     padding: 5px;
     border-radius: 2px;
}
.tooltip:hover > .tooltip-inner {
    top: 100%;
}
<div>
Mussum Ipsum, cacilds vidis <span class="tooltip">litro <div class="tooltip-inner">Litro é a melhor coisa pra matar a saudade desse grande humorista</div></span> abertis. Pra lá, depois divoltis porris, paradis. Delegadis gente finis, bibendum egestas augue arcu ut est. Suco de cevadiss deixa as pessoas mais interessantis. Viva <span class="tooltip">Forevis <div class="tooltip-inner">É o tempo que esse humorista será lembrado</div></span> aptent taciti sociosqu ad litora torquent.
</div>

SEO vs. Hidden Elements

There are different techniques to hide elements, some believe that Google punishes because of this, which may be very likely, I have already seen the use of these techniques, I have seen sites that have been punished, can not say whether Google and Bing will punish a site that uses this, I personally believe that the use will only be punished depending on the form that was applied, I believe that if the page in general has the main content, visible and organized, then an element that needs to be hidden is not motive for this.

There will be people who will affirm with all the letters that any use will have punishment, but the algorithm of google and rules always change, the indexer is very intelligent, it even can detect dynamic pages that are partially generated via some process by JavaScript .

    
29.11.2017 / 20:25