Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an opposite to display:none?

Tags:

css

People also ask

What can I use instead of display none?

To provide a simpler, better answer, visibilty: hidden; would be an option, though if you need the space that element was inhabiting, display: none ! important; would be your best option.

What is the opposite of display block?

Inline Elements: An inline element is the opposite of the block-level element.

What is diff between display none and visible false?

display:none means that the tag in question will not appear on the page at all (although you can still interact with it through the dom). There will be no space allocated for it between the other tags. visibility:hidden means that unlike display:none, the tag is not visible, but space is allocated for it on the page.

Is display none still rendered?

Yeah - GET requests for images, etc inside the display: none divs will still be carried out on page load, and it will all be parsed/loaded as part of the DOM, but I think it's not actually rendered until you change it to a visible display value, which then triggers a repaint/reflow of all content on the page.


display: none doesn’t have a literal opposite like visibility:hidden does.

The visibility property decides whether an element is visible or not. It therefore has two states (visible and hidden), which are opposite to each other.

The display property, however, decides what layout rules an element will follow. There are several different kinds of rules for how elements will lay themselves out in CSS, so there are several different values (block, inline, inline-block etc — see the documentation for these values here ).

display:none removes an element from the page layout entirely, as if it wasn’t there.

All other values for display cause the element to be a part of the page, so in a sense they’re all opposite to display:none.

But there isn’t one value that’s the direct converse of display:none - just like there's no one hair style that's the opposite of "bald".


A true opposite to display: none there is not (yet).

But display: unset is very close and works in most cases.

From MDN (Mozilla Developer Network):

The unset CSS keyword is the combination of the initial and inherit keywords. Like these two other CSS-wide keywords, it can be applied to any CSS property, including the CSS shorthand all. This keyword resets the property to its inherited value if it inherits from its parent or to its initial value if not. In other words, it behaves like the inherit keyword in the first case and like the initial keyword in the second case.

(source: https://developer.mozilla.org/docs/Web/CSS/unset)

Note also that display: revert is currently being developed. See MDN for details.


When changing element's display in Javascript, in many cases a suitable option to 'undo' the result of element.style.display = "none" is element.style.display = "". This removes the display declaration from the style attribute, reverting the actual value of display property to the value set in the stylesheet for the document (to the browser default if not redefined elsewhere). But the more reliable approach is to have a class in CSS like

.invisible { display: none; }

and adding/removing this class name to/from element.className.


I use display:block; It works for me


Like Paul explains there is no literal opposite of display: none in HTML as each element has a different default display and you can also change the display with a class or inline style etc.

However if you use something like jQuery, their show and hide functions behave as if there was an opposite of display none. When you hide, and then show an element again, it will display in exactly the same manner it did before it was hidden. They do this by storing the old value of the display property on hiding of the element so that when you show it again it will display in the same way it did before you hid it. https://github.com/jquery/jquery/blob/740e190223d19a114d5373758127285d14d6b71e/src/css.js#L180

This means that if you set a div for example to display inline, or inline-block and you hide it and then show it again, it will once again show as display inline or inline-block same as it was before

<div style="display:inline" >hello</div>
<div style="display:inline-block">hello2</div>
<div style="display:table-cell" >hello3</div>

script:

  $('a').click(function(){
        $('div').toggle();
    });

Notice that the display property of the div will remain constant even after it was hidden (display:none) and shown again.