首页 > web前端 > css教程 > 正文

CSS 定位 – 绝对、相对、固定和粘性。

WBOY
发布: 2024-09-10 18:00:33
原创
327 人浏览过

CSS Positioning – Absolute, Relative, Fixed, and Sticky.

第 11 讲:CSS 定位 – 绝对、相对、固定和粘性

欢迎来到《从基础到辉煌》课程第十一讲。在本次讲座中,我们将探讨CSS定位的不同类型:相对绝对固定粘性。了解定位可以让您控制元素在页面上的显示位置以及用户与内容交互时元素的行为方式。


1.了解位置属性

position 属性指定元素在文档中的位置。它可以采用以下值:

  • 静态:默认值。元素根据正常文档流定位。
  • 相对:元素相对于其正常位置定位。
  • 绝对:元素相对于其最近的定位祖先或初始包含块进行定位。
  • 固定:元素相对于浏览器窗口定位,并在滚动时保持在相同位置。
  • 粘性:元素被视为相对元素,直到达到阈值(例如滚动位置),然后它变得固定。

2.相对定位

具有position:relative的元素相对于其原始(静态)位置定位。它保留在文档流中,这意味着其他元素仍会考虑它。

  • 示例:使用相对定位来移动元素。
  .box {
    position: relative;
    top: 20px; /* Moves the box 20px down from its normal position */
    left: 30px; /* Moves the box 30px to the right */
  }
登录后复制

在此示例中,元素从原始位置向下移动 20px,向右移动 30px,但保留其在文档流中的空间。


3.绝对定位

位置为绝对的元素将从文档流中删除,并相对于其最近的定位祖先(即位置不是静态的祖先)进行定位。

  • 示例:将元素绝对定位在容器内。
  .container {
    position: relative; /* This container is the reference for the absolute positioning */
    width: 300px;
    height: 200px;
    background-color: #f4f4f4;
  }

  .box {
    position: absolute;
    top: 50px;
    right: 20px;
    background-color: #333;
    color: white;
    padding: 10px;
  }
登录后复制

在此示例中:

  • .box 绝对位于距离顶部 50 像素、距离右侧 20 像素 .container 元素内部。
  • .container 具有position:relative,使其成为.box 的定位参考。

4.固定定位

位置为固定的元素相对于浏览器窗口定位,无论页面如何滚动。

  • 示例:创建固定导航栏。
  .navbar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background-color: #333;
    color: white;
    padding: 15px;
    text-align: center;
  }
登录后复制

在此示例中:

  • .navbar 位于视口顶部,即使页面滚动也保持固定。

5.粘性定位

具有position: Sticky 的元素被视为相对元素,直到用户滚动超过定义的阈值,此时它变得固定。

  • 示例:滚动后保留在顶部的粘性标题。
  .header {
    position: sticky;
    top: 0;
    background-color: #333;
    color: white;
    padding: 10px;
  }
登录后复制

在此示例中:

  • .header 的行为就像普通元素一样,直到它到达页面顶部。之后,它会粘在顶部并在用户滚动时保持可见。

6. Z-索引

当元素被定位(相对、绝对、固定或粘性)时,您可以使用 z-index 属性控制它们的堆叠顺序。较高的 z-index 值使元素出现在较低的元素上方。

  • 示例:控制堆叠顺序。
  .box1 {
    position: absolute;
    top: 50px;
    left: 50px;
    z-index: 1; /* Lower z-index */
    background-color: #f4f4f4;
    padding: 20px;
  }

  .box2 {
    position: absolute;
    top: 80px;
    left: 80px;
    z-index: 2; /* Higher z-index */
    background-color: #333;
    color: white;
    padding: 20px;
  }
登录后复制

在此示例中:

  • 由于 z-index 值较高,.box2 将出现在 .box1 上方。

7.结合定位技术

您可以组合定位值来创建高级布局。

  • 示例:修复了具有相对内容区域的侧边栏。
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: 200px;
    height: 100vh;
    background-color: #333;
    color: white;
    padding: 20px;
  }

  .content {
    position: relative;
    margin-left: 220px; /* Account for the fixed sidebar */
    padding: 20px;
  }
登录后复制

在此布局中:

  • Le .sidebar est fixé à gauche de la page et reste visible lors du défilement.
  • La zone .content est positionnée de manière relative et ajuste sa marge pour tenir compte de la barre latérale.

Exercice pratique

  1. Créez une page Web avec un en-tête et un pied de page fixes, et utilisez un positionnement relatif et absolu pour le contenu.
  2. Ajoutez une barre latérale collante qui se fixe lors du défilement.

Prochaine étape : Dans la prochaine conférence, nous plongerons dans les Transformations et animations CSS, où vous apprendrez à animer facilement vos éléments Web. Préparez-vous à rendre vos créations dynamiques et interactives !


suivez-moi sur LinkedIn
Ridoy Hasan

以上是CSS 定位 – 绝对、相对、固定和粘性。的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!