262

The opposite of visibility: hidden is visibility: visible. Similarly, is there any opposite for display: none?

Many people become confused figuring out how to show an element when it has display: none, since it's not as clear as using the visibility property.

I could just use visibility: hidden instead of display: none, but it does not give the same effect, so I am not going with it.

Mohammad Areeb Siddiqui
  • 9,795
  • 14
  • 71
  • 113
  • 1
    well, you could have asked "how do you undo the effect of display:none" - then, Ilya's method would be a spot-on answer. In essence, you asked for some display: ?something?, such that that ?something? undoes the effect of display: none (that, I would call opposite). Sure, that ?something? does not exist. So no opposite. Right, no point arguing that, that is not in dispute. But you CAN undo the effect, if you use Ilya's method. So, in a higher sense, it's an opposite. It's just that there is no "one word" opposite. – mathheadinclouds Feb 29 '20 at 15:04
  • @mathheadinclouds thats what Paul's answer tell us. There is no opposite, they all are the opposite. I didn't ask for a way to undo the effect, I asked for a value that the display rule can hold like the visibility rule does. – Mohammad Areeb Siddiqui Mar 07 '20 at 21:08

18 Answers18

228

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".

Paul D. Waite
  • 96,640
  • 56
  • 199
  • 270
  • 2
    I notice you mentioned `display: initial` in the deleted self-answer - for browsers implementing CSS2.1 it's synonymous with `display: inline`. It doesn't reset `display` to the browser default for a given element - that's not what "initial value" means. – BoltClock Jul 14 '13 at 07:04
  • If you're bald by choice, when you stop shaving, your hair will grow back curly and black if that's how it was before you shaved. ;-) I agree with [this comment](https://news.ycombinator.com/item?id=25894541), that '`display` is way too overloaded' and CSS should have given us separate properties for flow (block or inline), internal layout (flex, grid, etc) and visibility (visible, invisible, none/hidden). While [separate flow and internal layout is now a thing](https://hacks.mozilla.org/2019/10/the-two-value-syntax-of-the-css-display-property/), overriding `display: none` is still a pain. – Kal Apr 15 '21 at 06:08
112

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.

Community
  • 1
  • 1
  • What about `display: initial`? – Flimm Jun 02 '20 at 13:29
  • @Flimm No, `display: initial` is equivalent to reverting back to `display: inline` which you don't want if you previously had an element with default `display: block`. – trusktr Feb 02 '21 at 20:13
43

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.

Ilya Streltsyn
  • 13,076
  • 2
  • 37
  • 57
  • 2
    If you want to override `.element { display: none }` (defined in CSS lib for example) with `.element { display: '' !important }` it won't work. You have to use `.element { display: unset !important }` – tanguy_k Feb 28 '18 at 00:07
  • 4
    I told about "undoing" `display:none` in JavaScript only. Of course, the empty string won't work in CSS, since it's the invalid value. Hovewer, `display: unset` you suggest won't restore, e.g., the default `display:block` for a `
    ` or the default `display:table-row` for a ``, it effectively turns everything into `display:inline` (just like `display:initial`). To restore the browser default value for the element, there is `display:revert`, but it's not well supported (https://caniuse.com/#feat=css-revert-value).
    – Ilya Streltsyn Feb 28 '18 at 08:08
6

Here's an answer from the future… some 8 years after you asked the question. While there's still no opposite value for display: none, read on… There's something even better.

The display property is so overloaded it's not funny. It has at least three different functions. It controls the:

  • outer display type (how the element participates in the parent flow layout, e.g. block, inline)
  • inner display type (the layout of child elements, e.g. flex, grid)
  • display box (whether the element displays at all, e.g. contents, none).

This has been the reality for so long, we've learnt to live with it, but some long-overdue improvements are (hopefully!) coming our way.

Firefox now supports two-value syntax (or multi-keyword values) for the display property which separates outer and inner display types. For example, block now becomes block flow, and flex becomes block flex. It doesn't solve the problem of none, but the explicit separation of concerns is a step in the right direction I think.

Chromium (85+), meanwhile, has given us the content-visibility property, and announced it with some fanfare. It aims to solve a different problem—speeding up page load times by not rendering an element (and its child layouts) until it approaches the viewport and really needs to be seen, while still being accessible for 'Find' searches, etc. It does this automatically just by giving it the value auto. This is exciting news in itself, but look at what else it does…

The content-visibility: hidden property gives you all of the same benefits of unrendered content and cached rendering state as content-visibility: auto does off-screen. However, unlike with auto, it does not automatically start to render on-screen.

This gives you more control, allowing you to hide an element's contents and later unhide them quickly.

Compare it to other common ways of hiding element's contents:

  • display: none: hides the element and destroys its rendering state. This means unhiding the element is as expensive as rendering a new element with the same contents.
  • visibility: hidden: hides the element and keeps its rendering state. This doesn't truly remove the element from the document, as it (and it's subtree) still takes up geometric space on the page and can still be clicked on. It also updates the rendering state any time it is needed even when hidden.

content-visibility: hidden, on the other hand, hides the element while preserving its rendering state, so, if there are any changes that need to happen, they only happen when the element is shown again (i.e. the content-visibility: hidden property is removed).

Wow. So it's kind of what display: none should have been all along—a way of removing an element from the layout, gracefully, and completely independently of display type! So the 'opposite' of content-visibility: hidden is content-visibility: visible, but you have a third, very useful option in auto which does lazy rendering for you, speeding up your initial page loading.

The only bad news here is that Firefox and Safari are yet to adopt it. But who knows, by the time you (dear fellow developer) are reading this, that may have changed. Keep one eye on https://caniuse.com/css-content-visibility!

Kal
  • 2,098
  • 24
  • 23
  • No worries! Yes, we can only hope that good sense prevails and the other browsers adopt this soon. Would seem like a no-brainer. – Kal Aug 20 '21 at 00:48
4

you can use

display: normal;

It works as normal.... Its a small hacking in css ;)

JSK NS
  • 3,346
  • 2
  • 25
  • 42
indera
  • 81
  • 1
  • 2
  • 2
    Why is this getting downvoted? Is this a bad way to do it or? It works fine for tablerows, but should I use something else? – Benjamin Karlog Dec 05 '14 at 10:16
  • 8
    The value 'normal' isn't a valid value for `display` property, so it's just ignored and effectively works like `element.style.display = ''` assignment (see [my answer](http://stackoverflow.com/a/17631239/2533215) above). – Ilya Streltsyn Jul 05 '15 at 11:51
  • 39
    so in other words you could have used `display: chunk norris;` for the same effect, with a bit more kick. – Kevin B Nov 23 '15 at 21:21
  • 3
    If you want to override `.element { display: none }` (defined in CSS lib for example) with `.element { display: normal !important }` it won't work. You have to use `.element { display: unset !important }` – tanguy_k Feb 28 '18 at 00:07
4

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.

user3589536
  • 204
  • 1
  • 5
3

I use display:block; It works for me

Starscream1984
  • 3,072
  • 1
  • 19
  • 27
Pradeep Behera
  • 475
  • 6
  • 15
  • 2
    That only is useful for elements that you want to be displayed as `block`, it's not what you want generally for an `inline` element like `` for instance. – Flimm Jun 02 '20 at 13:26
1

In the case of a printer friendly stylesheet, I use the following:

/* screen style */
.print_only { display: none; }

/* print stylesheet */
div.print_only { display: block; }
span.print_only { display: inline; }
.no_print { display: none; }

I used this when I needed to print a form containing values and the input fields were difficult to print. So I added the values wrapped in a span.print_only tag (div.print_only was used elsewhere) and then applied the .no_print class to the input fields. So on-screen you would see the input fields and when printed, only the values. If you wanted to get fancy you could use JS to update the values in the span tags when the fields were updated but that wasn't necessary in my case. Perhaps not the the most elegant solution but it worked for me!

TonyP
  • 91
  • 1
  • 1
1

I ran into this challenge when building an app where I wanted a table hidden for certain users but not for others.

Initially I set it up as display:none but then display:inline-block for those users who I wanted to see it but I experienced the formatting issues you might expect (columns consolidating or generally messy).

The way I worked around it was to show the table first and then do "display:none" for those users who I didn't want to see it. This way, it formatted normally but then disappeared as needed.

Bit of a lateral solution but might help someone!

  • 1
    Even though this doesn't directly answer the question, it's a really good point… You don't need to override `display:none` if you only apply it where needed in the first place, using media queries for example. I do think you could vastly improve the answer though by doing away with the story about your table, and just focusing on the original question. Perhaps you could start by saying, 'The opposite of `display:none` is not using `display:none`' :-) then provide a simple example with HTML and CSS. – Kal Apr 15 '21 at 05:38
0

You can use display: block

Example :

<!DOCTYPE html>
<html>
<body>

<p id="demo">Lorem Ipsum</p>

<button type="button" 
onclick="document.getElementById('demo').style.display='none'">Click Me!</button>
<button type="button" 
onclick="document.getElementById('demo').style.display='block'">Click Me!</button>

</body>
</html> 
0

opposite of 'none' is 'flex' while working with react native.

0

To return to original state put:

 display=""
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137
0

Use display: revert

From the documentation stated on https://developer.mozilla.org/en-US/docs/Web/CSS/revert

The revert CSS keyword reverts the cascaded value of the property from its current value to the value the property would have had if no changes had been made by the current style origin to the current element. Thus, it resets the property to its inherited value if it inherits from its parent or to the default value established by the user agent's stylesheet (or by user styles, if any exist). It can be applied to any CSS property, including the CSS shorthand property all.

It supported accross all major browsers - https://caniuse.com/css-revert-value

michal.jakubeczy
  • 8,221
  • 1
  • 59
  • 63
-2

visibility:hidden will hide the element but element is their with DOM. And in case of display:none it'll remove the element from the DOM.

So you have option for element to either hide or unhide. But once you delete it ( I mean display none) it has not clear opposite value. display have several values like display:block,display:inline, display:inline-block and many other. you can check it out from W3C.

Hardik Sondagar
  • 4,347
  • 3
  • 28
  • 48
-2

display:unset sets it back to some initial setting, not to the previous "display" values

i just copied the previous display value (in my case display: flex;) again(after display non), and it overtried the display:none successfuly

(i used display:none for hiding elements for mobile and small screens)

  • 1
    Hi, and welcome to StackOverflow. Please keep the answer section for answers only. If you have a question about an answer that has already been given, you should ask in the comments of that answer, or ask a new question if it has never been asked before. More about the answer guidelines here: https://stackoverflow.com/help/how-to-answer – Thanh-Quy Nguyen Dec 16 '20 at 22:17
-3

The best answer for display: none is

display:inline

or

display:normal
Pang
  • 9,564
  • 146
  • 81
  • 122
Yasir
  • 489
  • 5
  • 11
  • 2
    Seems that there's no such thing as `display:normal`. See https://developer.mozilla.org/en-US/docs/Web/CSS/display – Pang Sep 18 '17 at 05:03
-4

The best "opposite" would be to return it to the default value which is:

display: inline
TimNguyenBSM
  • 817
  • 2
  • 15
  • 33
  • Just want to make clear that the display property doesn't have a default value, the html element does however. The default value of a DIV would be display:block, a SPAN would default to display:inline. But the display property on it's own does not have a default value. – kevinius Jan 29 '14 at 21:46
-4

You can use this display:block; and also add overflow:hidden;

Pang
  • 9,564
  • 146
  • 81
  • 122
Bel
  • 153
  • 2
  • 11
  • 1
    Please read existing answers before contributing your own. There is a good, well upvoted, and *accepted* [answer from last May](http://stackoverflow.com/a/37289654/19068) and there is also [an answer with a negative total of votes](http://stackoverflow.com/a/34202733/19068) that is more-or-less identical to yours. – Quentin Jan 06 '17 at 10:16
  • I saw the answer. Display:Unset doesnt work as well as a Display:block. My opinion :) @Quentin – Bel Jan 06 '17 at 10:47
  • And `display: block` doesn't work well on a `` or a `` … and a previous answer has already mentioned `display: block`. – Quentin Jan 06 '17 at 10:58
  • 2
    You've edited the answer to mention `overflow: hidden`. Why? What is the point of adding that? It has nothing to do with undoing a `display: none`. – Quentin Jan 06 '17 at 10:59