Tips for using CSS in React to keep a component fixed at the bottom until the user reaches the footer

Working on a project with React and CSS in JS using Emotion. I am facing an issue where I need a button to stick at the bottom of the viewport on mobile, until the user scrolls to the footer. Once they reach the footer, the button should then be fixed at the top of the footer.

This button is a reusable component that can be easily added wherever needed and controlled with media queries for show/hide functionality.

Initially, I tried using:

position: fixed;
bottom: 0;

This solution worked well until the footer came into view. I require the button to remain at the top of the footer when reached.

I also attempted:

position: sticky;
top: (some amount of px);

However, position sticky did not have the desired effect in this scenario. It seems to work fine on parent components like the header, but not on child components.

If anyone knows how to make position: sticky work within a child component or has any alternative approaches, please share them with me. We are also utilizing Material UI, so if there's a lesser-known feature within MUI that could solve this issue, that would also be helpful. Thank you!

Answer №1

Though it may seem a bit complicated, if the sticky elements have parent siblings, it can function properly.

Here is a simple proof of concept:

.content {
  margin-top: -50px;
  background: grey;
  height: 1000px;
}

.btn {
  position: sticky;
  width: 50px;
  height: 50px;
  background: red;
  top: calc(100vh - 55px);
  z-index: 10;
}

.footer {
  position: sticky;
  width: 100%;
  height: 40px;
  background: blue;
  top: 0;
}
<div class="container">

  <div>
    <div class="btn"></div>
    <div class="content"></div>
  </div>


  <div>
    <div class="footer"></div>
  </div>

</div>

Answer №2

Utilizing react-use-scroll can allow you to track the user's scroll position and dynamically update your styles using emotion based on a specified scroll threshold.

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

React: Communicate between components and update shared variables on event-triggered actions

As someone fairly new to working with React, I think I may be using the wrong approach to tackle a simple issue. Picture a parent component that contains several child components: OverviewComponent (parent) DetailsComponent (child) EditComponent (child) ...

Change the color of unordered list items only at the same level

Hey there! I'm working with this HTML code: <ul id="nav" class="nav-1"> <li><a href="#">Loans</a></li> <li><a href="#">Bancassurance</a> <ul class="nav-2"> <li><a href="#"&g ...

Attempting to override the default system appearance in CSS for ASP.NET is ineffective

I am currently working on creating a CSS style that can modify the appearance of a dropdown control to look like a label, while allowing another dropdown on the same page to retain its default appearance. To achieve this, I am using a custom attribute with ...

Ensure that two div elements are always aligned, regardless of the circumstances

Is it possible to ensure that my two divs are always aligned, regardless of the content inside them? Sometimes the left div may contain images of varying sizes, while the right div may have text with different lengths. I initially tried setting a min-heigh ...

aligning radio buttons and labels on the same horizontal axis

Is there a way to ensure that radio buttons and their labels appear in the same line? I've tried adding margin and padding properties to the span tag, but it doesn't seem to work. Any suggestions on how to solve this issue? <form class="navba ...

Struggling to navigate through rows in a Material UI Table

After performing a search in my TextField, the rows appear correctly in the console. However, the table itself does not update at all. I attempted to set the result of the search to a new array, but this made my TextField read-only. Any assistance with fur ...

The parameter 'Node' cannot be assigned to the type 'CustomElement'. Error code: ts(2345)

Currently, I am developing a collaborative real-time text block editor using liveblocks technology. I have encountered some errors in my Editor.tsx file while working in VSCode. Could you provide some suggestions on how to resolve this issue? Error: Ar ...

Keep the footer consistently at the bottom of the page

Seeking advice on creating a footer that remains at the bottom of the page even when scrolling. How can I achieve this in the most effective way? Note: I have not written any code yet, just exploring how to accomplish this task optimally. ...

Looking to incorporate Functional Components in React using the package "@types/react" version "^18.0.17"? Learn how here!

With the removal of the children prop from React.FC type, what is the new approach for typing components? ...

What is the best way to display a unique image in a div based on the size of the

Being new to web design, I have a question about creating a webpage with a large image in the center like on GitHub's Windows page. How can I work with the image inside that particular div or area based on different screen sizes? Is it possible to mak ...

Issue with STS 4.7: CSS Autocomplete feature not functioning properly in Spring Boot project

Currently working on a web application using Spring Boot and Thymeleaf as the template engine. I've stored all my CSS files in the "resource/static/css" directory. However, when attempting to edit an HTML file, STS does not provide suggestions for CSS ...

Top techniques for creating a popup window with React

I'm working on my React application and I'm looking to implement a popup window that displays a table with some data. However, I want this popup to appear in a separate browser window rather than as a modal attached to the application's DOM. ...

Steps for properly invoking an asynchronous action creator sequentially

Source Code index.js import React from 'react'; import ReactDOM from 'react-dom'; import App from './App'; import { createStore, applyMiddleware, compose } from 'redux'; import { Provider } from 'react-redux& ...

single calendar bootstrap-datepicker allowing users to select date ranges

Is it possible to create a single calendar range date picker with Bootstrap instead of using two separate calendars? Currently, I have implemented a solution with two calendars. $('#datepicker').datepicker({ }); <link href="https://cdnjs.cl ...

Steps for updating the value of a button in Typescript and React

I currently have a button that manages my language switcher, configured to handle cookies, URL changes, and now I want it to update the translations on the page as well. The <img> tag is utilized for changing the country flag. < ...

Animating React components upon state change

Using Animate css in my current project involves a form where users need to provide answers to questions. If the answer is correct, the word correct should slowly appear, and if it's wrong, wrong should be displayed instead. Here's a simplified e ...

Unexpected behavior occurs when React Hooks state changes are not properly passed as props

I am facing an issue with my React app that uses hooks. I want to pass down the state of slots as props to another component, but unfortunately, the other component always returns undefined and does not react to any changes in the slots state. Here is the ...

The navigation bar remains fixed while the section heading fails to display properly

================================= My webpage acts like a homepage on the web. The issue arises when clicking on a new link, as my navbar is fixed and covers the section heading. I need the page to display the section heading properly even after clicking o ...

Sticky navigation bar anchored to the top of the webpage using CSS and HTML

Currently, I am in the process of learning CSS3 and HTML5 but I'm facing some confusion. My goal right now is to create a webpage with a fixed navigation bar at the top that scrolls along with the page. Although the navbar is fixed at the top and doe ...

Am I incorrectly linking Javascript/CSS files/pages?

<script src="scripts/lib/angular.min.js"></script> <script src="scripts/lib/angular-route.min.js"></script> <script src="scripts/lib/angular-animate.min.js"></script> <script src="scripts/lib/jquery.min.js"></sc ...