上课这点事-CSS知识

定位

定位属性列表position、top、bottom、right、left、z-index、position

基本语法:

position:static | absolute | fixed | relative

语法介绍:

static:默认值,无特殊定位。

absoulte:相对于其最近的一个定位设置的父对象进行绝对定位,可用left,right,top,bottom。

fixed:生成绝对定位的元素。

relative:生成相对定位的元素,可通过left,right,top,bottom属性设置相对于自身偏移位置。

代码:

div{

    position:relative;top:-4px;

}

bottom

基本特殊:定位元素

基本语法:bottom:auto | length

语法

auto:默认值,无特殊定位。

length:长度值 | 百分比,必须定义position的属性值为absolute或者relative才有效。

代码:

div{

    position:relative;bottom:6px;

 }

z-index

语法:z-index:auto | number

取值:auto:默认值,number:无单位的整数值,可负数。

代码:

div{

    position:absolute;z-index:5;width:6px; 

}

left

基本语法:left:auto | length

auto:默认值,无特殊定位。

length:长度值 | 百分比,必须定义position的属性值为absolute或者relative才有效。

代码:

div{

    position:relative;top:-3px;left:6px; 

}

top

基本语法:top:auto | length

auto:默认值,无特殊定位。

length:长度值 | 百分比,必须定义position的属性值为absolute或者relative才有效。

代码:

div{

    position:relative;top:-3px;left:5px;

}

right

基本语法:right:auto | length

auto:默认值,无特殊定位。

length:长度值 | 百分比,必须定义position的属性值为absolute或者relative才有效。

代码:

div{

    position:relative;top:-3px;right:6px

}

相对定位

relative生成相对定位的元素,相对于其它位置进行定位。

代码:

#box1{

margin:10px;width:100px;height:100px;background-color: blue;      

  }

#box2{

    margin:10px;width:100px;height:100px;background-color: red;/*position: relative;left: 100px; top:100px;*/

}

绝对定位

绝对定位相对于它的参照物移动位置,如果没有,默认为body为参照物。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 问答题47 /72 常见浏览器兼容性问题与解决方案? 参考答案 (1)浏览器兼容问题一:不同浏览器的标签默认的外补...
    _Yfling阅读 13,816评论 1 92
  • 各种纯css图标 CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出。直接用CSS3画出这些图形,要比...
    剑残阅读 9,715评论 0 8
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 27,811评论 1 45
  • 1. 前言 前端圈有个“梗”:在面试时,问个css的position属性能刷掉一半人,其中不乏工作四五年的同学。在...
    YjWorld阅读 4,559评论 5 15
  • 金樽对月操酒狂,宿醉俏骂饮醒汤。 泛酒品茗度无异,何干茶圣与杜康。
    墨迹简书阅读 668评论 2 5