简体   繁体   中英

show hidden child div without changing the size of parent div

I have the following code:

 function show() { document.getElementById("child").style.display = "block"; } 
 #parent { background-color: orange; padding: 2em; } #child { background-color: pink; padding: 3em; display: none; } 
 <div id="parent" onmouseover="show()"> <div id="child"> Child div </div> </div> 

When I hover over the parent div, the size of the parent changes. Is there a way to show the child div above the parent div, without changing the size of the parent div?

You can use

visibility:hidden

in place of

display:none

To show/hide the content and keep the size unchanged. Need to change the script accordingly.

You could use a relative position on your parent and an absolute position on the child elements:

 function show() { document.getElementById("child").style.display="block"; } 
 #parent { background-color: orange; padding:2em; position:relative; } #child { background-color: pink; padding: 3em; display: none; position: absolute; } 
 <html> <head> <title></title> </head> <body> <div id="parent" onmouseover="show()"> <div id="child"> Child div </div> </div> </body> </html> 

Is this what you require http://jsfiddle.net/u74m4x9o/

<div id="parent" onmouseover="show()">
  <div id="child">
    Child div
  </div>
</div>


   function show() {
  document.getElementById("child").style.display = "block";
} 



#parent {
  background-color: orange;
  padding: 2em;
}
#child {
  background-color: pink;
  padding: 1em;
  display: none;
  position:absolute;
  top : 1em
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM