CSS基础学习之元素定位(6)

        

目录

1、定位类型

2、取值

2.1、static

2.2、relative

2.3、absolute

2.4、fixed

2.5、stickty

3、示例

3.1、相对定位(relative)

3.2、绝对定位(absolute) 

3.3、固定定位(fixed) 

3.4、粘性定位(sticky) 

4、综合示例


CSS position 属性用于指定一个元素在文档中的定位方式。top,right,bottom 和 left 属性则决定了该元素的最终位置。

1、定位类型

  • 定位元素(positioned element)是其计算后位置属性为 relativeabsolutefixed 或 sticky 的一个元素(换句话说,除static以外的任何东西)。
  • 相对定位元素(relatively positioned element)是计算后位置属性为 relative的元素。
  • 绝对定位元素(absolutely positioned element)是计算后位置属性为 absolute 或 fixed 的元素。
  • 粘性定位元素(stickily positioned element)是计算后位置属性为 sticky 的元素。

        大多数情况下,height和width 被设定为 auto 的绝对定位元素,按其内容大小调整尺寸。但是,被绝对定位的元素可以通过指定top和bottom ,保留height未指定(即auto),来填充可用的垂直空间。它们同样可以通过指定left 和 right并将width 指定为auto来填充可用的水平空间。

        除了刚刚描述的情况(绝对定位元素填充可用空间):

  • 如果 top 和 bottom 都被指定(严格来说,这里指定的值不能为 auto ),top 优先。
  • 如果指定了 left 和 right ,当 direction设置为 ltr(水平书写的中文、英语)时 left 优先,当direction设置为 rtl(阿拉伯语、希伯来语、波斯语由右向左书写)时 right 优先。

2、取值

2.1、static

        该关键字指定元素使用正常的布局行为,即元素在文档常规流中当前的布局位置。此时 toprightbottomleft 和 z-index 属性无效。

2.2、relative

        该关键字下,元素先放置在未添加定位时的位置,再在不改变页面布局的前提下调整元素位置(因此会在此元素未添加定位时所在位置留下空白)。position:relative 对 table-*-group, table-row, table-column, table-cell, table-caption 元素无效。

2.3、absolute

        元素会被移出正常文档流,并不为元素预留空间,通过指定元素相对于最近的非 static 定位祖先元素的偏移,来确定元素位置。绝对定位的元素可以设置外边距(margins),且不会与其他边距合并。

2.4、fixed

        元素会被移出正常文档流,并不为元素预留空间,而是通过指定元素相对于屏幕视口(viewport)的位置来指定元素位置。元素的位置在屏幕滚动时不会改变。打印时,元素会出现在的每页的固定位置。fixed 属性会创建新的层叠上下文。当元素祖先的 transformperspectivefilter 或 backdrop-filter 属性非 none 时,容器由视口改为该祖先。

2.5、stickty

        元素根据正常文档流进行定位,然后相对它的最近滚动祖先(nearest scrolling ancestor)和 containing block(最近块级祖先 nearest block-level ancestor),包括 table-related 元素,基于 toprightbottom 和 left 的值进行偏移。偏移值不会影响任何其他元素的位置。 该值总是创建一个新的层叠上下文(stacking context)。注意,一个 sticky 元素会“固定”在离它最近的一个拥有“滚动机制”的祖先上(当该祖先的 overflow 是 hiddenscrollauto 或 overlay 时),即便这个祖先不是最近的真实可滚动祖先。这有效地抑制了任何“sticky”行为(详情见 Github issue on W3C CSSWG)。

3、示例

3.1、相对定位(relative)

        相对定位的元素是在文档中的正常位置偏移给定的值,但是不影响其他元素的偏移。下面的例子中,注意未应用定位的其他元素是按照 "Two" 在正常位置的情况下进行布局的。

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>元素定位学习</title><style>.box {display: inline-block;width: 100px;height: 100px;background: red;color: white;}#two {position: relative;top: 20px;left: 20px;background: blue;}</style>
</head><body><div class="box" id="one">One</div><div class="box" id="two">Two</div><div class="box" id="three">Three</div><div class="box" id="four">Four</div>
</body></html>

        运行效果如下:

