Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I show an element that has display: none in a CSS rule?

I have a really simple external css stylesheet that has the following :

div.hideBox {     display:none; } 

So when the html page loads, the div with that class attribute 'hideBox' will not show on the page, which is what I want. But I the box to show/appear when a user clicks on a button on that same page. I tried to use the onclick event to do this, but the div won't show.

So for example, the code would be :

<script language="javascript"> function showmydiv() { document.getElementById('mybox').style.display = ""; }  </script> </head> <body> <div id="mybox" class="hideBox"> some output of text </div> <input type="button" name="ShowBox" value="Show Box" onclick="showmydiv()"> 

What's strange is that a setup similar to this works when I use visibility:hidden; position:absolute; and I can use a JavaScript function to show the <div>.

What am I doing wrong here?

like image 725
katura Avatar asked Nov 01 '10 16:11

katura


People also ask

How do I show display none in CSS?

getElementById("element"). style. display = "none"; To show an element, set the style display property to “block”.

How do you counter display none?

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.

What happen to the element when the value of display property is none?

display: none Using a display value of none on an element will remove it from the accessibility tree. This will cause the element and all its descendant elements to no longer be announced by screen reading technology.


1 Answers

Because setting the div's display style property to "" doesn't change anything in the CSS rule itself. That basically just creates an "empty," inline CSS rule, which has no effect beyond clearing the same property on that element.

You need to set it to something that has a value:

document.getElementById('mybox').style.display = "block"; 

What you're doing would work if you were replacing an inline style on the div, like this:

<div id="myBox" style="display: none;"></div>  document.getElementById('mybox').style.display = ""; 
like image 179
Matt Ball Avatar answered Sep 18 '22 05:09

Matt Ball