1

Basically, i had a list that scrolls independently, but the list overflows the top padding when i scroll down, vanishing not on the padding, but in the end of the div, causing a wrong looking effect on my app.

Edit with 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>

Edith with 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;
}

Here, the list on its initial position, not scrolled:

01

And here, the result after scrolling up:

02

How can i apply the padding to the scrolling too?

2 Answers2

2

I thought this would be easier than it is, but I'm not having much success. The only way I could simulate the desired behavior was to use position: sticky on a pseudo-element as the mask for padding. It's not ideal and I don't think it will be useful in real-world scenarios with multiple scrolling containers, but wanted to share in case it helps 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>
jeffjenx
  • 17,041
  • 6
  • 57
  • 99
  • 2
    With some tweaks, i manage to do it. Again, not the ideal solution, rather a temporary fix. Thanks for the insight. https://codepen.io/AltairTF/pen/VwYrxyb – Altair Todescatto F Jan 03 '20 at 19:59
0

You need to assign a (white) background to the element that has the padding - otherwise the background (and with it the padding) will be transparent, causing the effect you describe.

Johannes
  • 64,305
  • 18
  • 73
  • 130