3.2、绝对定位(absolute) 

        相对定位的元素并未脱离文档流,而绝对定位的元素则脱离了文档流。在布置文档流中其他元素时,绝对定位元素不占据空间。绝对定位元素相对于最近的非 static 祖先元素定位。当这样的祖先元素不存在时,则相对于 ICB(initial containing block,初始包含块)。

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>元素定位学习</title><style>* {box-sizing: border-box;}body {width: 500px;margin: 0 auto;}p {background: aqua;border: 3px solid blue;padding: 10px;margin: 10px;}span {background: red;border: 1px solid black;}.positioned {position: absolute;background: yellow;top: 40px;left: 30px;}</style>
</head><body><h1>Absolute positioning</h1><p>I am a basic block level element. My adjacent block level elements sit on newlines below me.</p><p class="positioned">By default we span 100% of the width of our parent element, and we are as tallas our child content. Our total width and height is our content + padding +border width/height.</p><p>We are separated by our margins. Because of margin collapsing, we areseparated by the width of one of our margins, not both.</p><p>inline elements <span>like this one</span> and <span>this one</span> sit onthe same line as one another, and adjacent text nodes, if there is space onthe same line. Overflowing inline elements<span>wrap onto a new line if possible — like this one containing text</span>,or just go on to a new line if not, much like this image will do:<img src="./images/long.png" /></p></body></html>

        运行效果如下:

3.3、固定定位(fixed) 

        固定定位与绝对定位相似,但元素的包含块为 viewport 视口。该定位方式常用于创建在滚动屏幕时仍固定在相同位置的元素。在下面的示例中,"One" 元素定位在离页面顶部 20px,离页面左侧 20px 的位置。

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>元素定位学习</title><style>.box {background: red;width: 100px;height: 100px;margin: 20px;color: white;}#one {position: fixed;top: 20px;left: 10px;}.outer {width: 500px;height: 300px;overflow: scroll;padding-left: 150px;}</style>
</head><body><div class="outer"><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam congue tortoreget pulvinar lobortis. Vestibulum ante ipsum primis in faucibus orci luctuset ultrices posuere cubilia Curae; Nam ac dolor augue. Pellentesque mi mi,laoreet et dolor sit amet, ultrices varius risus. Nam vitae iaculis elit.Aliquam mollis interdum libero. Sed sodales placerat egestas. Vestibulum utarcu aliquam purus viverra dictum vel sit amet mi. Duis nisl mauris, aliquamsit amet luctus eget, dapibus in enim. Sed velit augue, pretium a semaliquam, congue porttitor tortor. Sed tempor nisl a lorem consequat, idmaximus erat aliquet. Sed sagittis porta libero sed condimentum. Aliquamfinibus lectus nec ante congue rutrum. Curabitur quam quam, accumsan idultrices ultrices, tempor et tellus.</p><p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam congue tortoreget pulvinar lobortis. Vestibulum ante ipsum primis in faucibus orci luctuset ultrices posuere cubilia Curae; Nam ac dolor augue. Pellentesque mi mi,laoreet et dolor sit amet, ultrices varius risus. Nam vitae iaculis elit.Aliquam mollis interdum libero. Sed sodales placerat egestas. Vestibulum utarcu aliquam purus viverra dictum vel sit amet mi. Duis nisl mauris, aliquamsit amet luctus eget, dapibus in enim. Sed velit augue, pretium a semaliquam, congue porttitor tortor. Sed tempor nisl a lorem consequat, idmaximus erat aliquet. Sed sagittis porta libero sed condimentum. Aliquamfinibus lectus nec ante congue rutrum. Curabitur quam quam, accumsan idultrices ultrices, tempor et tellus.</p><div class="box" id="one">One</div></div></body></html>

        运行效果如下:

3.4、粘性定位(sticky) 

        粘性定位可以被认为是相对定位和固定定位的混合。元素在跨越特定阈值前为相对定位,之后为固定定位。例如:

