小码哥的IT人生

CSS 布局 - position 属性 详解

css3基础 2022-05-23 12:04:16小码哥的IT人生shichen

CSS 布局 - position 属性

position 属性规定应用于元素的定位方法的类型(static、relative、fixed、absolute 或 sticky)。

position 属性

position 属性规定应用于元素的定位方法的类型。

有五个不同的位置值:

  1. static
  2. relative
  3. fixed
  4. absolute
  5. sticky

元素其实是使用 top、bottom、left 和 right 属性定位的。但是,除非首先设置了 position 属性,否则这些属性将不起作用。根据不同的 position 值,它们的工作方式也不同。

position: static;

HTML 元素默认情况下的定位方式为 static(静态)。

静态定位的元素不受 top、bottom、left 和 right 属性的影响。

position: static; 的元素不会以任何特殊方式定位;它始终根据页面的正常流进行定位:

这个 <div> 元素设置了 position: static;

这是所用的 CSS:

示例代码:

div.static {
  position: static;
  border: 3px solid #73AD21;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
div.static {
  position: static;
  border: 3px solid #73AD21;
}
</style>
</head>
<body>
<h1>position: static;</h1>
<p>设置 position: static; 的元素不会以任何特殊方式定位;它是始终根据页面的正常流进行定位:</p>
<div class="static">
这个 div 元素设置 position: static;
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

position: relative;

position: relative; 的元素相对于其正常位置进行定位。

设置相对定位的元素的 top、right、bottom 和 left 属性将导致其偏离其正常位置进行调整。不会对其余内容进行调整来适应元素留下的任何空间。

这个 <div> 元素设置了 position: relative;

这是所用的 CSS:

示例代码:

div.relative {
  position: relative;
  left: 30px;
  border: 3px solid #73AD21;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
div.relative {
  position: relative;
  left: 30px;
  border: 3px solid #73AD21;
}
</style>
</head>
<body>
<h1>position: relative;</h1>
<p>设置 position: relative; 的元素相对于其正常位置进行定位:</p>
<div class="relative">
这个 div 元素设置 position: relative;
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

position: fixed;

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

固定定位的元素不会在页面中通常应放置的位置上留出空隙。

请注意页面右下角的这个固定元素。这是所用的 CSS:

示例代码:

div.fixed {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 300px;
  border: 3px solid #73AD21;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
div.fixed {
  position: fixed;
  bottom: 0;
  right: 0;
  width: 300px;
  border: 3px solid #73AD21;
}
</style>
</head>
<body>
<h1>position: fixed;</h1>
<p>设置 position: fixed; 的元素会相对视口定位,这意味着即使页面滚动也会停留在某个位置:</p>
<div class="fixed">
这个 div 元素设置 position: fixed;
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

这个 <div> 元素设置了 position: fixed;

position: absolute;

position: absolute; 的元素相对于最近的定位祖先元素进行定位(而不是相对于视口定位,如 fixed)。

然而,如果绝对定位的元素没有祖先,它将使用文档主体(body),并随页面滚动一起移动。

注意:“被定位的”元素是其位置除 static 以外的任何元素。

这是一个简单的例子:

这个 <div> 元素设置了 position: relative;
这个 <div> 元素设置了 position: absolute;

这是所用的 CSS:

示例代码:

div.relative {
  position: relative;
  width: 400px;
  height: 200px;
  border: 3px solid #73AD21;
}
div.absolute {
  position: absolute;
  top: 80px;
  right: 0;
  width: 200px;
  height: 100px;
  border: 3px solid #73AD21;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
div.relative {
  position: relative;
  width: 400px;
  height: 200px;
  border: 3px solid #73AD21;
}
div.absolute {
  position: absolute;
  top: 80px;
  right: 0;
  width: 200px;
  height: 100px;
  border: 3px solid #73AD21;
}
</style>
</head>
<body>
<h1>position: absolute;</h1>
<p>设置 position: absolute; 的元素会相对于最近的定位祖先进行定位(而不是相对于视口进行定位,比如 fixed):</p>
<div class="relative">这个 div 元素设置 position: relative;
  <div class="absolute">这个 div 元素设置 position: absolute;</div>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

position: sticky;

position: sticky; 的元素根据用户的滚动位置进行定位。

粘性元素根据滚动位置在相对(relative)和固定(fixed)之间切换。起先它会被相对定位,直到在视口中遇到给定的偏移位置为止 - 然后将其“粘贴”在适当的位置(比如 position:fixed)。

注意:Internet Explorer、Edge 15 以及更早的版本不支持粘性定位。 Safari 需要 -webkit- 前缀(请参见下面的实例)。您还必须至少指定 toprightbottomleft 之一,以便粘性定位起作用。

在此例中,在到达其滚动位置时,sticky 元素将停留在页面顶部(top: 0)。

示例代码:

div.sticky {
  position: -webkit-sticky; /* Safari */
  position: sticky;
  top: 0;
  background-color: green;
  border: 2px solid #4CAF50;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
div.sticky {
  position: -webkit-sticky;
  position: sticky;
  top: 0;
  padding: 5px;
  background-color: #cae8ca;
  border: 2px solid #4CAF50;
}
</style>
</head>
<body>
<p>请试着在这个框架内<b>滚动</b>页面,以理解粘性定位的原理。</p>
<div class="sticky">我是有粘性的!</div>
<div style="padding-bottom:2000px">
  <p>在此例中,当您到达元素的滚动位置时,粘性元素将停留在页面顶部(top: 0)。</p>
  <p>向上滚动以消除粘性。</p>
  <p>一些启用滚动的文本.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>一些启用滚动的文本.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

重叠元素

在对元素进行定位时,它们可以与其他元素重叠。

z-index 属性指定元素的堆栈顺序(哪个元素应放置在其他元素的前面或后面)。

元素可以设置正或负的堆叠顺序:

这是一个标题

由于图像的 z-index 为 -1,所以它位于文本后面。

示例代码:

img {
  position: absolute;
  left: 0px;
  top: 0px;
  z-index: -1;
}

 

完整实例【亲自试一试】:

<!DOCTYPE html>
<html>
<head>
<style>
img {
  position: absolute;
  left: 0px;
  top: 0px;
  z-index: -1;
}
</style>
</head>
<body>
<h1>这是标题</h1>
<img src="/skin/news/images/logo1.png" width="188" height="267">
<p>由于图像的 z-index 为 -1,它将被置于文本之后。</p>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

具有较高堆叠顺序的元素始终位于具有较低堆叠顺序的元素之前。

注意:如果两个定位的元素重叠而未指定 z-index,则位于 HTML 代码中最后的元素将显示在顶部。

定位图像中的文本

如何在图片上放置文字:

示例代码:

phpcodeweb Logo
Bottom Left
Top Left
Top Right
Bottom Right
Centered

亲自试一试:

 

完整实例【Top Left】:

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  position: relative;
}
.topleft {
  position: absolute;
  top: 8px;
  left: 16px;
  font-size: 18px;
}
img {
  width: 100%;
  height: auto;
  opacity: 0.3;
}
</style>
</head>
<body>
<h1>图像文本</h1>
<p>在图像左上角添加一些文本:</p>
<div class="container">
  <img src="/i/logo/w3logo-2.png" alt="phpcodeweb" width="800" height="450">
  <div class="topleft">Top Left</div>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

完整实例【Top Right】:

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  position: relative;
}
.topright {
  position: absolute;
  top: 8px;
  right: 16px;
  font-size: 18px;
}
img {
  width: 100%;
  height: auto;
  opacity: 0.3;
}
</style>
</head>
<body>
<h1>图像文本</h1>
<p>在图像右上角添加一些文本:</p>
<div class="container">
  <img src="/i/logo/w3logo-2.png" alt="phpcodeweb" width="800" height="450">
  <div class="topright">Top Right</div>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

完整实例【Bottom Left】:

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  position: relative;
}
.bottomleft {
  position: absolute;
  bottom: 8px;
  left: 16px;
  font-size: 18px;
}
img {
  width: 100%;
  height: auto;
  opacity: 0.3;
}
</style>
</head>
<body>
<h1>图像文本</h1>
<p>在图像左下角添加一些文本:</p>
<div class="container">
  <img src="/i/logo/w3logo-2.png" alt="phpcodeweb" width="800" height="450">
  <div class="bottomleft">Bottom Left</div>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

完整实例【Bottom Right】:

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  position: relative;
}
.bottomright {
  position: absolute;
  bottom: 8px;
  right: 16px;
  font-size: 18px;
}
img {
  width: 100%;
  height: auto;
  opacity: 0.3;
}
</style>
</head>
<body>
<h1>图像文本</h1>
<p>在图像右下角添加一些文本:</p>
<div class="container">
  <img src="/i/logo/w3logo-2.png" alt="phpcodeweb" width="800" height="450">
  <div class="bottomright">Bottom Right</div>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

完整实例【Centered】:

<!DOCTYPE html>
<html>
<head>
<style>
.container {
  position: relative;
}
.center {
  position: absolute;
  top: 50%;
  width: 100%;
  text-align: center;
  font-size: 18px;
}
img {
  width: 100%;
  height: auto;
  opacity: 0.3;
}
</style>
</head>
<body>
<h1>图像文本</h1>
<p>居中图像中的文本:</p>
<div class="container">
  <img src="/i/logo/w3logo-2.png" alt="phpcodeweb" width="800" height="450">
  <div class="center">Centered</div>
</div>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

 

更多实例

完整实例【设置元素的形状】:

<!DOCTYPE html>
<html>
<head>
<style>
img {
  position: absolute;
  clip: rect(0px,60px,200px,0px);
}
</style>
</head>
<body>
<img src="/skin/news/images/logo1.png" width="188" height="267">
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

本例演示如何设置元素的形状。元素被裁剪为这种形状并显示。

所有 CSS 定位属性

属性 描述
bottom 设置定位框的底部外边距边缘。
clip 剪裁绝对定位的元素。
left 设置定位框的左侧外边距边缘。
position 规定元素的定位类型。
right 设置定位框的右侧外边距边缘。
top 设置定位框的顶部外边距边缘。
z-index 设置元素的堆叠顺序。

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024