Reacting like sticky bottoms and tops

I'm working on a react/tailwind project that involves a component I want to be fixed at both the top and bottom of the screen.

In simpler terms, there's an element that I need to always stay visible even when the user scrolls up or down the page.

When the page loads, this element should appear at the bottom of the screen. As the user scrolls, it should move along with the content until it reaches the top of the viewport where it should remain sticky.

Here's an example of what I've been trying:

<div>
  This is the main container
  
  <div>
    This is a block element pushing others off-screen 
  </div>
  
  <div className="sticky top-0 bottom-0">
    This element needs to stick at the top and bottom of the screen
  </div>
  
  <div>Other elements</div>
</div>;

I've experimented with different solutions and done some research, but nothing seems to work as required. Appreciate any help. Thank you!

Answer №1

Setting className to "sticky top-0 bottom-0" does indeed work, but you may need to adjust the heights for it to function properly. Here is an example of how you can make it work:</p>
<pre><code>    <div className="bg-red-600">
      This serves as the outermost container
      <div className="h-[128rem] bg-blue-600">
        This element occupies space and pushes elements below off-screen
      </div>
      
      <div className="sticky top-0 bottom-0 bg-orange-600 h-28">
        The element I want to remain fixed on the screen (sticky at the top and bottom)
      </div>
      
      <div className="h-[128rem] bg-green-600">Additional elements</div>
    </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

What is the best way to access JSON data that is saved in a variable located within a separate component?

I'm currently utilizing axios to fetch JSON data from my API, mapping it, and storing it as a variable. I'm struggling to figure out the optimal way to call these variables within my React components. Retrieving and Storing JSON Data as Variable ...

vertically centering a div specifically on laptops

What is the best way to vertically center a div on lap-tops? (...) body {padding: 4% 16%;} body {top:0px; left:0px; bottom:0px; right:0px;} body {border: 12px solid darkred; border-style: double;} body {background-color: #FAFCB4;} p {font-size: 80%; text- ...

The variable is currently undefined because it has an array assigned to it

Upon selecting multiple checkboxes for variants, I am retrieving checked data using the following method: get selectedIdsFromViolCategoriesFormArray(): string[] { return this.violCategories .filter((cat, catIdx) => this.violCategoriesFormArr. ...

Troubleshooting problem with ESLint when importing Firebase into React application

When starting my file, I include two imports: import firebase from "firebase/app"; import "firebase/database"; const firebaseConfig = { apiKey: "***", authDomain: "***", databaseURL: "***", project ...

Secure your website against XSS attacks with DOMPurify

Currently I am working on resolving a cross-site scripting (XSS) vulnerability using DOMPurify. The issue lies in the URL provided below: https://stage-xyzmysite.com/login/?rUrl=javascript:alert('hi'). In order to create a proof of concept, I am ...

How to Implement Single Selection in Material UI Accordion with Multiple Select Options

As a newcomer to material-ui, I came across an example in their documentation showcasing the Accordion component. I am interested in using it but would like to restrict it to single select only. Unfortunately, the default version provided in the documentat ...

Comparing the map function and for loop in the Puppeteer package for Node.js

I experimented with the Puppeteer package in NodeJS and noticed a significant difference in functionality between using the map function versus a for loop. Here is an illustration of what I observed: Using the map function: data.map(async(info) =>{ ...

Creating a responsive design for a centered image with absolute positioning

I'm trying to align an image at the bottom of a div while also centering it using the following CSS: HTML: <div class="myDiv"> <img src="http://bit.ly/1gHcL8T" class="imageCenter" /> </div> CSS: .myDiv { width: 80%; height: ...

What is the best way to display time in EJS?

Currently facing an issue with formatting a time retrieved from the database. The value is strictly a time and not associated with any date. I've attempted to use getHours and getMinutes in EJS, but they don't seem to be functioning as expected. ...

Challenges with JavaScript calculations on dynamic HTML forms

Currently, I am immersed in a project focused on creating invoices. My progress so far involves a dynamic form where users can add and remove rows (as shown in the snippet below). I'm encountering an issue with my JavaScript and I could use some ass ...

Error in MUI: Unable to access undefined properties (reading 'drawer')

Recently, I encountered an unexpected error in my project using MUI v5.0.2. Everything was working fine just a week ago with no errors, but now I'm facing this issue without any changes made to the code: Error: TypeError: Cannot read properties of un ...

Anomalies observed in label transition while in active state for input field

Can you explain why there is a gap in the label when it's positioned at left:0? I'm aiming to achieve a UI similar to Material design but struggling with label positioning. I've experimented with using translateY(-6px), however, this method ...

Display various React components for widgets

I am looking to display multiple instances of a widget in html. I have 3 divs with the same id, each with different attributes, and I want to render my react component three times on the page. Currently, I am only able to display the first component. Here ...

Addressing problems with rendering SVG image elements in Safari and Firefox

While constructing a react app on express 4, I encountered an issue with displaying images within the SVG tag on Safari and Firefox browsers (not Chrome). Here is an example of the code: <svg width="1373.6499999999999" height="250"> <g transfor ...

The center alignment doesn't seem to function properly on mobile or tablet devices

I've designed a basic webpage layout, but I'm facing an issue with responsiveness on mobile and tablet devices. The problem seems to be related to the width: 100% property in my CSS, but I can't seem to pinpoint the exact solution. While tr ...

What is the best way to dynamically adjust row sizes in react-bootstrap?

I have successfully created a grid of cards in groups of three using react-bootstrap. However, I am facing an issue where if one card's height is longer than the others, it creates unnecessary space and disrupts the layout of the entire row. You can s ...

How can I modify the parent form element to only evaluate the expression at the text node, without affecting Angular interpolation?

Currently, I am in the process of developing an eCommerce platform and utilizing angular to construct a widget on product detail pages. Unfortunately, my control over the initial HTML rendered for the browser is quite limited. While most tasks related to m ...

What is the best way to add animation to our data display with react-spring?

Is it possible to animate the display of records using react-spring? Can we utilize the useTransition function within useEffect()? I am looking to animate records from bottom to top individually when a user navigates to the page. So far, my attempts have b ...

What is the best way to show an error message on a field when using a custom form validator?

In my JavaScript code, I have created a form validator that is capable of validating different input fields based on specific attributes. However, I need assistance with implementing error handling and scrolling to the erroneous field. For each <input& ...

Shifting the location of the indicator for Tabs in Material UI

Can anyone help me figure out how to move the indicator to the top of a TAB instead of the bottom? I've been struggling with this and tried updating the CSS, but it's not working. I'm new to React and having trouble customizing the component ...