Tips for placing a div directly on top of another div

I need to position a div absolutely in the center of the screen and have it appear in front of another div. However, when the user scrolls down and the div reaches the footer, it should stop floating and be positioned just above the footer. I've included some example code below that isn't working as intended:

.main {
background: blue;
height: 2000px;
}
.footer {
background: red;
height: 400px;
}
.float {
background: green;
position:fixed;
height: 30px;
width: 100px;
bottom: 50px;
left: 300px;
}
<div class="main">Main</div>
<div class="float">Button</div>
<div class="footer">Footer</div>

Below are images illustrating how I expect the element to behave:

Positioned just over the main: https://i.sstatic.net/91Xhg.png

The desired state when it reaches the footer: https://i.sstatic.net/KjeQQ.png

An ideal solution using only CSS is preferred.

Answer №1

If you want an element to stick on the page as it scrolls, try using position: sticky instead of position: fixed.

position: sticky:

With this property, the element is positioned based on the normal flow of the document but is offset relative to its nearest scrolling ancestor and containing block. This positioning does not affect other elements on the page.

position: fixed:

On the other hand, when an element has a position of fixed, it is taken out of the normal document flow and positioned relative to the initial containing block (usually the viewport). The top, right, bottom, and left values determine its final position.

Take a look at the following example:

.main {
background: blue;
height: 2000px;
}
.footer {
background: red;
height: 400px;
}
.float {
background: green;
position: sticky;
height: 30px;
width: 100px;
bottom: 50px;
left: 300px;
}
<div class="main">Main</div>
<div class="float">Button</div>
<div class="footer">Footer</div>

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

How to Align Video Alongside Image in HTML without Using CSS

Is there a way to place a video on the left and an image on the right of the same line in HTML without relying on CSS? ...

What is the best way to modify the CSS class of a particular textbox within a gridview using C# in ASP.NET?

I need assistance with removing and adding a CssClass to a specific textbox within a gridview. I have attempted the following code but it does not update the css for the textbox. Here is the CSS in my .aspx page: <style type="text/css"> .erroram ...

Identifying hyperlinks in plain text without anchor elements

If a user inputs text into the text box, saves it, and later wants to add more text, they can edit the existing text and save the changes if necessary. When a user enters text with links, any URLs are automatically converted into hyperlinks that open in a ...

What is the best way to generate an HTML snapshot using C#?

I'm currently working on a page with Ajax functionality and I'm interested in making it SEO crawlable. After reviewing Google's guidelines at https://developers.google.com/webmasters/ajax-crawling, I learned that I need to use "#!" to create ...

Making a Django template recognize the load tag when inheriting from a base template involves ensuring proper syntax and file

My current setup involves a file called base.html. {% load static from staticfiles %} <html> <title>COOL| {% block title %} Sometitle {% endblock %}</title> <body> <!--- BEGIN INSERT CONTENT FOR OTHER PAGE HERE--> ...

What is the process for retrieving content from an HTML page and displaying it using Python?

I am looking to retrieve the current 'max' forecast value from the following page: and display that value using Python. Could someone guide me in accomplishing this task? (I am new to programming so apologize if this is a basic question!) Plea ...

Ways to stop a user from being deleted while logged in on a JSP page or HTML

I have a JSP code with a delete button, but I want to exclude the current login user from deletion. This is my jsp code with delete button. I dont want to delete current login user. <table border="1"> <thead style="background: # ...

Unable to bypass YouTube advertisement

I am currently experimenting with using nodejs puppeteer to test if I can bypass the ad on Youtube. Although this is just for testing purposes, I am facing some challenges with getting it to work as expected. I have implemented a while loop to search for ...

Ensuring proper alignment of images on image slider specifically for Chrome browser

While updating a website, I encountered an issue with the image slider displaying two images side-by-side. However, in Chrome, the images were not aligned properly as shown here: The website utilizes PHP and HTML to fetch data from a database and display ...

Tips for increasing the spacing between inline elements without resorting to using  

I am facing a challenge of adding space between two input fields without resorting to the use of &nbsp. I attempted to insert {' '} but saw no change in the render. This is how they appear when rendered: return ( <div cl ...

Adjust SVG color transition height based on CSS class characteristics

Utilizing a combination of SVG and CSS code, I am attempting to animate the fill level of an SVG shape. .st0 { fill: none; stroke: #000000; stroke-width: 4; stroke-miterlimit: 5; } .st1 { fill: none; stroke: #000000; stroke-width: 3; st ...

What is the best method for transforming a stream into a file with AngularJS?

Currently, I have a server returning a file stream (StreamingOutput). My goal is to convert this file stream into an actual file using AngularJS, javascript, JQuery, or any other relevant libraries. I am looking to display the file in a <div> or ano ...

JavaScript enables the deletion of a class

In HTML 2, I am able to show different statements based on the scenario. These statements are styled using the Bootstrap alert class. The goal is to ensure that when new data is sent, any old communication disappears without causing overload on the page. ...

Using Data URIs can negatively impact the loading speed of a webpage

I created a custom script to replace all inline images with data URIs in order to minimize http requests and speed up loading times on mobile devices. Surprisingly, I noticed a decrease in loading speed. Could it be because the HTML file was larger (aroun ...

What is the method for achieving the equivalent effect of "background-size: cover" on an <img>?

I am looking to ensure that an <img> has the smallest possible size without any empty spaces inside a div, while also being perfectly centered both horizontally and vertically. The image size may vary. To better illustrate, here is an example: http: ...

Flexbox parent div experiencing overflow due to horizontal margins protruding beyond the boundaries

Experiencing unexpected margin behavior with flex and seeking help for clarification. Here is a simple HTML structure I am using: <div className="dashboard"> <div className="dashboard__inner-container">Inner Container</div> </di ...

Trouble with getElementsByTagName function

Utilizing the NODE JS module, I have created an HTTP server that responds with a page containing JavaScript code to embed a webpage within an <iframe>. Within this <iframe>, I am trying to access its elements using the getElementsByTagName meth ...

Issues with navigating the HTML menu are preventing users from properly accessing

Below is the structure of my menu in HTML: <div id=navigation> <ul id=nav> <li> <asp:HyperLink ID=HyperLink1 runat=server NavigateUrl=~/Home.aspx>Home</asp:HyperLink> ...

HTML5 - Transforming your webpages into interactive flipbooks

Is there a way to achieve a page-turn effect when loading external HTML pages into a div? I am interested in utilizing CSS3, HTML5 or jQuery for this purpose. ...

Creating a Blob in JavaScript to Save an Image File (Using the HTML5 File API)

Currently, I am working on a project involving a Chrome extension that involves copying user-selected files into the extension's filesystem. Everything seems to be functioning without any errors, but when attempting to view the image, it appears broke ...