Div position under another element

how can i put a div under another element without the rest of the layout, depends on the div element?

<div style="margin:50px">
    <div style="margin:20px; padding:10px; width:100px">
        here is a little <span id="test" style="font-weight:bold">test</span>.. some text some text some text some text
    </div>
</div>
<script>
    var elm = document.getElementById('test');
    var div = elm.appendChild(document.createElement('div'));
    with(div){
        style.position = 'relative';
        style.left = elm.offsetLeft+'px';
        style.background = '#ffffff';
        style.width = '100px';
        style.height = '50px';

        innerHTML = 'wooop';
    }
</script>
+3
source share
2 answers

style.left = elm.offsetLeft + "px";

+1
source

Use css zss index or zIndex javascript style property.

z-index determines the height of the layer. A canvas (usually a body tag) uses the z-index of 0. Everything above is above the body, something below below.

Css example:

<style type="text/css">
.my_layer {
  display: block; 
  position: absolute; 
  z-index: 10; 
  top: 10px; 
  left: 10px; 
  width: 100px; 
  height: 100px;
}
</style>

Javascript example:

<script type="text/javascript">
  el = document.getElementById("my_div")
  with(el) {
    style.zIndex = 10;
    style.display = "block"; 
    style.position = "absolute"; 
    style.top = "10px"; 
    style.left = "10px"; 
    style.width = "100px"; 
    style.height = "100px";
  }
</script>
+2
source

Source: https://habr.com/ru/post/1789115/


All Articles