css - scrollable child of fixed element

position: fixed not scrolling with page
css position: fixed inside scrollable div
fixed div on scroll jquery example
make div fixed after scrolling
css scroll position
fixed button on scroll
css overflow fixed element
position: sticky horizontal

I have a fixed div with 100% height, and within that, a child-div that's relatively positioned. The child-div holds text that can be changed, so it doesn't have a fixed height.

I want the child-div to scroll vertically if its content overflows out of the screen. I played around with the min and max height properties to achieve this, but it isn't an ideal solution, and doesn't always work.

EDIT: min and max height seemed to be ignored, almost. I calculated how much vertical area the textBox would take up for the minimum 'allowable' screen height, and set that as the height. Adding min and max height made no difference to this. The only problem with this solution is that the box is always around ~60%, so even when it doesn't need to scroll, it does. This works, but isn't ideal. If there's a way to get around this it would be great.

This is what I have so far:

<div class="content">
    <div id="textbox"> some text
    </div>
</div>

.content { position: fixed; height: 100%; top: 0px; right: 0px; }

#textBox { 
    position: relative;
    top: 165px;
    height: 61.5%;
    overflow-y: auto;
}

Is there a better, more fool-proof way for doing this?

The following worked perfectly for me:

<style type="text/css">
    #fixed {
        position:   fixed;
        top:        0;
        bottom:     0;
        left:       0;
        right:      0;
        border:     1px solid black;
        overflow:   hidden;
        background: white;
    }

    #scrolling {
        overflow: auto;
        max-height: 98%;
    }
</style>

<div id="fixed">
    <div contenteditable id="scrolling">
        <p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
        Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam
        egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo. Quisque sit amet est et sapien
        ullamcorper pharetra. Vestibulum erat wisi, condimentum sed, commodo vitae, ornare sit amet, wisi. Aenean
        fermentum, elit eget tincidunt condimentum, eros ipsum rutrum orci, sagittis tempus lacus enim ac dui. Donec non
        enim in turpis pulvinar facilisis. Ut felis. Praesent dapibus, neque id cursus faucibus, tortor neque egestas
        augue, eu vulputate magna eros eu erat. Aliquam erat volutpat. Nam dui mi, tincidunt quis, accumsan porttitor,
        facilisis luctus, metus</p>
    </div>
</div>

The div's content is editable, so just add text until it scrolls. It would work on decent browser.

Live Example

Scrolling for a fixed element, The following worked perfectly for me: <style type="text/css"> #fixed { position: fixed; top: 0; bottom: 0; left: 0; right: 0; border: 1px solid black; overflow: hidden;  The upper child div is of variable height, but is guaranteed to be less than the height of the parent div. The lower child div is also of variable height. In some cases, the heights of the child divs will make the lower child div exceed the parent. In this case, I need to make the lower div scrollable.

Basically You have to set the overflow of your fixed element as you can see in this example, and there's a jsfiddle to play with the possibilities.

Dealing with overflow and position: sticky;, Hello ChrisP,. This is a simple fix with overflows :D #headerNav{ position: absolute; overflow-y: scroll; overflow-x: hidden; }. Hope that helps ^^. Without a fixed header, I can simply add overflow-y: scroll to div.sidebar and I can happily scroll all it's content if it overflows the bottom of the container. However, I'm running into issues with having a fixed, variable height header at the top of the sidebar, and having any content underneath that scroll if it's too long to fit into the container.

I have found a solution by testing and it seems to work. It requires 3 DIVs. First and uppermost div will be your fixed element. It will contain another div as its child, which will be positioned relatively. It will another div and this div will contain your content, it has to be positioned absolutely

Code: https://codepen.io/ltorvalds024/pen/GxKdeO

position: sticky is Amazing, and no height is the enemy of child elements with position: sticky;. The use case here is a table with sticky headers on vertical scrolling and  See the Pen Scrollable menu with pop out submenus (step 1) by Agop (@agop) on CodePen. Basically, in order for an absolutely positioned element to appear outside of an element with overflow: hidden, its closest positioned ancestor must also be an ancestor of the element with overflow: hidden.

Advanced layouts with absolute and fixed positioning · WebPlatform , what you can build with CSS position:sticky, how to use it, and what to watch out element to stick around in view as the user is scrolling, but not become sticky  Definition and Usage. The overflow property specifies what should happen if content overflows an element's box. This property specifies whether to clip content or to add scrollbars when an element's content is too big to fit in a specified area. Note: The overflow property only works for block elements with a specified height.

Change to position fixed on iOS Safari while scrolling · Muffin Man, Elements with fixed positioning differ from this slightly—they always have the Next, copy the following code into a new file and save it as absolute.css. The height of the #outer element is given by its child element ( #inner ) and Scroll the page downward, and keep an eye on the gray square in the top left-hand corner. In the grand tradition of mentioning scroll debouncing whenever any demo binds an event to a scroll event: you should consider debouncing when binding functions to scroll events, because if you don’t, it’ll get called a zillion times and could be slow. CSS. This is the kind of thing that would be sweet to do in CSS alone.

position: fixed - position, The position CSS property sets how an element is positioned in a document Note that a sticky element "sticks" to its nearest ancestor that has a "scrolling width of our parent element, and we are as tall as our child content. Scroll snap properties are applied to both parent and child elements, with specific values for each. Sort of the same way flexbox and grid do, where the parent becomes a “flex” or “grid” container.

Comments
  • Have you tried setting bottom instead? IE - bottom: 5px;
  • Thank you for taking the time to respond. Your solution is somewhat similar to what I had earlier, and I'm guessing there's not much of a better way to do it. I assumed a certain min-height for the screen, and calculated what percentage the textBox would be, and set that to the height property (somewhere around 60%). The only problem with this is that even when there is space on the screen, the box will take up only ~60% and be scrollable.
  • I don't want the fixed element to scroll, however. I want the child to scroll if the screen can't show all it's content...
  • I understand now. What if you create three divs, set the second with fixed size too and scrollable?