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

How Can I Position an Element Fixed Vertically and Absolute Horizontally While Scrolling?

Linda Hamilton
Linda HamiltonOriginal
2024-12-07 08:04:16757browse

How Can I Position an Element Fixed Vertically and Absolute Horizontally While Scrolling?

Position Element Fixed Vertically, Absolute Horizontally

The user sought a solution for a button that remained a set distance from the right edge of a div, regardless of viewport size, while scrolling with the window. This can be achieved using the following strategy:

HTML Structure

<div class="inflow">
  <div class="positioner"> <!-- May not be needed -->
    <div class="fixed"></div>
  </div>
</div>

CSS Styling

div.inflow {
  width: 200px;
  height: 1000px;
  border: 1px solid blue;
  float: right;
  position: relative;
  margin-right: 100px;
}

div.fixed {
  width: 80px;
  border: 1px solid red;
  height: 100px;
  position: fixed;
  top: 60px;
  margin-left: 15px;
}

Key Points

  • Fixed Vertical Position: The position: fixed on div.fixed ensures it scrolls with the window.
  • Absolute Horizontal Position: The fixed horizontal position is not set directly. Instead, it relies on the wrapper divs (div.inflow and optionally div.positioner) to define its position relative to the parent div.
  • Dynamic Adjustment: As the container div's size changes, the button maintains its desired distance from the right edge.
  • Overflow Handling: The fixed position element is exempt from the overflow context of its container div, even with the overflow: hidden property set.

The above is the detailed content of How Can I Position an Element Fixed Vertically and Absolute Horizontally While Scrolling?. 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