如何将按钮绝对定位并渲染到新行?

cssweb developmentfront end technology更新于 2025/2/13 2:20:17

如果要控制元素在网页中的定位方式,我们必须使用 position CSS 属性。定义元素在文档中位置的属性至关重要,包括其 top、left、bottom 和 right 属性,而position 是一个简写属性,可用于设置所有这四个属性。

position 属性可以使用的可能值如下所示 -

  • Static - 元素按照文档的自然流向放置。top、right、bottom、left 或 z-index 属性不会产生任何影响。这是预设选项。

  • Relative - 元素按照文档的自然流向放置,其与自身的偏移量由 top、right、bottom 和 left 的值决定。页面布局中分配给元素的空间与位置为静态时的空间相同,因为偏移量不会影响任何其他元素的位置。

    当 z-index 的值不是 auto 时,此值会建立一个新的堆叠上下文。它将如何影响 table-*-group、row、column、cell 和 table-caption 元素目前仍未定义。

  • 绝对- 该元素将从典型的文档流中移除,页面布局中不会为其分配空间。如果有,则根据其祖先元素的位置进行放置;如果没有,则根据第一个包含的块的位置进行放置。top、right、bottom 和 left 值定义了它的最终位置。

    当 z-index 的值不是 auto 时,此值会建立一个新的堆叠上下文。绝对定位可防止框的边距与其他边距重叠。

  • 固定− 该元素从典型的文档流中移除,页面布局中不为其留出空间。除非其祖先元素的 transform、perspective 或 filter 属性设置为 none 以外的值(参见 CSS Transforms 规范),或者将 will-change 属性设置为 transform(在这种情况下,该祖先元素将充当包含块),否则该元素将相对于视口建立的初始包含块进行定位。(请注意,浏览器之间的透视和过滤器差异可能会导致生成封闭块。)top、right、bottom 和 left 值定义了其最终位置。

  • 粘性− 该元素根据文档的自然流进行定位,并根据 top、right、bottom 和 left 的值,相对于其最近的滚动祖先元素和包含块(最近的块级祖先元素)进行偏移,包括与表格相关的元素。其他元素的位置不受偏移量影响。

    此值始终会创建一个新的堆叠上下文。需要注意的是,粘性元素会"粘"到具有"滚动机制"(当 overflow 为隐藏、滚动、自动或覆盖时产生)的最近祖先元素上,即使该祖先元素并非真正滚动的最近祖先元素。

相对定位和绝对定位元素

相对定位元素的计算位置为"relative",而绝对定位元素的计算位置为"absolute"或"fixed"。

相对定位示例

以下是使用相对定位的示例代码。

<!DOCTYPE html>
<html>
<head>
   <style>
      .relativePositioning {
         position: relative;
         left: 50px;
         border: 2px solid red;
      }
   </style>
</head>
<body>
   <h2>Using relative positioning in CSS</h2>
   <p>This is a sample paragraph onto which relative positioning is being applied.</p>
   <div class="relativePositioning">This part of the content has position : relative</div>
</body>
</html>

绝对定位示例

以下是使用绝对定位的示例代码。

<!DOCTYPE html>
<html>
<head>
   <style>
      .relativePositioning {
         position: relative;
         width: 500px;
         height: 250px;
         border: 2px solid red;
      }
      .absolutePositioning {
         position: absolute;
         top: 100px;
         right: 0;
         width: 300px;
         height: 150px;
         border: 2px solid red;
      }
   </style>
</head>
<body>
   <h2>Example of using absolute positioning</h2>
   <p>Lorem ipsum dolor sit amet consectetur   adipisicing elit. Nesciunt, possimus.</p>
   <div class="relativePositioning">
      This is the container element with position : relative
      <div class="absolutePositioning">This is an example of absolute positioning</div>
   </div>
</body>
</html>

使用绝对定位在新行中渲染按钮

现在我们了解了定位的工作原理以及如何在 CSS 中使用绝对定位。我们将运用所学知识来解决当前的问题。

示例

下面是一个在 CSS 中使用绝对定位在新行中渲染按钮的示例。

<!DOCTYPE html>
<html lang="en">
<head>
</head>
   <style>
      .outerBox {
         position: relative;
      }
      .btn-pri {
         color: #fff;
         padding: 0.5px 7% 0.5px 5px;
         height: 45px;
         display: inline-block;
         cursor: pointer;
         background: green;
         border: 2px solid #ccc;
      }
      .btn-txt {
         margin-top: 6px;
         margin-bottom: 6px;
      }
      .btn-pri-2 {
         position: absolute;
         left: 1px;
         top: 53px;
      }
   </style>
<body>
   <div class="outerBox">
      <a class="btn-pri btn-pri-1">
         <h5 class="btn-txt">Lorem ipsum dolor sit amet.</h5>
      </a>
      <a class="btn-pri btn-pri-2">
         <h5 class="btn-txt">Lorem ipsum dolor sit amet.</h5>
      </a>
   </div>
</body>
</html>

结论

总而言之,绝对定位元素允许您通过指定按钮在页面上的精确位置,将按钮渲染到新行中。您可以通过将元素的"position"属性设置为"absolute",然后为 top、left、right 或 bottom 属性提供值来实现,这些值指示了您想要放置按钮的确切位置。正确使用绝对定位可以帮助您以最少的精力创建整洁的布局。


相关文章


有用资源