CSS-定位(Position)
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
是粘性定位,这种定位方式相当于 relative
和 fixed
定位方式的结合体。
<!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)的更多相关文章
- div+css定位position详解
div+css定位position详解 1.div+css中的定位position 最主要的两个属性:属性 absolute(绝对定位) relative(相对定位),有他们才造就了div+css布局 ...
- 《css定位 position》课程笔记
这是我学习课程css定位 position时做的笔记! 本节内容 html的三种布局方式 position可选参数 z-index 盒子模型和定位的区别 侧边栏导航跟随实例 html的三种布局方式 三 ...
- web前端css定位position和浮动float
最近做了几个项目:配资公司,ecmal商城等,客户对前台要求都很高.所以,今天来谈谈css的基础,以及核心,定位问题. div.h1或p元素常常被称为块级元素.这意味着这些元素显示为一块内容,即“块框 ...
- css定位position认识
1.绝对定位(position: absolute) 2.相对定位(position: relative) 3.固定定位(position: fixed) 绝对定位 设置position:absolu ...
- CSS定位position
position选项来定义元素的定位属性,选项有5个可选值:static.relative.absolute.fixed.inherit 属性值为relative.absolute.fixed时top ...
- css 定位position总结
在CSS中,Position 属性经常会用到,主要是绝对定位和相对定位,简单的使用都没有问题,尤其嵌套起来,就会有些混乱,今记录总结一下,防止久而忘之. CSS position 属性值: absol ...
- css定位position属性深究
1.static:对象遵循常规流.此时4个定位偏移属性不会被应用. 2.relative:对象遵循常规流,并且参照自身在常规流中的位置通过top,right,bottom,left这4个定位偏移属性进 ...
- 【前段开发】10步掌握CSS定位: position static relative absolute float
希望能帮到须要的人,转自:http://www.see-design.com.tw/i/css_position.html 1. position:static 元素的 position 屬性默認值為 ...
- css定位-position
前言 定位的目的就是把元素摆放到指定的位置. 定位上下文:定位元素的大小,位置都是相对于定位上下文的. position属性值有5个值 static:所有有元素定位默认的初始值都是static.就是不 ...
- CSS - 定位(position),难点
元素的定位属性主要包括定位模式和边偏移两部分. 1. 边偏移 边偏移属性 描述 top 顶端偏移量,定义元素相对于其父元素上边线的距离 bottom 底部偏移量,定义元素相对于其父元素下边线的距离 l ...
随机推荐
- 使用block的时候,导致的内存泄漏
明确,只要在block里边用到我们自己的东西,成员变量,self之类的,我们都需要将其拿出来,把它做成弱指针以便之后进行释放. 在ZPShareViewController这个控制器中,由如下代码: ...
- WinEdt 和 Sumatra 双向关联设置
(1)配置PDF Viewer,在菜单栏选Options -> Execution Modes ->PDF Viewer ->点击右侧的"Browse"按钮,在弹 ...
- TERMIOS详解【转】
转自:https://blog.csdn.net/guo_wangwei/article/details/1102931# TERMIOS NAME termios, tcgetattr, tcset ...
- springmvc和mybatis整合关键配置
springmvc+mybaits的系统架构: 第一步:整合dao层 mybatis和spring整合,通过spring管理mapper接口. 使用mapper的扫描器自动扫描mapper接口在spr ...
- 转载:为什么选择Nginx(1.2)《深入理解Nginx》(陶辉)
原文:https://book.2cto.com/201304/19610.html 为什么选择Nginx?因为它具有以下特点: (1)更快 这表现在两个方面:一方面,在正常情况下,单次请求会得到更快 ...
- 了解的CAP和BASE等理论
CAP,BASE和最终一致性是NoSQL数据库存在的三大基石.而五分钟法则是内存数据存储的理论依据.这个是一切的源头. 几个名词解释: 网络分区:俗称“脑裂”.当网络发生异常情况,导致分布式系统中部分 ...
- 使用JDBC连接数据库报“找不到驱动程序”错误解决
1.jre安装不成功: 2.jre中没有数据库驱动(D:\jre\lib\ext 中查看)
- 保存 laravel model 而不更新 timestamps 的方法
$product = Product::find(1); $product->view_count += 1; $product->timestamps = false; $product ...
- web----WSGI
概念: WSGI协议其实是定义了一种server与application解耦的规范 WSGI规范简单理解:一方面给Server提供接口,凡是以这种接口的web服务器,都是遵循WSGI规范的 另一方面给 ...
- python+selenium一:对浏览器的操作
# 1.打开Firefox浏览器from selenium import webdriverdriver = webdriver.Firefox()driver.get("https://w ...