CSS 位置

CSS 位置

CSS 位置属性

CSS 属性 position (位置) 有助于在网页中操作元素的位置。属性 top (上方) , bottom (下方) , right (右侧) 和 left (左侧) 用于控制元素在页面上的确切位置。它们指定了一个元素相对于其边缘的偏移量。

属性 position (位置) 可用于创建浮动元素、浮动侧边栏和其他交互功能。

以下是可能的值:

  • static (静态)− 元素的位置根据页面的默认或正常流布局确定。因此,如果我们设置 left/right/top/bottom/z-index,对该元素没有任何影响。

  • relative (相对)− 元素的原始位置与页面的正常流布局相同,就像 static (静态) 值一样。但是,现在 left/right/top/bottom/z-index 将会起作用。定位属性将把元素从原始位置推向该方向。

  • absolute (绝对)− 元素完全从文档流中移除。然后,它相对于其包含块进行定位,并使用侧偏移属性设置其边缘。绝对定位的元素可能会重叠其他元素,或被其它元素重叠。

  • fixed (固定)− 元素的固定定位与绝对定位类似,区别在于固定元素的包含块始终是视口。在这里,元素完全从文档流中移除,并且不与文档中的任何部分相对位置。

  • sticky (粘性)− 元素会固定在最近具有“滚动机制”的已定位祖先的顶部。

CSS 位置 – static

当您使用 CSSposition: static (静态) 属性时,元素将按照文档流的正常布局进行定位。left、right、top、bottom 和 z-index 属性将不会影响元素。这是默认值。

示例

以下是一个示例 −

<html>
<head>
<style>
   .normal-box {
      display: inline-block;
      background-color: #f2c3ee;
      border: 1px solid #000000;
      padding: 10px;
      margin-bottom: 20px;
      width: 300px;
      height: 100px;
   }
   .static-box {
      display: inline-block;
      position: static;
      background-color: #bbedbb;
      border: 1px solid #000000;
      padding: 10px;
      width: 300px;
      height: 100px;
   }
</style>
</head>
<body>
   <div class="normal-box">
      <h2>Normal Box</h2>
      <p>This is a normal box.</p>
   </div>

   <div class="static-box">
      <h2>Position: static</h2>
      <p>This is a box with static position.</p>
   </div>
</body>
</html>

CSS 位置 – relative

CSS position: relative 属性将元素相对于其在页面中的原始位置进行定位。您可以使用 left、right、top 和 bottom 属性来移动元素,但它仍然会在文档流中占据空间。

示例

下面是一个示例:

<html>
<head>
<style>
   .normal-box {
      display: inline-block;
      background-color: #f2c3ee;
      border: 1px solid #000000;
      padding: 10px;
      margin-bottom: 20px;
      width: 300px;
      height: 100px;
   }
   .relative-box {
      display: inline-block;
      position: relative;
      left: 30px;
      background-color: #bbedbb;
      border: 1px solid #000000;
      padding: 10px;
      width: 300px;
      height: 100px;
   }
</style>
</head>
<body>
   <div class="normal-box">
      <h2>Normal Box</h2>
      <p>This is a normal box.</p>
   </div>
   <div class="relative-box">
      <h2>Position: relative</h2>
      <p>This is a box with relative position.</p>
   </div>
</body>
</html>

CSS 位置 – absolute

具有 position: absolute 的元素被从文档流中取出,并相对于其最近的已定位祖先元素(如果有)。如果没有已定位的祖先元素,则元素相对于视口定位。

您可以使用 top、right、bottom 和 left 属性来指定元素相对于其包含块的位置。

示例

这是一个示例 –

<html >
<head>
<style>
   .normal-box {
      background-color: #f2c3ee;
      border: 1px solid #333;
      padding: 10px;
      margin-bottom: 20px;
      width: 350px;
      height: 100px;
   }
   .absolute-box {
      background-color: #bbedbb;
      border: 1px solid #333;
      padding: 10px;
      position: relative;
      width: 300px;
      height: 100px;
      left: 20px;
      bottom: 20px;
   }
