在页面上定位内容时,可以使用一些属性来帮助您操纵元素的位置。本文将向您展示一些使用 CSSposition 属性包含不同定位元素类型的示例。要在元素上使用定位,您必须首先声明它的 position property,它指定用于元素的定位方法的类型。使用 position 属性值,使用 top、bottom、left 和 right 属性定位元素。它们的工作方式也不同,具体取决于它们的位置值。

定位值有五种类型:

  • 静止的
  • 相对的
  • 固定的
  • 绝对

静止的

HTML元素默认是静态定位的,元素按照文档的正常流程定位;静态定位元素不受 top、bottom、left 和 right 属性的影响。具有位置的元素:静态;没有以任何特殊方式定位

用于将位置设置为静态的 CSS 是:

position: static; 

接下来是使用静态位置值的示例:

<!DOCTYPE html>
<html>
<head>
<style>
body {color: WHITE;font:  Helvetica;width: 420px;
}
.square-set,
.square {border-radius: 15px;
}
.square-set {background: darkgrey;
}
.square {position: static;background: Green;height: 70px;line-height: 40px;text-align: center;width: 90px;
}
</style>
</head>
<body>
<div class="square-set"><figure class="square square-1">SQUARE 1</figure><figure class="square square-2">SQUARE 2</figure><figure class="square square-3">SQUARE 3</figure><figure class="square square-4">SQUARE 4</figure>
</div>
</body>
</html>

相对的

元素根据文档的正常流程定位,相对于其正常位置进行定位,然后 根据顶部、右侧、底部和左侧的值 对于自身进行偏移。偏移量不影响任何其他元素的位置;因此,页面布局中为元素指定的空间与位置是静态的相同。设置相对定位元素的 top、right、bottom 和 left 属性将导致它被调整远离其正常位置。其他内容不会被调整以适应元素留下的任何间隙。

用于将位置设置为相对的 CSS 是:

position: relative; 

下面的示例使用相对位置值:

<!DOCTYPE html>
<html>
<head>
<style>
body {color: white;font:   Helvetica ;width: 420px;
}
.square-set,
.square {border-radius: 15px;
}
.square-set {background: darkgrey;
}
.square {background: green;height: 70px;line-height: 40px;position: relative; text-align: center;width: 80px;
}
.square-1 {top: 15px;}
.square-2 {left: 50px;
}
.square-3 {bottom: -23px;right: 30px;
}
</style>
</head>
<body>
<div class="square-set"><figure class="square square-1">SQUARE 1</figure><figure class="square square-2">SQUARE 2</figure><figure class="square square-3">SQUARE 3</figure><figure class="square square-4">SQUARE 4</figure>
</div>
</body>
</html>

绝对

该元素将从正常的文档流中移除,并且在页面布局中,不会为该元素创建空间。元素相对于最近的定位祖先(如果有的话)定位;否则,它相对于初始包含块放置,其最终位置由顶部、右侧、底部和左侧的值确定。

用于将位置设置为绝对的 CSS 是:

position: absolute; 

一个元素position: absolute; 相对于最近定位的祖先进行定位。如果绝对定位元素没有定位的祖先,它使用文档正文,并与页面滚动一起移动。“定位”元素的位置不是static.

下一个例子强调元素的绝对位置:

<!DOCTYPE html>
<html>
<head>
<style>
.square-set {color: WHITE;background: darkgrey;height: 200px;position: relative;border-radius: 15px;font:  Helvetica ;width: 420px;
}
.square {background: green;height: 80px;position: absolute;width: 80px;border-radius: 15px;line-height: 60px;
}
.square-1 {top: 10%;left: 6%;
}
.square-2 {top: 5;right: -20px;
}
.square-3 {bottom: -15px;right: 40px;
}
.square-4 {bottom: 0;
}
</style>
</head>
<body>
<div class="square-set"><figure class="square square-1">SQUARE 1</figure><figure class="square square-2">SQUARE 2</figure><figure class="square square-3">SQUARE 3</figure><figure class="square square-4">SQUARE 4</figure>
</div>
</body>
</html>