#one {position: sticky;top: 10px;
}

        在 viewport 视口滚动到元素 top 距离小于 10px 之前,元素为相对定位。之后,元素将固定在与顶部距离 10px 的位置,直到 viewport 视口回滚到阈值以下。

        粘性定位常用于定位字母列表的头部元素。标示 B 部分开始的头部元素在滚动 A 部分时,始终处于 A 的下方。而在开始滚动 B 部分时,B 的头部会固定在屏幕顶部,直到所有 B 的项均完成滚动后,才被 C 的头部替代。

须指定 top, right, bottom 或 left 四个阈值其中之一,才可使粘性定位生效。否则其行为与相对定位相同。

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>元素定位学习</title><style>* {box-sizing: border-box;}dl {margin: 0;padding: 24px 0 0 0;}dt {background: #b8c1c8;border-bottom: 1px solid #989ea4;border-top: 1px solid #717d85;color: #fff;font:bold 18px/21px Helvetica,Arial,sans-serif;margin: 0;padding: 2px 0 0 12px;position: -webkit-sticky;position: sticky;top: -1px;}dd {font:bold 20px/45px Helvetica,Arial,sans-serif;margin: 0;padding: 0 0 0 12px;white-space: nowrap;}dd+dd {border-top: 1px solid #ccc;}</style>
</head><body><div><dl><dt>A</dt><dd>Andrew W.K.</dd><dd>Apparat</dd><dd>Arcade Fire</dd><dd>At The Drive-In</dd><dd>Aziz Ansari</dd></dl><dl><dt>C</dt><dd>Chromeo</dd><dd>Common</dd><dd>Converge</dd><dd>Crystal Castles</dd><dd>Cursive</dd></dl><dl><dt>E</dt><dd>Explosions In The Sky</dd></dl><dl><dt>T</dt><dd>Ted Leo & The Pharmacists</dd><dd>T-Pain</dd><dd>Thrice</dd><dd>TV On The Radio</dd><dd>Two Gallants</dd></dl></div></body></html>

 运行效果如下:

sticky定位效果

4、综合示例

        如下是我们要实现的一个效果:

         首先是中间的虚线方框:

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>元素定位学习</title><style>div {height: 200px;width: 200px;position: absolute;left: 200px;top: 50px;font-size: 4rem;line-height: 200px;text-align: center;}.original {border: 1px dashed;}</style>
</head><body><div class="original"></div></body></html>

        效果如下:

        没错就是一个方框,接下来添加两条虚线,水平的那条是没有旋转的线,另外一条旋转了135度(顺时针方向):

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>元素定位学习</title><style>div {height: 200px;width: 200px;position: absolute;left: 200px;top: 50px;font-size: 4rem;line-height: 200px;text-align: center;}.original {border: 1px dashed;}.original:before,.original:after {content: "";position: absolute;top: 100px;width: 500px;left: -150px;height: 1px;border-top: 2px dotted;}.original:after {transform: rotate(135deg);}</style>
</head><body><div class="original"></div></body></html>

         效果如下:

        这里使用了伪元素.original:before及 .original:after,这两个伪元素就是负责添加水平虚线和另外一条旋转过的虚线的。

        .original:after {content: "";position: absolute;top: 100px;width: 500px;left: -150px;height: 1px;border-top: 2px dotted;}

         定位方式为绝对定位,而绝对定位元素相对于最近的非 static 祖先元素定位。当这样的祖先元素不存在时,则相对于 ICB(initial containing block,初始包含块),这里其实是相对于div定位了。

        接下来就是完整的效果,代码如下:

<!DOCTYPE html>
<html><head><meta charset="utf-8"><title>元素定位学习</title><style>div {height: 200px;width: 200px;position: absolute;left: 200px;top: 50px;font-size: 4rem;line-height: 200px;text-align: center;}.original {border: 1px dashed;}.original:before,.original:after {content: "";position: absolute;top: 100px;width: 500px;left: -150px;height: 1px;border-top: 2px dotted;}.original:after {transform: rotate(135deg);}.one {background-color: #ccc;}.two {background-color: #d6bb72;}.one {transform: translateX(200px) rotate(135deg);}.two {transform: rotate(135deg) translateX(200px);}</style>
</head><body><div class="original"></div><div class="one">1</div><div class="two">2</div></body></html>

        该案例有助于理解元素定位以及transform的作用。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/872662.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大模型中的MoE是什么?

大模型中的MoE是什么&#xff1f; MoE&#xff08;Mixture of Experts&#xff09;是一种用于提高深度学习模型性能和效率的架构。其核心思想是通过引入多个专家&#xff08;Experts&#xff09;模型&#xff0c;每个输入数据只选择和激活其中的一部分专家模型来进行处理&…

nacos 适配瀚高数据库、ARM 架构

下载nacos源码&#xff1a; https://github.com/alibaba/nacos/tree/2.3.1 瀚高技术文档 1、修改pom.xml 根目录nacos-all > pom.xml<dependencyManagement><dependency><groupId>com.highgo</groupId><artifactId>HgdbJdbc</artifactI…

硅纪元视角 | 微软开发全新AI模型,革新电子表格处理效率!

在数字化浪潮的推动下&#xff0c;人工智能&#xff08;AI&#xff09;正成为塑造未来的关键力量。硅纪元视角栏目紧跟AI科技的最新发展&#xff0c;捕捉行业动态&#xff1b;提供深入的新闻解读&#xff0c;助您洞悉技术背后的逻辑&#xff1b;汇聚行业专家的见解&#xff0c;…

使用llama-cpp-python制作api接口

文章目录 概要整体操作流程技术细节小结 概要 使用llama-cpp-python制作api接口&#xff0c;可以接入gradio当中&#xff0c;参考上一节。 llama-cpp-python的github网址 整体操作流程 下载llama-cpp-python。首先判断自己是在CPU的环境下还是GPU的环境下。以下操作均在魔搭…

【Linux杂货铺】期末总结篇4:shell编程

&#x1f308;个人主页&#xff1a;聆风吟_ &#x1f525;系列专栏&#xff1a;Linux实践室、网络奇遇记 &#x1f516;少年有梦不应止于心动&#xff0c;更要付诸行动。 文章目录 一. ⛳️什么是Shell脚本&#xff1f;二. ⛳️Shell 入门三. ⛳️Shell 变量3.1 &#x1f514;变…

Kotlin中Unit、Any和Nothing

Unit Unit是一个特殊的类型&#xff0c;它表示“没有意义的值”的单元类型。在Kotlin中&#xff0c;当你不需要函数返回任何具体值时&#xff0c;可以使用Unit类型。 和Java 中 void一样。 Any 所有非空类的父类 Any?所有类的父类 类似Java中Object Nothing 表示一个函数或…

旗晟巡检机器人的应用场景有哪些?

巡检机器人作为现代科技的杰出成果&#xff0c;已广泛应用于各个关键场景。从危险的工业现场到至关重要的基础设施&#xff0c;它们的身影无处不在。它们以精准、高效、不知疲倦的特性&#xff0c;担当起保障生产、守护安全的重任&#xff0c;为行业发展注入新的活力。那么&…

如何使用简鹿水印助手或 Photoshop 给照片添加文字

在社交媒体中&#xff0c;为照片添加个性化的文字已经成为了一种流行趋势。无论是添加注释、引用名言还是表达情感&#xff0c;文字都能够为图片增添额外的意义和风格。本篇文章将使用“简鹿水印助手”和“Adobe Photoshop”这两种工具给照片添加文字的详细步骤。 使用简鹿水印…

IDEA实现NPM项目的自打包自发布自部署

目录 前言 正文 操作背景 NPM自发布 Package自发布 NPM部署 尾声 &#x1f52d; Hi,I’m Pleasure1234&#x1f331; I’m currently learning Vue.js,SpringBoot,Computer Security and so on.&#x1f46f; I’m studying in University of Nottingham Ningbo China&#x1f…

射线和平面求交

射线和平面求交 1、平面方程 如果已知平面的高度&#xff08;即沿法向量方向的距离&#xff09;为 height&#xff0c;平面方程可以表示为&#xff1a; n ^ ⋅ p h e i g h t \bold{\hat{n}} \cdot p height n^⋅pheight p p p 是平面上的任意一点 height 的正负取决于法向量…

W外链创建抖音私信卡片教程,私信卡片跳转微信工具

W外链地址wai.cn 在数字化时代的浪潮中&#xff0c;私域流量的价值愈发凸显&#xff0c;成为企业获取用户、建立品牌忠诚度、提升转化率的关键手段。抖音&#xff0c;作为当下最热门的短视频社交平台之一&#xff0c;其用户基数庞大、互动性强&#xff0c;为企业私域引流提供了…

一些颜色的RGB整理

(214,219,233) (215,220,230) (189,189,189) (193,210,240) (190,210,240) (0,60,119) (0,60,120) (230,230,250)

初识Docker及管理Docker

Docker部署 初识DockerDocker是什么Docker的核心概念镜像容器仓库 容器优点容器在内核中支持2种重要技术&#xff1a;Docker容器与虚拟机的区别 安装Docker源码安装yum安装检查Docker Docker 镜像操作配置镜像加速器&#xff08;阿里系&#xff09;搜索镜像获取镜像查看镜像信息…

计算机网络技术期末复习

一. 填空 在采用电信号表达数据的系统中&#xff0c;数据有 数字数据 和 模拟数据 两种。域名系统DNS是一个 分布式数据库 系统。TCP/IP的网络层最重要的协议是 IP互连网协议&#xff0c;它可将多个网络连成一个互连网。 4. 在TCP/IP层次模型的网络层中包括的协议主要有ARP 、…

科技出海|百分点科技智慧政务解决方案亮相非洲展会

近日&#xff0c;华为非洲全联接大会在南非约翰内斯堡举办&#xff0c;吸引政府官员行业专家、思想领袖、生态伙伴等2,000多人参会&#xff0c;百分点科技作为华为云生态合作伙伴&#xff0c;重点展示了智慧政务解决方案&#xff0c;发表《Enable a Smarter Government with Da…

Web开发:卡片翻转效果(HTML、CSS)

目录 一、实现效果 二、完整代码 三、实现过程 1、页面结构 2、初始样式 3、翻转效果 4、图片大小问题 一、实现效果 如下图所示&#xff0c;当鼠标移入某个盒子&#xff0c;就反转这个盒子&#xff0c;并显示其背面的内容——卡片翻转效果&#xff1b; 卡片翻转效果 二…

Linux网络编程-socket套接字使用详解

1.概念 在Linux中&#xff0c;套接字&#xff08;socket&#xff09;是一种通信机制&#xff0c;用于实现不同进程之间或同一主机上的不同线程之间的数据交换。它是网络编程的基础&#xff0c;允许应用程序通过网络进行通信&#xff0c;也可以在同一台机器上的不同进程间进行通…

集群服务器如何解决跨服务器通信?大量并发通信问题?

Nginx tcp负载均衡模块&#xff1a; 1.将client的请求按照 负载均衡算法 分发到服务器 2.负载均衡器与服务器保持心跳机制&#xff0c;监测故障、保障服务可靠性 3.可以发现添加新的服务器&#xff0c;方便扩展服务器集群的数量 Nginx反向代理用途&#xff1a; 2.4 用途 …

在golang中Sprintf和Printf 的区别

最近一直在学习golang这个编程语言&#xff0c;我们这里做一个笔记就是 Sprintf和Printf 的区别 fmt.Sprintf 根据格式化参数生成格式化的字符串并返回该字符串。 fmt.Printf 根据格式化参数生成格式化的字符串并写入标准输出。由上面就可以知道&#xff0c;fmt.Sprintf返回的…

php随机海量高清壁纸系统源码,数据采集于网络,使用很方便

2022 多个分类随机海量高清壁纸系统源码&#xff0c;核心文件就两个&#xff0c;php文件负责采集&#xff0c;html负责显示&#xff0c;很简单。做流量工具还是不错的。 非第三方接口&#xff0c;图片数据采集壁纸多多官方所有数据&#xff01; 大家拿去自行研究哈&#xff0…