Consider the following. I have a overflow auto div whose content outgrows its height. I then have an absolute position div (red background) that I would like to run the entire height of the scrollable div. In other words I want it to extent to the bottom of where the text stops, but right now it stops at the bottom the initially-visible text only. In other words how can the red div (.b) extend to the bottom of the scrolling distance. Any way I can do this with CSS?
.a {
height: 80vh;
width: 200px;
overflow-y: auto;
position: relative;
}
.b {
height: 100%;
position: absolute;
top: 0;
width: 10px;
background: red;
}
<div class="a">
test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test test
<div class="b"></div>
</div>
Ideal solution will be CSS-only and not rely on refactoring the HTML layout.
5
Answers
One possible way is with a grid and setting grid-row, see example below, now without refactoring
Of cause, this is a bad idea if you dont just have "test test test" in your div, but actual content with other html elements, which will then break the grid. The "secure" solution requires adding one wrapper div (original solution posted, before question was altered)
The problem with your current structure is that the height of
a
is fixed at80vh
and the height ofb
is100%
ofa
, which means also80vh
.If you are open to changing the HTML a bit, you can fix this by allowing the height of
a
to be auto (default) and instead wrappinga
in a container element that has a fixed height with overflow scroll. This way the height ofa
will be the full height of the content and the height ofb
will also be the full height of the content.Here’s an example:
Add
min-height:100%
to your div and setheight:auto
. This will stretch your color to full hieght, but also grow as your text grows past 100% height.An idea with sticky position but it only works if you know the value of height because you need it for
margin-top
If extra
<div>
will not be so critical, then add it:There is another crazy option, but it is unlikely to be appropriate in the context of your task: