Home > Mobile >  Display ReactJS Components Within a Scrollable List with height:auto
Display ReactJS Components Within a Scrollable List with height:auto

Time:01-12

How can I render the same component inside a map, without overlapping, but one below another

Here's what i mean:

return (
    <div className='app'>
        {[0, 1, 2, 3].map(element => {
            return(<MyComponent props = {element}/>)
        })}
    </div>
)

How can I display that component in a scrollable list, considering that its height is auto?

CodePudding user response:

You are Describing the Default Behavior Already

How can I display that component in a scrollable list, considering that its height is auto? [emphasis mine]

With height:auto;, the behavior you describe happens already:

.item {
  height:300px;
  border: 1px solid black;
}
<div class='app'>
    <div id="1" ></div>
    <div id="2" ></div>
    <div id="3" ></div>
</div>

But if you wanted a Limited Scroll

You need two things:

  1. You need to add overflow-y to be set to scroll.
  2. You need to set the height to how much of a scrollbar you want.

.app {
  height: 40px;
  overflow-y: scroll;
}

.item {
  height:30px;
  border: 1px solid black;
}
<div class='app'>
    <div id="1" ></div>
    <div id="2" ></div>
    <div id="3" ></div>
</div>

CodePudding user response:

you can add the following css:

.app{
     display: flex;
     flex-direction: column;
     gap: 2vh;
     overflow-y: auto;
}
  •  Tags:  
  • Related