I've made a simple site with a #container
div that is parent to two divs: #left
and #right
, by using Grid Layout:
Is there any way to make the left column fixed? I'd like the left text to persist on its position, and the right text to be scrollable as it is now. Adding position: fixed
to #left
breaks the layout.
I'm aware that this question has been already solved, but I'd appreciate a way to make it work with the grid layout.
Thanks.
body {
margin: 0 0 0 0;
}
#container {
display: grid;
grid-template-columns: 50% 50%;
}
.section {
padding: 5% 5% 5% 5%;
}
#left {
background-color: aquamarine;
}
#right {
background-color: beige;
}
<div id="container">
<div id="left" class="section">
<p>This should not scroll</p>
</div>
<div id="right" class="section">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla aliquet consectetur purus nec volutpat. Donec vel libero nec est commodo facilisis vel et nisl. Praesent porta sed eros eu porta. Cras dolor nulla, ullamcorper et tincidunt quis, porta ut
tellus. Maecenas cursus libero sed accumsan luctus. Integer sed consequat ante. Morbi sit amet lectus tempor elit tempor cursus ut sed enim. Donec placerat bibendum volutpat.
</p>
<p>
Nunc sit amet eleifend sapien, sed tincidunt neque. Donec id sapien et nunc scelerisque iaculis dignissim nec mauris. Fusce at pretium nulla. Maecenas vel rutrum tellus, a viverra nunc. Aenean at arcu vitae dui faucibus dapibus. Vivamus hendrerit blandit
mollis. Aenean sit amet lectus a metus faucibus condimentum. Proin vel eros ut elit pharetra lacinia vitae eu orci. Etiam massa massa, aliquam at pulvinar ut, porttitor eu mauris. Ut in iaculis sapien.
</p>
<p>
In vitae rhoncus arcu. Maecenas elementum nunc quis magna finibus, vitae imperdiet diam pulvinar. Phasellus sit amet nibh eu massa facilisis luctus. Nulla ullamcorper sodales ante id vestibulum. Fusce felis nisi, lacinia sit amet mauris vel, euismod suscipit
neque. Mauris quis libero eget enim facilisis pharetra. Fusce non ligula auctor nunc pretium dignissim eget eget turpis. Nam ultricies dolor ac libero maximus vestibulum. Mauris et tortor vitae nisi ultrices vestibulum ac id mauris. Proin interdum
dapibus sollicitudin. Phasellus ultricies vulputate sem id hendrerit. Cras eget posuere nunc, in placerat velit. Pellentesque sed ante at elit ornare efficitur. Donec sed condimentum nisl. Curabitur dapibus leo id ligula dignissim pharetra.
</p>
</div>
</div>
You wrote:
If you want the element to remain a grid item, then the answer is "no".
Once an element has
position: absolute
orposition: fixed
(which is a form of absolute positioning, with reference to the viewport), it takes on new characteristics:From the spec:
So a grid item doesn't work well with absolute positioning.
However, you won't have a problem applying
position: fixed
to a grid container.Consider managing your
#left
and#right
elements separately.#left
can be a fixed-position grid container.#right
can be another grid container and remain in-flow.Also, as an aside, you've given your grid items percentage-based padding:
When applying
margin
andpadding
to grid items (and flex items), it's best to stay away from percentage units. Browsers may compute the values differently.try this:
https://jsfiddle.net/km5gdrcm/3/
You can do something like this
here is the fiddle
here is the code
I had a bit of the same problem. I needed a fixed sidenav (col 1) with scrollable content (col 2). Here's how I solved the problem (note that I use styled-component, but you can surely do it with regular css, sass, less, or whatever):
Now the css property for each of those styled-components:
It looks like this, but a bit more complex on my side since I also have a header and footer in my grid, and the sidenav is collapsable. But I think this could work for you.
You can achieve this by adding these CSS rules to your id #left:
Link for sticky position: Sticky position with nothing but CSS
Hope it helps you
EDIT (fix jumpy behaviour)
In order to avoid the left part to jump up at the end of the page, just add the following CSS rule to your id #left:
See updated code snippet:
Add one more div in right panel which panel you want to scroll for that give some
max-height
andoverflow: auto;
so left panel will be stick and right panel content will scroll.