CSS-定位(Position)

学习自

菜鸟教程

Overview

CSS中HTML元素存在以下之后定位选项

Position Desc
static html 元素的默认的定位方式(即没有定位),元素正常出现在文档流中
fixed 相对于浏览器窗口是固定的
relative 相对于自己的定位
absolute 相对于父布局进行定位
sticky 粘性定位, 相当于 relative 和 fixed 的结合体

static

HTML 元素的 默认 值,即没有定位,元素正常出现在文档流中,此定位方式不会受到 top right .. 的影响。

fixed

元素的位置相对于浏览器窗口是固定的。

<!DOCTYPE <!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<title>Study Html</title>
<style>
h2 {
width: 100%;
border-left-style: solid;
border-left-width: 5px;
border-left-color: cornflowerblue;
background-color: beige;
position: fixed;
}
</style>
</head> <body>
<h2>This is a Fixed H2</h2>
<ul>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<li>Item</li>
<!-- .... -->
</ul> </body> </html>

relative

relative 定位是相对其 正常 的位置.

<!DOCTYPE <!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<title>Study Html</title>
<style>
h2 {
border-left: 5px solid coral;
background-color: aliceblue
} h2.positive-left {
position: relative;
left: 20px;
} h2.negative-left {
position: relative;
left: -20px;
}
</style>
</head> <h2>This is a common normal H2!</h2>
<h2 class="positive-left">This is a left=20px H2!</h2>
<h2 class="negative-left">This is a left=-20px H2!</h2> <body> </body> </html>

absolute

absolute 定位方式相对的是父布局,如果没有父布局,那么相对的是 html 节点。

<!DOCTYPE <!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<title>Study Html</title>
<style>
h2 {
border-left: 5px solid coral;
background-color: aliceblue;
position: absolute;
top: 100px;
left: 100px;
}
</style>
</head> <h2>This is a common normal H2!</h2>
<p>绝对定位的元素的位置相对于最近的已定位父元素,如果元素没有已定位的父元素,那么它的位置相对于html</p> <body> </body> </html>

sticky

sticky 是粘性定位,这种定位方式相当于 relativefixed 定位方式的结合体。

<!DOCTYPE <!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<title>Study Html</title>
<style>
h2 {
position: sticky;
left: 10px;
top: 0PX;
border-left: 5px solid lightblue;
background-color: antiquewhite;
width: 100%;
}
</style>
</head> <p>翻滚吧</p>
<p>翻滚吧</p>
<p>翻滚吧</p>
<p>翻滚吧</p>
<p>翻滚吧</p>
<h2>翻滚吧,牛宝宝!</h2>
<p>翻滚吧</p>
<p>翻滚吧</p>
<p>翻滚吧</p>
<p>翻滚吧</p>
<!-- ..... -->
<body> </body> </html>

重叠的元素

元素的定位与文档流无关,所以他们可以覆盖页面上的其他的元素,z-index 属性确定了哪个元素在上面,哪个元素在下面。

<!DOCTYPE <!DOCTYPE html>
<html> <head>
<meta charset="utf-8" />
<title>Study Html</title>
<style>
img {
position: absolute;
top: 0px;
left: 0px;
z-index: -1;
}
</style>
</head> <p>测试文本测试文本测试文本测试文本测试文本测试......</p>
<img src="./imgs/dog.png" /> <body> </body> </html>

