I have a simple calendar view which statically can be coded as the React component below. There is a parent container and it contains two containers timeslot
and days
. I want them next to each other so i used flex property. Now i want container to be some fixed size and want the overflow items to scroll. When i scroll i want headers to stick. The timeHeader
and dayHeader
stops sticking after I scroll 40% of the vertical length. I am stuck on this for a while. Pls help!
import React from "react";
import "./Test.css";
function Test() {
return (
<div className="parent">
<div className="timeslot">
<div className="timeHeader">Time</div>
<div>
<ul>
<li>7:00 a.m</li>
<li>8:00 a.m</li>
<li>9:00 a.m</li>
<li>10:00 a.m</li>
<li>11:00 a.m</li>
<li>12:00 p.m</li>
<li>1:00 p.m</li>
<li>2:00 p.m</li>
<li>3:00 p.m</li>
<li>4:00 p.m</li>
<li>5:00 p.m</li>
<li>6:00 p.m</li>
<li>7:00 p.m</li>
<li>8:00 p.m</li>
<li>9:00 p.m</li>
<li>10:00 p.m</li>
</ul>
</div>
</div>
<div className="days">
<div className="dayHeader">Days</div>
<div className="dayslots">
<div>
<ul>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
<li>1</li>
</ul>
</div>
<div>
<ul>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
<li>2</li>
</ul>
</div>
<div>
<ul>
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
<li>3</li>
</ul>
</div>
<div>
<ul>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
<li>4</li>
</ul>
</div>
</div>
</div>
</div>
);
}
export default Test;
and its CSS
.parent {
display: flex;
width: 400px;
height: 100px;
overflow: scroll;
}
.timeHeader {
position: sticky;
top: 0;
background-color: white;
}
.timeslot {
position: sticky;
left: 0;
z-index: 2;
background: white;
}
.days {
flex: 5;
}
li {
min-width: 100px;
}
.dayslots {
display: flex;
}
.dayHeader {
background-color: white;
position: sticky;
top: 0;
}
The data inside the li tag, you can ignore it, it could be anything. All i want to fix is Time Header to stick all the time. Day header to stick when i scroll vertically and time i.e 7:00am… to stick when i scroll horizontally.
2
Answers
Your issue is with .timeslots css property. Almost what you’re looking for
For flex items to shrink over their content size to fit to the container size, you need to set the corresponding min-width (or height) to 0, so add
min-width:0
on thetimeslot
anddays
container.This can be solved by flex box
flex-direction: column;
and as above mentionedmin-height: 0
to allow the huge lists to shrink over their content size. The lists container can now shrink, and settingoverflow-y: scroll
(orauto
) on these two huge lists can work.The final html and css will look like this: