What methods are available to keep a component in a fixed position on the window during certain scrolling intervals?

I need help creating a sidebar similar to the one on this Airbnb page. Does anyone have suggestions for how to make a component stay fixed until you scroll past a certain section of the page? I am working with functional React and Material-UI components, and I am relatively new to front-end development and TypeScript/HTML/CSS. If this question has already been addressed somewhere else, I apologize. I am having difficulty finding solutions online due to my limited understanding of how to describe the problem accurately.

Answer №1

To make your content stick to the top of the page, you can utilize the CSS property position: sticky along with top: 0.

If you want to add some space above the sticky content, you can adjust the value in top:50px according to your preference.

This technique will ensure that the content remains fixed at the top of the viewport when scrolled.

div{display: block;}

.some_top_content {
  width: 100%;
  height: 100px;
  background: yellow;
}

.container::after {
  clear: both;
  display: table;
  content: '';
}

*{box-sizing: border-box;}

.left_content {
  height: 800px;
  float: left;
  width: 48%;
  margin-right: 4%;
  background: blue;
}

.right_content {
  width: 48%;
  float: right;
  position: sticky;
  top: 0;
  height: 400px;
  background: green;
}

.some_bottom_content {
  width: 100%;
  height: 600px;
  background: red;
}
<div class="some_top_content"></div>
<div class="container">
  <div class="left_content"></div>
  <div class="right_content"></div>
</div>
<div class="some_bottom_content"></div>

PS: Make sure to run this snippet in full-page view for optimal results.

Check out the Codepen example here

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

Click on the button to generate a PDF report using Internet Explorer 11

After encountering some challenges with printing a PDF report specifically on IE 11, I am reaching out for help. The code snippet below works perfectly in Chrome, but when it comes to IE 11, everything falls apart. Just to provide some context, I am develo ...

Conceal the current component prior to displaying the component associated with Navlink in React Router

Currently, I am in the process of developing a single-page web application using React routing. My goal is to hide the current component before rendering the next component when a NavLink is clicked, similar to how <a href="someLink"> works in HTML. ...

Sorting of boxes is not possible when utilizing the sortable() function

Is there a way to swap the positions of the left and right boxes using the sortable() function from jQuery UI? It seems to work fine for numbers 1 and 2, but not for switching the boxes. $(".sort-me").sortable({ connectWith: ".connectedSortable" } ...

An unhandled error occurred when trying to parse JSON data, indicating that there is an unexpected character 'r' at the beginning

I'm a newcomer to the world of react and express. I followed the same steps for login and register, but encountered an error "Uncaught (in promise) SyntaxError: Unexpected token r in JSON at position 0" while working on the register functionality. He ...

Proper HTML style linking with CSS

Describing the file structure within my project: app html index.html css style.css The problem arises when trying to link style.css as follows: <link rel="stylesheet" type="text/css" href="css/style.css"/> S ...

Using Material UI Grid in React

Is there a simple and effective method to create a table with Material UI? I've tried using direction="column" or direction="row, but they don't work when I need columns of varying heights. Any recommendations? https://i.sstatic.net/YzuC8.png ...

Mastering the art of nesting loops and conditions in React: A comprehensive guide

I am fetching data in React using the following method: const data = [ { header: 'my header1', copy: [ 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.', 'Etiam et risus quam. Pr ...

tips for moving a button 2 pixels up or down in position

How can I adjust the position of my button by a specific number of pixels up or down from where it currently is? Can you provide guidance on how to find the current location? Thank you in advance. ...

Steps for displaying a loader after refreshing data using queryClient.invalidateQueries:1. Utilize the query

I am currently working on a project where I need to redirect to a specific page after updating an item. While the code is functioning correctly, I have encountered an issue with the loader not displaying. export const useUpdateStatusArchiveSurvey = () => ...

The module "@uploadthing/react" does not contain the exported member "FileWithPath"

'use client' import type { FileWithPath } from '@uploadthing/react' import { useCallback, Dispatch, SetStateAction } from 'react' import { useDropzone } from '@uploadthing/react/hooks' import { generateClientDropzon ...

The outdated expo-cli legacy version is not compatible with Node +17

After installing expo-cli and setting the environment variable, I encountered the following error: The outdated expo-cli does not work with Node +17. It is recommended to switch to the updated Expo CLI (npx expo). Uncaught Error: EPERM - operation not all ...

Struggling to align your image properly?

My attempt to center the image "Logo_Home.svg" on my page has failed, as it is currently positioned in the top left corner. Can anyone help me identify what I am doing wrong and guide me in the right direction? Thank you. EDIT 1 I realized that I forgot ...

Determine the generic parameter of the output type by analyzing the resolved value of a data type within the function

I am looking to automatically determine the generic parameter of the return type by utilizing the resolved value of a type within the function. Consider the following: export type Context = any; export type Handler<T> = (ctx: Context) => Promise& ...

Extracting an array from an HTTP response in Angular/Typescript using the map operator or retrieving a specific element

Q1: How can I extract an array of objects from a http response using map in Angular? Q2: Is there a way to retrieve a specific object from a http response by utilizing map in Angular? Below are the example URL, sample data, CurrencyAPIResponse, and Curre ...

Enhancing the appearance of child elements using CSS modules in Next.js

My Countdown component implementation includes: import styles from "./Countdown.module.scss"; import React from "react"; import useTimer from "hooks/useTimer"; import cn from "classnames"; function Countdown({ date, ...

Why isn't my element displaying on the screen?

Having recently started learning React, I've encountered a challenge that I've struggled with for quite some time - I can't seem to add a component inside the main app.js file. I initially set up my project using create-react-app. Here&apo ...

Ways to prevent animations from displaying solely in IE

Is there a way to remove a keyframe animation if the browser is IE, while keeping it for Chrome, Safari, and FireFox? The animation I am referring to is defined as follows: // Animations @-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } } ...

Is it possible to utilize [key:string]: any in order to eliminate the requirement for an additional function when working

Currently, I am utilizing recompose within my React project. Specifically, I am leveraging its compose function which is defined as: function compose<TInner, TOutter>( ...functions: Function[] ): ComponentEnhancer<TInner, TOutter>; interf ...

Exploring ways to incorporate conditional imports without the need for personalized webpack settings!

Our project is designed to be compatible with both Cordova and Electron, using a single codebase. This means it must import Node APIs and modules specific to Node for Electron, while ignoring them in Cordova. Previously, we relied on a custom webpack con ...

The export button in the React Material Table doesn't seem to be displaying correctly

[I am encountering an issue with the React export button in Material Table. The bar is not showing completely. My code includes the following: options = {{exportButton:true}} How can I resolve this?]1 ...