Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

position a div under another element

Tags:

html

css

how can I position a div under another element without the rest of the layout is affected of 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>
like image 776
clarkk Avatar asked Jun 26 '26 14:06

clarkk


2 Answers

Use the css property z-index or the javascript style property zIndex.

z-index defines the layer's height. The canvas (usually the body tag) uses the z-index 0. Anything higher is above body, anything lower 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>
like image 55
Spliffster Avatar answered Jun 28 '26 06:06

Spliffster


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

like image 34
thirtydot Avatar answered Jun 28 '26 06:06

thirtydot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!