Position elements absolutely inside a scrollable container

I am looking to create a scrollable element with an absolutely positioned element inside, along with a larger element for scrolling.

.container{
  width: 200px;
  height: 200px;
  position: relative;
  overflow: scroll;
  border: 1px solid red;
}
.content{
  height: 300px;
  background: green;
}
.wtf{
  top: 0px;
  position: absolute;
  height: 10px;
  width: 100%;
  background: red;
}

<div class="container">
  <div class="content">
  </div>
  <div class="wtf"></div>
</div>

http://codepen.io/anon/pen/pvKwvZ In this scenario, I aim to ensure the red element remains within the green square even after scrolling to the bottom.

Answer №1

The reason why the .wtf-Element moves with the .container-Element is because your .container has been set to position: relative. This makes the .container the point of reference for the .wtf-Element.

If you want the .wtf-Element to stay fixed, you can either remove the position: relative from the .container (or replace it with the default position: static), or...

Alternatively, you could set the .wtf position to fixed. This would then position the .wtf-Element in relation to the "outer html", rather than relative to the .container.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Changing the selection in the Angular Mat-Select dropdown causes the dropdown's position to shift

Issue with dropdown position: The dropdown should display below the select element, but when selecting the second value, it appears on top of the select element. I have removed any relevant CSS to troubleshoot, but the problem persists. See screenshots for ...

"Enhancing user experience by dynamically adding a drop-down menu with jQuery's .append functionality

Looking to include another drop-down selector every time the button is pressed. I am currently using JQUERY to insert the new dropdown field, but I am facing issues getting it added successfully. The code provided below is not functioning as expected: jsf ...

Positioning a .png file in the center of a div while giving it a high z-index

Is it possible to ensure that a .png image is always centered in a div, regardless of the device size, and appears above another .png image within the same div? Below is the HTML code snippet: <div class="col-sm-6"> <div id="spi ...

Issue with concealing floated elements within a container div

I am currently facing an issue with trying to hide floated divs within a parent div, but unfortunately my code is not working as expected. Here is the code snippet: div.scrollarea { overflow: scroll; width: 400px; ...

Easy Steps to Build a Line Item Grid using AngularJS

I have a visual display that was initially designed using React to manage line items. Now, I am in need of recreating the same functionality utilizing AngularJS and HTML. Any assistance with implementing this with AngularJS would be greatly appreciated. T ...

Quick way to include id="" and class="" attributes to HTML elements using Sublime Text 2

Is there a way to configure Sublime Text 2 where inputting a . (dot) automatically generates class=" " and adding a # (hash) creates id=" " while typing an HTML opening tag? ...

Redirecting the parent window in ASP.NET using an iFrame

Can a response.redirect be used within an iFrame to redirect the entire page, making the destination page viewable in full screen instead of confined within the iFrame? Here is the current code behind: public partial class ServerResult : System.Web.UI.Pa ...

Provide rationale for elements connected to a fixed space (varied width) arrangement

I am facing a situation where I have a container with multiple elements inside it. These elements need to be justified and spaced out by a fixed amount (e.g. 20px). This means that the width of each element should adjust accordingly. For instance, conside ...

Function: get the next item from an HTML POST form and register it as undefined when submitted

Encountering a problem with my form - trying to enter a website address/keyword along with a selected filter. The filters are dynamically updated from the database, hence only one option is currently displayed <div class="add-website-content"& ...

Insert within a canvas

Although there is a previous inquiry on this topic, it appears to be outdated. Despite searching through dartlang's resources and conducting online research, I have yet to find a solution to the issue at hand. document.onPaste.listen((e) { ...

Look up HTML div tags and showcase the findings on a separate webpage

I am looking to search through multiple HTML files from a different page. I want to find text within all the divs that have a specific id, and display those divs on the search results page if they contain the matched search term. Here is an example of how ...

What is the best way to ensure a variable-width column stays perfectly centered within a three-column layout?

I need help with aligning text in a toolbar-like control. The left side contains variable-size text, the center has a small block of text, and the right side also has text. My goal is to always have the center text centered without knowing its width in ad ...

Is it possible to host three separate web pages within a single Firebase project on Firebase Hosting?

Imagine I have set up a project called ExampleApp on Firebase. I've designed a page with a form for users to submit data, hosted by Firebase. The Cloud Firestore database is storing this submitted data when the user completes the form and hits submit ...

Is there a way to modify just the homepage url of the logo on a WordPress website using the OceanWP theme?

My website, abc.com, is set up with Angular for the homepage and WordPress for the blogs. The WordPress site is located in a subfolder within abc.com. You can see the file structure in the image below. I am now looking to change only the homepage link on ...

Unable to override Bootstrap margin tag with CSS in @media query

Whenever I try to adjust the margin top (mt-5) of a h1 heading tag in my HTML using an @media query, it seems that the other conditions are working fine except for the margin. Despite having my CSS sheet linked after the Bootstrap one. I attempted adding ...

Expanding the background further than the parent's width using HTML and CSS

I am looking to extend the background color or image to mimic the appearance of the example at the bottom in this jsfiddle Same code but showcased here: Example1: <div class="fixed_width"> <div class="header">Header</div> <div ...

Update cursor when hovering over a button

What can I do to make the cursor change to a hand when hovering over my submit button? Currently, the cursor remains as the standard arrow and does not switch when placed on the button. The code for my button is: <button>Submit</button> ...

Placing images in a webpage (HTML)

While everything works fine with the CSS code for hovering effects on these images, I am facing a challenge in re-arranging their position on the page. Currently, they are stacked vertically on top of each other. How can I make them appear side by side? ...

The CSS scale transformation in iOS Safari causes the page to zoom out

I am attempting to implement a text snippet that will "zoom and fade in" upon the page loading. In order to do this, I am creating a div containing the text and applying an immediate transform to it: .whatIwantZoomed { opacity:0; /* Add Vendor Pref ...

How to Condense an Element Using Position: Absolute

https://i.sstatic.net/GMUss.png I'm attempting to create functionality where the "Open Chat" button displays an Absolute positioned div, then hides it when clicked again. I experimented with using the react-collapse component, but encountered issues ...