固定的

从正常文档流中删除的元素,并且在页面布局中,没有为元素创建空间。元素相对于由视口建立的初始包含块定位,其最终位置由值 top、right、bottom 和 left 确定。此值始终创建一个新的堆叠上下文。

用于将位置设置为固定的 CSS 如下所示:

position: fixed; 

元素position: fixed; 相对于视口定位,这意味着即使页面滚动,它也始终保持在同一位置。top、right、bottom 和 left 属性用于定位元素。

元素按照文档的正常流程定位,然后根据top、right、bottom和left的值,相对于其最接近的升序块级进行偏移,包括与表格相关的元素。偏移量不会影响任何其他元素的位置。

此值始终创建一个新的堆叠上下文。请注意,粘性元素“粘”到其最近的具有“滚动机制”的祖先,即使该祖先不是最近的实际滚动祖先。

用于将位置设置为粘性的 CSS 是:

position: sticky;

元素position: sticky; 的定位基于用户的滚动位置,relative 并fixed 根据滚动位置在位置之间切换。

重叠元素

网页上的重叠元素对于突出、宣传和关注我们网页上的重要内容非常有用。使元素覆盖在您的网站上是非常有用且非常有价值的功能设计实践。当元素被定位时,它们可以与其他元素重叠,所以要指定顺序(什么元素应该放在其他元素的前面或后面),我们应该使用 z-index 属性。具有较大堆栈顺序的元素始终位于具有较低堆栈顺序的元素之前。请注意,z-index 属性仅适用于定位元素(位置:绝对、位置:相对或位置:固定)。

下一个示例显示 z-index 属性如何在不同的正方形上工作:

<!DOCTYPE html>
<html>
<head>
<style>
.square-set {color: white;background: purple;height: 170px;position: relative;border-radius: 15px;font:  Helvetica;width: 400px;
}
.square {background: orange;border: 4px solid goldenrod;position: absolute;border-radius: 15px;height: 80px;width: 80px;
}
.square-1 {position: relative;z-index: 1;border: dashed;height: 8em;margin-bottom: 1em;margin-top: 2em;
}
.square-2 {position: absolute;z-index: 2; background: black;width: 65%;left: 60px;top: 3em;
}
.square-3 {position: absolute;z-index: 3; background: lightgreen;width: 20%;left: 65%;top: -25px;height: 7em;opacity: 0.9;
}</style>
</head>
<body>
<div class="square-set"><figure class="square square-1">SQUARE 1</figure><figure class="square square-2">SQUARE 2</figure><figure class="square square-3">SQUARE 3</figure>
</div>
</body>
</html>

在图像上定位文本

下面的示例使用上述 CSS 定位值在图像上覆盖一些文本:

