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
Release: 2024-12-07 07:53:13
Original
791 people have browsed it

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>
Copy after login

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 */
}
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template