CSS-定位(Position)的更多相关文章

  1. div+css定位position详解

    div+css定位position详解 1.div+css中的定位position 最主要的两个属性:属性 absolute(绝对定位) relative(相对定位),有他们才造就了div+css布局 ...

  2. 《css定位 position》课程笔记

    这是我学习课程css定位 position时做的笔记! 本节内容 html的三种布局方式 position可选参数 z-index 盒子模型和定位的区别 侧边栏导航跟随实例 html的三种布局方式 三 ...

  3. web前端css定位position和浮动float

    最近做了几个项目:配资公司,ecmal商城等,客户对前台要求都很高.所以,今天来谈谈css的基础,以及核心,定位问题. div.h1或p元素常常被称为块级元素.这意味着这些元素显示为一块内容,即“块框 ...

  4. css定位position认识

    1.绝对定位(position: absolute) 2.相对定位(position: relative) 3.固定定位(position: fixed) 绝对定位 设置position:absolu ...

  5. CSS定位position

    position选项来定义元素的定位属性,选项有5个可选值:static.relative.absolute.fixed.inherit 属性值为relative.absolute.fixed时top ...

  6. css 定位position总结

    在CSS中,Position 属性经常会用到,主要是绝对定位和相对定位,简单的使用都没有问题,尤其嵌套起来,就会有些混乱,今记录总结一下,防止久而忘之. CSS position 属性值: absol ...

  7. css定位position属性深究

    1.static:对象遵循常规流.此时4个定位偏移属性不会被应用. 2.relative:对象遵循常规流,并且参照自身在常规流中的位置通过top,right,bottom,left这4个定位偏移属性进 ...

  8. 【前段开发】10步掌握CSS定位: position static relative absolute float

    希望能帮到须要的人,转自:http://www.see-design.com.tw/i/css_position.html 1. position:static 元素的 position 屬性默認值為 ...

  9. css定位-position

    前言 定位的目的就是把元素摆放到指定的位置. 定位上下文:定位元素的大小,位置都是相对于定位上下文的. position属性值有5个值 static:所有有元素定位默认的初始值都是static.就是不 ...

  10. CSS - 定位(position),难点

    元素的定位属性主要包括定位模式和边偏移两部分. 1. 边偏移 边偏移属性 描述 top 顶端偏移量,定义元素相对于其父元素上边线的距离 bottom 底部偏移量,定义元素相对于其父元素下边线的距离 l ...

随机推荐

  1. ROS学习笔记(二) # ROS NodeHandles

    1. 自动启动和关闭 ros::NodeHandle nh: 这段代码执行之后,如果内部节点还没有启动,ros::NodeHandle 会启动这个节点:一旦所有的 ros::NodeHandle 实例 ...

  2. Jetson tx1 安装ROS

    注意,是 Jetson TX1 系统版本: R24.2 参考链接: https://www.youtube.com/watch?v=-So2P0kRYsk

  3. word文档里打不开公式 打开后都是方框

    因为系统缺少一种字体,只要到网络上下载或到其他计算机中复制一种文件名为“symbol.ttf”的字体文件来安装上,就可以了.参考资料:Office之家 http://www.officejia.com ...

  4. springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布

    一.单元测试 生成的demo里面包含spring-boot-starter-test :测试模块,包括JUnit.Hamcrest.Mockito,没有的手动加上. <dependency> ...

  5. mysql系列十三、mysql中replace into和duplicate key的使用区

    一.创建测试表 1.创建唯一索引"b" CREATE TABLE `test2` ( `id` int(10) NOT NULL AUTO_INCREMENT, `a` varch ...

  6. python创建系统用户和用户组

    #coding=utf8 import pwd import grp import sys from _utils.patrol2 import run_cmd info=None try: info ...

  7. eclipse设置代码模板和格式

    该设置可以在保存文件时自动根据模板调整代码格式. 首先准备华为代码格式化文件: FEFO-Formatter.xml FEFO-codetemplates.xml 设置格式Preferences-Ja ...

  8. zabbix3.0.4添加对指定进程的监控

    zabbix3.0.4添加对进程的监控: 主要思路: 通过 ps -ef|grep sdk-push-1.0.0.jar |grep -v grep|wc -l 这个命令来判断进程sdk-push是否 ...

  9. [学习笔记]Javascript的包装对象

    例子1: var s="test"; s.len = 4; var t = s.len // t is undefined 原因是s是字符串,第二行代码,实际上是创建一个临时字符串 ...

  10. 560. Subarray Sum Equals K

    Given an array of integers and an integer k, you need to find the total number of continuous subarra ...