Web速查-索引

Alt text

定义

top 属性规定元素的顶部边缘。

注意:该属性定义了一个定位元素的上外边距边界与其包含块上边界之间的偏移。如果 “position” 属性的值为 “static”,那么设置 “top” 属性不会产生任何效果。

语法

top:auto|%|length|inherit;

属性值

说明
auto 默认值。通过浏览器计算顶边缘的位置。
% 设置以包含元素的百分比计的顶边位置。可使用负值。
length 使用 px、cm 等单位设置元素的顶边位置。可使用负值。
inherit 规定应该从父元素继承 top 属性的值。

实例

<html>
<head>
<meta charset="UTF-8">
<title>鱼C-零基础入门学习Web(Html5+Css3)</title>
<style type="text/css">
.ex1
{
/*对于绝对定位元素,top属性设置单位高于/低于包含它的元素的顶边。*/
position:absolute;
top:280px;
}
.ex2
{
/*对于相对定位元素,top属性设置单位高于/低于其正常位置的元素的顶边。*/
position:relative;
top:-20px;
}
</style>
</head>
<body>
<div class="ex1" style="width: 200px;height: 200px;background-color: #faa;">ex1</div>
<h1>基准</h1>
<div class="ex2" style="width: 200px;height: 200px;background-color: #afa;">ex2</div>
</body>
</html>

Alt text

浏览器支持

表格中的数字表示支持该属性的第一个浏览器版本号

Safari Chrome FireFox IE
1.0 1.0 1.0 5.5