开发者

position a div under another element

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>


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>


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