Visually, it looks somewhat like this:
headerheaderheader
mainmainmainmain
col1col1col col2
I am trying to achieve two things here:
- have
col2
be fixed on the screen when scrolling down. - only keep
col2
fixed when the header hits the columns. It's a fixed header.
I've tried adding position fixed/sticky but that doesn't seem to work in this case. That column simply disappears. My React components are structured this way:
<Header />
<Middleware />
//below is inside of Middleware
<main>
<form className="booking-form"></form>
<Selection/>
</main>
//below is inside of Selection
<div className="selection"></div>
<BookingDetail/>
//finally, inside of BookingDetail
<div className="booking-detail"></div>
This is what my html looks like when rendered:
<header> This is the fixed header </header>
<main class="form-container">
<form class="booking-form"> takes up 100% horizontally </form>
<div class="selection"> takes up about 80% horizontally, below form </div>
<div class="booking-detail"> takes up about 20% horizontally </div>
</main>
Finally, the css
.form-container {
display: grid;
grid-template-columns: repeat(24, 1fr);
}
.booking-form {
grid-column: 3 / span 20; // takes up entirety of row, see 'main' above
}
.selection { //left column (col1)
grid-column: 3 / span 15;
}
.booking-detail { //right column (col2), which I need fixed when the header hits.
display: block; //display is block as at a smaller break point it's 'display: none;'
grid-column: 19 / -2;
}
EDIT: Kenan's answer works great except for a little bug I've been trying to fix. The contents inside of the 'selection' col (some divs with internal content) are unexpectedly resizing because of the scroll code and I cannot figure out how to fix it. These are its internal content (there are many of these)
<div className="business-and-timegrid-wrapper" key={index}>
<Estabelecimento
logoURL={item.logoURL}
name={item.name}
rating={item.rating}
address={item.address}
/>
{item && <TimeGridSelection item={item} date={dateForRenderingTimeGrid} />}
</div>
------------------------------------
.business-and-timegrid-wrapper {
padding: 1em;
margin-bottom: -3vh;
background-color: #fff;
display: grid;
grid-template-columns: repeat(2, 50%);
border: 1px #ECECEC solid;
box-shadow: 0px 3px 6px #00000029;
opacity: 1;
}