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

Asked

Viewed 457 times

13

I started to wonder about that a little while ago. I read in some article, which I don’t remember what it is, that for "visibility" sake instead of using display: none or visibility: hidden should be used a left: -9999px.

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

I didn’t really understand the meaning of this and, in fact, I’ve seen some library or some site making a use of left: -9999px instead of display:none.

What’s the point of someone using left: -9999px instead of display:none, visibility:hidden and the like?

  • 1

    Those articles are kind of old. But, from what I understand, this technique would only be to keep the content accessible to screen readers (Readers screens), since they can not access hidden elements on the page.

2 answers

8


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

About the Display:none Words of the Webaim

visibility: hidden; and/or display:none; These Styles will Hide text from all users. The text is Removed from the visual flow of the page and is 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 don’t want to read by screen Readers.

"visibility: hidden; and/or display:none; These styles will hide text from all users. Text is removed from the page’s visual flow and ignored by screen readers. Don’t use this CSS if you want the content to be read by a screen reader. But you use it for content you don’t want to read by screen readers."

Source: https://webaim.org/techniques/css/invisiblecontent/

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

I had recently noticed that JAWS Announces span Elements Hidden with display: none in Anchor Elements, but that the span was not announced by Window-Eyes.

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

Source: http://juicystudio.com/article/screen-readers-display-none.php

Display:none

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

Source: https://developer.paciellogroup.com/blog/2012/05/html5-accessibility-chops-hidden-and-aria-hidden/

Approach of Bootstrap: Bootstrap has a specific class to hide content on the screen, but keep showing it to screen readers. sr-only

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

https://getbootstrap.com/docs/4.0/getting-started/accessibility/#visually-Hidden-content


TIP: Don’t use the property hidden of HTML

The hidden attribute must 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 attribute hidden 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 source W3C: http://w3c.github.io/html/editing.html#the-Hidden-attribute

3

So I guess properties like:

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

They are used for only in some specific cases, where people wish the so-called screen readers (accessibility) work for the visually impaired, of course there is not always a good service done by 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 the 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 the position: absolute;, what 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 look how different you turned out 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>

Already the display: none; will not be accessible and will not occupy the space.


Concluding on the use

It is not possible to affirm a specific use for left: -9999px;, but the reason will really be this, although the element does not appear on the screen, because it will be outside the limit yet it will be accessible, also note that if an element parent be with position: relative;, the left and top of the element you are trying to hide will actually be related to this parent

An example of "need" we could have would be tooltip without Javascript, or be a tooltype with CSS pure, thus:

Note: Personally I think only use left or just top be good size, both is not something needed

.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’ve seen the use of these techniques, I’ve seen websites that have been punished, can’t say if Google and Bing will punish a site that uses this, i personally believe that the use will only be punished depending on the form it has been applied, I believe that if the page in general has the main content, visible and organized, then an element that needs to be hidden will not be reason for this.

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

Browser other questions tagged

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