When I try to scroll, the sticky card fails to stay in place as intended with CSS

I'm having trouble creating a sticky card that remains in place when I scroll.

https://i.sstatic.net/wPZ6S.png

<Box display={'flex'} width={'100%'} height={'150rem'}  >
  <Box width={'100%'}>
    {features.map((feature) => (
      <div key={feature.id}>
        <Typography variant='h2' py={10} color={'grey'}>
          {feature.title}
        </Typography>
      </div>
    ))}
  </Box>
  <Box position={'sticky'} width={'100%'} sx={{ backgroundColor: 'grey' }} top={0} height={'50rem'} alignSelf={'flex-start'} >
    This is where the sticky card content would go
  </Box>
</Box>

Answer №1

In order to ensure that the sticky element stays fixed at the top of the page as you scroll, it needs to be placed directly inside the <body> instead of within a parent container like <box>. This way, the sticky behavior will be applied correctly.

 <Box display={'flex'} width={'100%'} position={'relative'} alignItems={'start'}>
  <Box width={'100%'}>
    {features.map((feature) => (
      <div key={feature.id}>
        <Typography variant='h2' py={10} color={'grey'}>
          {feature.title}
        </Typography>
      </div>
    ))}
  </Box>
</Box>
<Box display={'flex'} position={'sticky'} width={'100%'} top={0} height={'100vh'} alignItems={'center'}>
  <Box width={'100%'} height={'50%'} sx={{ backgroundColor: 'grey' }}>
    sticky card placeholder
  </Box>
</Box>

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

Finding the Earliest and Latest Date in Material UI DatePicker Component within a React Application

I am facing an issue with date selection based on the 'access' value in my code. When the 'access' value is 1, there should be no restrictions on selecting dates. However, when the value of 'access' is not 1, users should only ...

Customizing the Collapse Button CSS in Datatables

I am looking to customize the CSS of the collapse button on this particular example https://datatables.net/extensions/searchpanes/examples/customisation/buttonText.html I have already conducted numerous searches and attempted various solutions. ...

Every time I try to access my website, all I see is the WordPress installation process page

After initially hosting a WordPress website, I made the decision to switch over to SPIP. However, when attempting to access the site on my laptop, it continues to bring up the WordPress installation process. Interestingly enough, the website appears to lo ...

Issues with Font Awesome fonts failing to load after compiling an Angular project using `ng build`

I've encountered an issue with Angular 8 and Font Awesome icons. I initially added the font-awesome path in the angular.json as follows: "./node_modules/font-awesome/css/font-awesome.css" However, all the icons were displaying as empty boxes (not lo ...

Tips for aligning two elements in a row using Bootstrap 4 Flex

I am facing a challenge with positioning an H2 element and a Dropdown button on the same line using bootstrap 4 flex. Here is what I have: https://i.sstatic.net/kV45k.png This is what I am trying to achieve: https://i.sstatic.net/Owt5j.png Below is my ...

Creating a flipbook out of a PDF file

I am looking for a cost-effective solution to convert my numerous PDF files into flipbooks for easy reading. I have been unable to find a free tool for this purpose, so I am curious if it is possible to create a flipbook using only HTML, CSS, and JavaScr ...

Executing Cascading Style Sheets (CSS) within JQuery/Javascript

I've been encountering a problem with my website. I have implemented grayscale filters on four different images using CSS by creating an .svg file. Now, I'm looking to disable the grayscale filter and show the original colors whenever a user clic ...

The Material UI 5 Autocomplete feature is not providing the expected filtered options

I am currently utilizing Material UI V5, Since the default filtering in Autocomplete does not provide the desired result array, I have implemented my own filterOptions function. const customFilter = (options, state) => { let result = options.filter(op ...

Creating a grid layout with a row of buttons

I'm currently working on aligning buttons within a div to achieve the desired effect shown in the image below. My goal is to have all buttons centered on the page, with the small circles placed in a separate div as they are initially invisible and it& ...

Increasing and preserving the width value of a div using SCSS?

I am currently working on a div where I need to increase the width based on checkboxes that are selected. The goal is to add to the width as more checkboxes get checked. Here is an example of the HTML code I'm working with (please excuse any syntax e ...

Adjust the size of an element by utilizing a different element

I'm facing a challenge with this code. I need to dynamically set the width of the "wrap-description" divs to be equal to the width of the corresponding "picture-damage" images plus an additional 20 pixels. Since there will be multiple elements for bot ...

Is there a way to conceal the Tumblr App buttons on an iPhone?

I attempted to hide the buttons using the CSS code below, but unfortunately, it did not work: .app-cta-button .tx-button.ios { display: none !important; } Despite my efforts, the buttons still appear on the website. The URL of the website in questi ...

What is the trick to shortening paragraphs with dots...?

There is a p tag, <p> most iconic character in cinema</p> I need to truncate the text after 8 characters, and display dots afterwards. For example: most ic... Is there a way to achieve this using CSS? Any help with AngularJS would also be ...

Tips for aligning buttons in the center of a card both vertically and horizontally

I am currently utilizing the following bootstrap template to develop a Help Desk Ticket App: After the user logs in on the index page, there are two cards available for the user to choose between heading to the app's dashboard or creating a ticket. I ...

Troublesome issue with custom select feature in foundation when used within a button

I'm currently incorporating Zurb Foundation 4 into my website and using the "custom forms" feature for special styling on form elements. The issue I'm facing is that HTML select elements aren't functioning properly when placed inside a butto ...

The jQuery code functions smoothly on computers, but experiences delays when running on an iPhone

I was working on my website and trying to add a div that sticks to the top of the browser when it scrolls out of view. I found a script that works well on desktop, but when testing it on iPhone, there is a slight delay before the div pops back up in the ri ...

Getting rid of unnecessary compiled CSS files in Compass

After making changes to files and running compass compile, the compiled files remain even if they are renamed or deleted. Similarly, compass clean does not remove these old files as it only focuses on cleaning up current files in use. I want to avoid compl ...

Bootstrap 4 content block alignment explained

Below is the code snippet I am currently working with: .banner { background: #f9f9f9; width: 300px; height: 60px; } <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" hre ...

How can we determine the dimensions of an absolutely positioned element within a relatively positioned element?

One thing I noticed is that when placing an absolutely positioned element within a relatively positioned element, the latter does not automatically inherit the width and height of the former unless manually set. Take this example: <!DOCTYPE html> ...

The PurgeCSS CLI does not generate CSS files beyond the command line interface

I'm struggling to successfully extract my CSS using purgecss from the command line and save it to a separate file. The instructions on PurgeCSS.com are vague: By default, the CLI only outputs the result in the console. To save the purified CSS files ...