<!DOCTYPE html>
<html>
<head>
<style>
.module{background: linear-gradient{rgba(0, 4, 5, 0.6),rgba(2, 0, 3, 0.6)),url(http://www.holtz.org/Library/Images/Slideshows/Gallery/Landscapes/43098-DSC_64xx_landscape-keltern-1_wall.jpg);background-size: cover;width: 600px;height: 400px;margin: 10px 0 0 10px;position: relative;float: left;
}
.mid h3 {font-family: Helvetica;font-weight: 900;color: white;text-transform: uppercase;margin: 0;position: absolute;top: 30%;left: 50%;font-size: 3rem;transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<div class="module mid"><h3>Wild nature</h3>
</div>
</body>
</html>

结论

在本文中,我们描述并给出了 CSS 定位类型的示例,并描述了如何重叠元素并在图像上添加一些文本。

如何使用 CSS 定位 HTML 元素相关推荐

  1. css定位页面元素,页面元素定位-CSS元素基本定位

    基本定位 """属性定位 一 """ # #通过id # driver.find_element_by_css_selector(" ...

  2. 前端:CSS/12/display属性,overflow属性,cursor光标类型,CSS定位,综合案例:今日闪价

    display属性 功能:规定网页元素如何显示: 取值:none(隐藏),block(以块元素显示),inline(以行内元素显示): block:可以实现将行内元素转换为块元素: inline:可以 ...

  3. 在selenium中使用css选择器进行元素定位(一)

    大家在使用selenium元素定位的时候,通常更多使用的是XPATH,css定位方式用得比较少 但有时候css定位方式还是有一些优势的, 优势1:一般情况下定位速度要比XPATH快 优势2:语法要比X ...

  4. html css整理笔记,HTML CSS整理笔记 (八) 定位网页元素

    ----8 定位网页元素---- 51.Position属性:指定盒子的位置,相对它父级的位置或它自身应该在的位置. (1)static 默认无定位,元素按照标准文档布局. (2)relative相对 ...

  5. css怎么使元素绝对定位有过度效果_CSS定位属性Position实例分析

    CSS所提供的Position定位属性在进行网页页面布局过程中非常重要,通过使用Position定位属性可以实现对页面元素进行精确定位,最终达到较好的设计及页面展示效果.本文主要针对Position属 ...

  6. input里面只有name属性 可以用id定位么_Selenium元素定位 提高篇 CSS定位元素

    CSS(Casading Style Sheets)是一种用于渲染HTML或者XML文档的语言,CSS利用其选择器可以将样式属性绑定到文档中的指定元素.理论上来说无论一个元素定位多复杂都能够定位到元素 ...

  7. ie自带css定位,CSS实现元素相对于浏览器窗口进行定位_css

    文章简介:css实现相对浏览器窗口定位彻底研究. Web Developer / Designer 经常需要将一个元素"固定"在页面的某个位置.例如弹出窗口.漂浮广告位等--本文将 ...

  8. Selenium Tips - CSS定位元素

    Selenium Tips - CSS定位元素 原文: http://sauceio.com/index.php/2010/01/selenium-totw-css-selectors-in-sele ...

  9. UI自动化测试(二)浏览器操作及对元素的定位方法(xpath定位和css定位详解)

    Selenium下的Webdriver工具支持FireFox(geckodriver). IE(InternetExplorerDriver).Chrome(ChromeDriver). Opera( ...

最新文章

  1. 机翻降重?掩饰抄袭?SCI期刊上的这些「奇言怪语」,不少来自中国作者
  2. 可穿戴计算之父:成都能成为可穿戴技术未来中心
  3. python模板语言_Python Django 模板语言之 Tags(标签)
  4. 数据仓如何支撑应用?
  5. P2898 [USACO08JAN]haybale猜测Haybale Guessing
  6. 实现TeX的算法:回首编程技术的过去三十年
  7. 什么是生命,这取决于肝脏。——《调音师》影评
  8. 鸟哥的linux私房菜博客目录
  9. 什么是准静态平坦瑞利衰落信道?
  10. 白色背景,白色文字怎么显示清晰?
  11. Golang开发手机应用有两种方式简介
  12. element 问号提示_软件安装时显示问号的解决方法
  13. bps(bitspersecond)是什么?
  14. Flash、EEPROM、SRAM的区别与理解
  15. Key Fact as Pivot: A Two-Stage Model for Low Resource Table-to-Text Generation 论文翻译
  16. MATLAB计算二元一次方程组
  17. 2016年清华特等奖答辩(观后感)
  18. 《缠中说禅108课》19:学习缠中说禅技术分析理论的关键
  19. mysql的load data_【MySQL】load data语句详解(一)
  20. fastclick-源码解析

热门文章

  1. 数据查询和业务流分开_滴滴实时数仓逐层剖解:实时与离线数据误差0.5%
  2. 验证arbiter的思考
  3. 【Vue3+vite+Element-UI Plus 】
  4. 开始记录深度学习工作站的学习之路
  5. Spring三级缓存解决循环依赖
  6. 乐学偶得python视频_乐学偶得 - 课程
  7. python爬虫——图片
  8. 【毕业设计】深度学习+python+opencv实现动物识别 - 图像识别
  9. 内置 DSP,回音消除,噪音抑制全双工通话芯片—ATH8809
  10. 快播“洗白”,难道要抛弃用户?