</style>
</head>
<body>
   <div class="normal-box">
      <h2>Normal Box</h2>
      <p>This is a Noraml box.</p>
      <div class="absolute-box">
         <h2>Position: Absolute</h2>
         <p>This is a box with absolute position.</p>
      </div>
   </div>
</body>
</html>

CSS 位置 – fixed

要使元素在用户滚动时保持在屏幕上的相同位置,可以将 position 属性设置为 fixed 。然后,可以使用left,right,top和bottom属性来定位元素所需的位置。

示例

这是一个例子−

<html>
<head>
<style>
   .position_container {
      width: 400px;
      height: 200px;
      background-color: #f2c3ee;
      overflow: auto;
      padding: 5px;
   }
   .fixed-position {
      position: fixed;
      top: 15px;
      left: 60px;
      padding: 5px;
      background-color: #bbedbb;
      text-align: center;
   }
</style>
</head>
<body>
   <div class="position_container">
      <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
      <p class="fixed-position">Tutorialspoint CSS Position Fixed</p>
      <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
      <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
      <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
   </div>
</body>
</html>

CSS 位置 – sticky

您可以将position属性设置为sticky,从而创建一个元素,当用户滚动页面时,该元素会固定在视口的顶部。

position: sticky 属性是 position: relativeposition: fixed 属性的组合。

示例

这是一个示例:

<html>
<head>
<style>
   .position_container {
      width: 400px;
      height: 200px;
      background-color: #f2c3ee;
      overflow: auto;
      padding: 5px;
   }
   .sticky-position {
      position: sticky;
      top: 15px;
      padding: 5px;
      background-color: #bbedbb;
      text-align: center;
   }
</style>
</head>
<body>
   <div class="position_container">
      <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
      "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
      <p class="sticky-position">Tutorialspoint CSS Position Sticky</p>
      <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
      <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
      <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
      <p>"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
   </div>
</body>
</html>

在图片中定位CSS文本

在下面的例子中,您可以使用 position: absolute 属性在不同的方向中定位文本。文本元素被定位在左上角、右上角、左下角和右下角。

示例

这是一个例子-

<html>
<head>
<style>
   .container {
      position: relative;
      border: 2px solid #ef2c2c;
   }
   .center {
      position: absolute;
      top: 45%;
      width: 100%;
      text-align: center;
   }
   .top-left {
      position: absolute;
      top: 12px;
      left: 30px;
   }
   .top-right {
      position: absolute;
      top: 12px;
      right: 30px;
   }
   .bottom-left {
      position: absolute;
      bottom: 12px;
      left: 30px;
   }
   .bottom-right {
      position: absolute;
      bottom: 12px;
      right: 30px;
   }
   img {
      width: 100%;
      opacity: 0.3;
   }
</style>
</head>
<body>
   <div class="container">
      <img src="images/red-flower.jpg" alt="abc" width="1000px" height="350px">
      <h3 class="center">Text at Centered</h3>
      <h3 class="top-left">Text at Top Left</h3>
      <h3 class="top-right">Text at Top Right</h3>
      <h3 class="bottom-left">Text at Bottom Left</h3>
      <h3 class="bottom-right">Text at Bottom Right</h3>
   </div>
</body>
</html>

CSS位置 – 相关属性

以下是所有与 位置 相关的CSS属性的列表:

属性 描述
bottom 与 position 属性一起使用,用于定位元素的底部边缘。
clip 设置元素的剪辑遮罩。
left 与 position 属性一起使用,用于定位元素的左边缘。
overflow 决定溢出内容的渲染方式。
position 设置元素的定位模型。
right 与 position 属性一起使用,用于定位元素的右边缘。
top 与 position 属性一起使用,用于定位元素的顶部边缘。
vertical-align 设置元素的垂直定位。
z-index 设置当前元素的渲染层级。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程