Home >Web Front-end >CSS Tutorial >How Can I Absolutely Position an Element Horizontally and Fixed Vertically?

How Can I Absolutely Position an Element Horizontally and Fixed Vertically?

Linda Hamilton
Linda HamiltonOriginal
2024-12-07 07:53:13783browse

How Can I Absolutely Position an Element Horizontally and Fixed Vertically?

Position Element Absolutely Horizontally and Fixed Vertically

The goal is to create a button that maintains a fixed distance from the right edge of another element regardless of the viewport size, while scrolling vertically with the window.

Solution

To achieve this, we can use a combination of CSS positioning properties:

<div class="container">
  <div class="button"></div>
</div>

CSS:

.container {
  position: relative;
}

.button {
  position: absolute;
  right: 10px;  /* Distance from the right edge of the container */
  top: 100px;   /* Distance from the top of the viewport */
}

By setting the position of .button to absolute, we make it independent of the normal document flow. The right property specifies the horizontal distance from the right edge of the container, while top defines the vertical distance from the top of the viewport.

Note: This solution creates a fixed element that is horizontally positioned relative to its container, meeting the condition of "fixed vertically, absolute horizontally" in the question.

The above is the detailed content of How Can I Absolutely Position an Element Horizontally and Fixed Vertically?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn