Issue with scrolling in div not adhering to top padding restrictions

Essentially, I encountered an issue where a list was scrolling independently but overflowed the top padding when scrolled down, disappearing not within the padding area, but at the end of the div. This led to a visually incorrect effect on my application.

Edit using React code

<div className={styles.parent}>

<div className={styles.div1}>
  <Header props={props} />
  <Cards/>
</div>

<div className={styles.div2}>
  {cards.map((card, index) => (
    <>
      <List className={styles.cardsItem}>
        <ListItem
          button
          divider={true}
          key={index}
          selected={selectedIndex === index}
          onClick={e => handleListItemClick(e, index, card)} >

        <ListItemAvatar>
          <Avatar>
            <CreditCardIcon />
          </Avatar>
        </ListItemAvatar>

        <ListItemText primary={card.name} secondary={card.number} />
      </ListItem>
    </List>
  </>
 ))}
</div>

</div>

Edit using CSS

.parent {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: repeat(2, 1fr);
  grid-column-gap: 0px;
  grid-row-gap: 0px;
  height: 100vh;
  text-align: center;
  overflow: hidden;
}

.div1 {
  grid-area: 1 / 1 / 2 / 2;
  height: 45vh;
}
.div2 {
  grid-area: 2 / 1 / 3 / 2;
  height: 55vh;
  overflow: scroll;
  padding: 1.5em;
  box-shadow: 0px -30px 30px #0000000d;
  border-radius: 2em;
}

Above is the list in its initial position without any scrolling:

https://i.sstatic.net/6Sx05.png

And below is the result after scrolling up:

https://i.sstatic.net/2ANDw.png

How can I also incorporate the padding into the scrolling behavior?

Answer №1

Initially, I anticipated this task to be simpler, but I am encountering challenges in finding a successful solution. To emulate the desired functionality, I resorted to using position: sticky on a pseudo-element as a padding mask. While this approach is not perfect and may not work effectively in scenarios involving multiple scrolling containers, I wanted to share it just in case it proves helpful to someone else.

div {
  border: 1px solid black;
  height: 100px;
  overflow: scroll;
  padding: 1.5em;
  position: relative;
}

div::before {
  content: "";
  display: block;
  position: sticky;
  top: 0;
  z-index: 100;
  background-color: white;
  width: 100%;
  height: 1.5em;
}
<div>
  <ul>
    <li>asdasdasd</li>
    <li>asdasdasd</li>
    <li>asdasdasd</li>
    <li>asdasdasd</li>
    <li>asdasdasd</li>
    <li>asdasdasd</li>
  </ul>
</div>

Answer №2

To ensure that the padding is visible, make sure to give a white background to the element. If not, the background will remain transparent and the padding won't show up as expected.

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

Refreshing GIF images in React using forceReload

In order to restart the gif animation every 12 seconds or whenever the activeIndex changes, I need to reload a GIF image with CHECKMARK_ANIMATION_ICON as the source. Below is the code: const reloadImgSource = (imgSource) => { setTimeout(() =& ...

Incorporating custom HtmlHelper methods in ASP.NET MVC 3

As a newcomer to MVC, I am eager to create my own extension method in order to enhance the functionality of the html helpers available in my razor views. While Html.DropDownListFor() allows for the creation of a drop down list for any property on my model, ...

How come my header is not spanning the full width of the page?

Hey there, I've been struggling with a specific issue on my website and can't seem to find a solution anywhere else. The header on my site is supposed to stretch across the entire width of the screen, but for some reason, there's always a ga ...

Align Bootstrap 4 dropdowns with their parent element

Is there a way to match the width of a bootstrap dropdown menu with its parent input group? <div id="ddl_1" class="input-group"> <input type="text" class="form-control "> <div class="input ...

Text hidden within the image, each line concealing a separate message

I am having an issue where my image is overlaying the text. I would like the text to wrap around the image and for any hidden text to appear on a different line. How can this be achieved? Here is the CSS code I am using: div.relative { position: relati ...

What could be the reason that the height: auto property is not creating a content area big enough to accommodate my content

When a user is not logged in, the header displays a logo and a login form that requires ample space. Once the user logs in, the full navigation menu appears in a smaller header size. To achieve this, adjusting the height:auto property of the "site-header" ...

Python code to retrieve historical data in CSV format from a website that mandates authentication

Despite my thorough online search, I have been unable to uncover a solution to my dilemma. Specifically, I am interested in downloading daily csv files of historical data from a sunnyportal website that requires login. The login page can be found here: ht ...

The alignment of the Bootstrap 4 row is off-center

My bootstrap structure includes a row with 4 columns, initially set up with 3 columns containing icons that aligned perfectly. Upon adding a fourth column, the new addition appears slightly lower on the page compared to the rest, resulting in misalignment. ...

Sorry, Computed styles are not available in IE 11 Edge DevTools

Currently facing an issue with a component element rendering differently on IE11, however the DevTools for both IE and Edge aren't showing any CSS in the Styles or Computed tab. What is the correct way to debug this situation? Comparison from left to ...

Searching for a solution on how to effectively utilize ng-repeat to filter data based on a specific field when passing an array, while considering that the field may or may not be included in the array. Working within Angular

ng-repeat="equipment in equipments | filter: {guidEquiment: ['cb8b22e1-43f0-4dc9-b2dc-34847731e2d1','cb8b22e1-43f0-4dc9-b2dc-34847731e2d1','cb8b22e1-43f0-4dc9-b2dc-34847731e2d1',....N]}" When the variable equipment has a guid ...

The filtering function using jQuery and ajax

Hello, I'm currently working on a website that compares the prices of domain names. I could use some assistance with a specific function that I'm trying to implement. At the moment, there is a section on the website where users can click on dif ...

How to instantly return progress bar to zero in bootstrap without any animations

I am currently working on a task that involves multiple actions, and I have implemented a bootstrap progress bar to visually represent the progress of each action. However, after completion of an action, the progress bar is reset to zero using the followi ...

The color used to highlight questions on Stackoverflow

Could someone please help me identify the specific color that stackoverflow uses to highlight the background when a question answer link is followed? For instance, take a look at this link: Is there a float input type in HTML(5)? Appreciate any assistanc ...

The background appears only in the upper portion of the screen upon refreshing the table

Whenever the user clicks on the edit button or when the page is initially loading, a backdrop appears and disappears once the modal is displayed. The backdrop effectively highlights the entire page. However, after updating my table with data via Ajax witho ...

Attempting to clear out a bootstrap-select element and refill it with new options

I've been attempting to clear all the values in a multiselect selectpicker from bootstrap-select, but unfortunately, no matter what method I try, it doesn't seem to work. I attempted to follow the examples provided for the remove method in the m ...

What is the reason for the jquery datatable never being reinitialized?

I've been struggling to get this right, but unfortunately, I keep failing. The issue arises when I call the filltblServicesReport function, which is primarily responsible for initializing the database. Initially, it works perfectly fine; however, upon ...

I possess a button that decreases the value of a number in the database when pressed. I desire for this action to occur repeatedly without fail

I have a button that, when pressed, decreases a number in my database by one. I want this action to decrement the number each time it is clicked. Code in HTML and PHP <form method="post"> <div><?php $username = "root" ...

Discover the process of retrieving HTML elements from a different webpage and incorporating them into your own site

As a newcomer, I'm in search of a code that can help me fetch an HTML string from one webpage and use it in another webpage. To illustrate my point, consider the following mock examples. Example 1: Mysite.com/A.html <body> <!---My Script Goe ...

jQuery command to display a div element when a button is clicked

Hey there, I'm trying to display a div element in jQuery mobile when the user touches the button. I have already created custom classes for both the button and div element but unfortunately, nothing seems to happen. Can anyone tell me which classes I ...