`
java-mans
  • 浏览: 11630051 次
文章分类
社区版块
存档分类
最新评论

DIV绝对定位 position、z-index、top、right、bottom和left属性

 
阅读更多

转载自:

http://wujt.iteye.com/blog/1181558

一、Position
1、语法:position:static/ absolute / fixed / relative
2、参数:
(1)static:默认值,无特殊定位,对象遵循HTML定位规则。
(2)absolute:将对象从文档流中拖出,使用left,right,top,bottom等属性相对于其最接近的一个最有定位设置的父对象进行绝对定位。如果不存在这样的父对象,则依据body对象。而其层叠通过z-index属性定义。
(3)relative:对象不可层叠,但将依据left,right,top,bottom等属性在正常文档流中偏移位置。
(4)fixed:对象定位遵从绝对(absolute)方式。
3、说明:
对象的定位方式。
设置此属性值为absolute会将对象拖离出正常的文档流绝对定位而不考虑它周围内容的布局。假如其他具有不同z-index属性的对象已经占据了给定的位置,他们之间不会相互影响,而会在同一位置层叠。
此时对象不具有外边距margin,但仍有内边距padding和边框border。
对应的脚本特性为:position。
4、示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript" language="javascript">
function selA(id) {
switch(id) {
case "1":
document.getElementById("subobj").style.position = "static";
break;
case "2":
document.getElementById("subobj").style.position = "absolute";
break;
case "3":
document.getElementById("subobj").style.position = "relative";
break;
case "4":
document.getElementById("subobj").style.position = "fixed";
break;
}
}
</script>
<style type="text/css">
#all {
width:190px;
height:95px;
padding:10px 0 0 10px;
border:1px #000 solid;
position:relative;
}
#subobj {
width:100px;
height:50px;
border:1px #000 solid;
bottom:9px;
position:static;
}
#sel {
margin-top:5px;
}
select {
width:200px;
}
</style>
</head>
<body>
<div id="all">
<div id="subobj">子对象1</div>
</div>
<div id="sel"><select onchange="selA(this.value)"><option value="1">static</option><option value="2">absolute</option><option value="3">relative</option><option value="4">fixed</option></select></div>
</body>
</html>


5、提示:
(1)由上面的例子可以看出,只有position属性值为absolute或relative时top、right、bottom、left才有效。
(2)目前还不支持position:fixed的属性值。
二、z-index
1、语法:z-index:auto/ number 2、参数:
(1)auto:默认值,遵从其父对象的定位。
(2)number:无单位的整数值,可为负数。
3、说明:
设置对象的层叠顺序。
较大number值的对象会覆盖在较小number值的对象之上。如两个绝对定位对象的此属性具有同样的number值,那么将依据它们在HTML文档中声明的顺序层叠对于未指定此属性的绝对定位对象,此属性的number值为正数的对象会在其之上,而number值为负数的对象在其之下。设置参数为null可以移除此属性。 此属性仅仅作用于position属性值为relative或absolute的对象上。
对应的脚本特性为:zIndex。
4、示例:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html http://www.w3.org/1999/xhtml">http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script type="text/javascript" language="javascript">
function selA(id,v) {
if (v) obj = "subobj"; else obj = "subobj2";
switch(id) {
case "0":
document.getElementById(obj).style.zInex = 0;
break;
case "1":
document.getElementById(obj).style.zIndex = 10;
break;
case "2":
document.getElementById(obj).style.zIndex = -10;
break;
}
}
</script>
<style type="text/css">
#all {
width:190px;
height:95px;
padding:10px 0 0 10px;
border:1px #000 solid;
position:relative;
}
#subobj {
width:100px;
height:50px;
border:1px #000 solid;
top:10px;
position:absolute;
background-color:#09C;
}
#subobj2 {
width:100px;
height:50px;
border:1px #000 solid;
top:10px;
position:absolute;
background-color:#CCC;
}
#sel {
margin-top:5px;
}
select {
width:95px;
}
</style>
</head>
<body>
<div id="all">
<div id="subobj">子对象1</div>
<div id="subobj2">子对象2</div>
</div>
<div id="sel">
<select onchange="selA(this.value,1)"><option value="0">对象1</option><option value="1">10</option><option value="2">-10</option></select>
<select onchange="selA(this.value,0)"><option value="0">对象2</option><option value="1">10</option><option value="2">-10</option></select>
</div>
</body>
</html>

5、提示:
(1)z-index只有position属性的值为relative或absolute时才有效。
三、Top、Right、Bottom、Left 四个属性的设置都是相同的,下面以Top为例。
1、语法:top:auto/ length
2、参数:
(1)auto:默认值,无特殊定位,根据HTML定位规则在文档流中分配。
(2)length:由浮点数字和单位标识符组成的长度值或者百分数。必须定义position属性值为absolute或者relative此取值方可生效。请参阅长度单位
3、说明:
设置对象与其最近一个定位的父对象顶部相关的位置。
对应的脚本特性为:top。其值为一字符串,所以不可用于脚本中的计算。请使用style对象的posTop,pixelTop,以及对象的offsetTop等特性。


///////////////////////
div总是被select遮挡的解决方法
//////////////////////////
html>
<head>

<meta http-equiv="Content-Type" c>

<title>div被select遮挡的解决方法——脚本之家</title>
</head>
<body>

<iframe style="position:absolute;z-index:9;width:expression
(this.nextSibling.offsetWidth);height:expression
(this.nextSibling.offsetHeight);top:expression(this.nextSibling.offsetTop);left:expression
(this.nextSibling.offsetLeft);" frameborder="0" ></iframe>

<form id="Form1" method="post">

<div style="z-index:10;position:absolute;background-
color:blue;width:100;height:18;overflow:hidden;" >aaaaaaa<br/>bbbbbbb<br/>ccccccc</div>

<select style="width:200" ><option>test0<option>test1<option>test2<option>test3</select>

<br/>

<pre>

Div被Select挡住,是一个比较常见的问题。

有的朋友通过把div的内容放入iframe或object里来解决。

可惜这样会破坏页面的结构,互动性不大好。
这里采用的方法是:
虽说div直接盖不住select

但是div可以盖iframe,而iframe可以盖select,

所以,把一个iframe来当作div的底,

这个div就可以盖住select了.
</pre>

</form>

</body>
</html>

function install(){
var propertiesCount = chenhao.count();

//var object = document.getElementById('test');
var object = document.body;


createElement(object,chenhao.name,1);
createElement(object,chenhao.email,2);
createElement(object,chenhao.website,3);

createElement(object,chenhao['name'],4);
createElement(object,chenhao['email'],5);
createElement(object,chenhao['website'],6);


//document.("test").innerHTML = chenhao.name;
//document.getElementById("test").innerHTML = chenhao.email;
//document.getElementById("test").innerHTML = chenhao.website;
}

function createElement(object,content,index){
var div = document.createElement("div");
div.setAttribute('style','position:absolute;top:'+parseInt(index*20)+'px;left:100px');
div.innerHTML=content+'';
object.appendChild(div);

}

分享到:
评论

相关推荐

    深入理解css中position属性及z-index属性1

    这意味着元素从文档流中完全移除,并根据设置的`top`、`bottom`、`left`、`right`属性相对于最近的定位父元素进行定位。例如,一个`absolute`定位的元素可以设置`top: 0`和`left: 0`使其贴合到父元素的左上角。 在...

    div的position属性

    `,元素将脱离文档流,并且可以通过`left`、`right`、`top`和`bottom`属性来指定其相对于最近的已定位祖先元素的位置。 #### 四、层模型——相对定位 相对定位也是层模型的一种,它允许元素相对于其正常位置进行...

    div中的相对定位与绝对定位.docx

    相对定位保留了元素在正常文档流中的位置,元素的原始位置不会被改变,但它可以通过设置`top`、`bottom`、`left`和`right`属性来调整其位置。即使元素被移动,它仍然占据着原来的空间,可能会与其他元素重叠。例如:...

    CSS的z-index实例代码

    - 使用top、bottom、left和right属性可以调整定位元素的位置。 - absolute和fixed元素脱离了常规文档流,因此它们不会影响到其他元素的布局。 - 对于fixed元素,其定位是相对于浏览器窗口进行的,因此当页面滚动...

    div弹出层position属性小解

    这意味着它会根据 `top`、`right`、`bottom` 和 `left` 属性相对于最近的已定位祖先元素或初始包含块进行定位。如果没有指定具体的偏移量,则默认情况下 `#sub1` 会位于左上角。 ##### 特性说明 - **相对于最近的已...

    divcss盒子之绝对定位和相对定位.docx

    绝对定位的元素可以层叠,通过z-index属性控制层叠顺序。z-index值越大,元素越靠前。元素的位置通过left, right, top, bottom属性指定,即使父元素移动,已定位的子元素也会跟随移动。 3. 相对定位(relative):...

    详解css定位与定位应用

    - 只有定位元素(`position`不是`static`)才支持`z-index`属性; - 在大多数浏览器中,当`z-index`设置为负数时,元素会被隐藏。 #### 定位元素的位移原理 在CSS中,元素的位移是指元素在其原始位置的基础上...

    div弹出层 定位问题的 处理

    你可以通过`top`, `bottom`, `left`, 和 `right`属性调整其位置。 2. **固定定位**:`position: fixed;`使`div`始终相对于浏览器窗口定位,即使滚动页面也不会改变其位置。这对于创建始终可见的弹出层或悬浮菜单...

    DIV的Style常用属性

    Position 属性用于设置 DIV 的定位方式,常用的值有 relative、absolute 和 fixed。 例如:&lt;div style="position:relative;top:10px;left:10px;width:140px;height:140px;background-color:White;"&gt;&lt;/div&gt; ...

    CSS教程——元素定位

    使用 position: relative 时,需要配合 top、bottom、left、right 四个属性来确定元素的位置。例如: ```css #div-1 { position: relative; top: 20px; left: 40px; } ``` 相对定位的元素将相对于其原始位置...

    CSS定位相关

    当多个绝对定位元素相互重叠时,可以通过`z-index`属性来控制它们的堆叠顺序。`z-index`属性只适用于定位元素(`relative`、`absolute`、`fixed`),对于非定位元素无效。 - **默认值**: `z-index`默认为`0`。 - **...

    Div+Css常用属性

    - `z-index`: 设置`div`的堆叠顺序,当两个元素重叠时,`z-index`较大的元素会覆盖较小的元素。 6. **Font**: 指定`div`中文本的样式。例如: ```html &lt;div style="font:bold 14px 宋体;background-color:Yellow...

    div中的相对定位与绝对定位.pdf

    绝对定位的元素可以使用`z-index`来控制其在堆叠顺序中的位置,数值越大,元素越靠前。例如: ```css .parent { position: relative; /* 创建定位上下文 */ } .child { position: absolute; top: 0; right: 0; ...

    div绝对定位布局页面(上中(包括左中右)下).zip

    `、`top`、`bottom`、`left`和`right`属性来定位。例如: ```css .top-bar { position: absolute; top: 0; left: 0; right: 0; height: 50px; /* 自定义高度 */ } .main-content { position: absolute; top...

    div分层

    相对定位保持元素在正常文档流中的位置,但可以通过top、bottom、left和right属性调整其位置。绝对定位则相对于最近的已定位祖先元素进行定位,没有则相对于初始包含块。固定定位始终相对于浏览器窗口定位。 ```css ...

    css_position用法详解

    - 当 `position` 被设置为 `relative` 时,可以通过 `z-index` 来改变元素的堆叠顺序。 - **示例代码**: ```html &lt;div style="position:relative;height:50px;width:50px;background:#f00;left:50px;top:50px;...

    HTML5+CSS3网页设计-第八章 定位网页元素.pptx

    - `relative`:相对定位,元素相对于其原本在标准流中的位置进行偏移,不影响其他元素的位置,偏移量通过top、right、bottom、left属性设定。 - `absolute`:绝对定位,元素相对于最近的非static定位的祖先元素...

    DIV 样式 CSS 各种属性 JavaScript 中会用到的CSS

    5. **Left**, **Top**, **Right** 和 **Bottom**: 当`position`设置为非`static`时,可以使用这些属性来调整元素相对于其定位参照物的位置。 6. **Z-Index**: `z-index` 控制元素的堆叠顺序,数值越大,元素越靠前...

Global site tag (gtag.js) - Google Analytics