lamelas
lamelas

Reputation: 872

A scrollable div under a div

Guys, I have the following HTML. Is it possible for the #underdiv to scroll under the #topdiv? I want to achieve the effect of having a list of items and to be able to scroll it up and down while keeping the #topdiv always visible on top of it. Can it be done just with the CSS or do I have to add some Javascript magic? I also have JQuery and JQueryMobile (as this is meant for an iOS device) included in the file but I kept them out to make the HTML look simpler.

Thanks in advance for helping me out!

<!DOCTYPE html> 
<html>
<head>
    <title>test</title>
    <style>
        #underdiv {
            background-color: red;
            position: relative;
            top: -40px;
            width: 80%;
            margin: 0 auto;
        }
        #topdiv {
            background-color: yellow;
        }
    </style>
</head>
<body>
    <div id="topdiv">
        <h1>Random title</h1>
        <p>This is a random paragraph bla bla bla bla yada yada yada</p>
    </div>
    <div id="underdiv">
        <ul>
            <li>Item 1</li>
            <li>Item 2</li>
            <li>Item 3</li>
            <li>Item 4</li>
            <li>Item 5</li>
            <li>Item 6</li>
            <li>Item 7</li>
            <li>Item 8</li>
            <li>Item 9</li>
            <li>Item 10</li>
            <li>Item 11</li>
            <li>Item 12</li>
            <li>Item 13</li>
            <li>Item 14</li>
            <li>Item 15</li>
            <li>Item 16</li>
            <li>Item 17</li>
            <li>Item 18</li>
            <li>Item 19</li>
            <li>Item 20</li>            
        </ul>
    </div>
</body>
</html>

Upvotes: 0

Views: 308

Answers (2)

Anupam
Anupam

Reputation: 8016

remove position:relative from underiv and add position:fixed to topdiv. Look at the resut here

Upvotes: 0

clairesuzy
clairesuzy

Reputation: 27624

it's possible, though the scrollbar disappears under too;

ul {
    margin: 0; 
    padding: 40px 0 0 30px;
}

#underdiv {
    background-color: red;
    width: 80%;
    margin: 0 auto;
    height: 200px;
    overflow: auto;
    margin-top: -40px;
}


#topdiv {
    background-color: yellow;
    padding: 1px; /* to stop margins collapsing */
    position: relative;
}

Upvotes: 1

Related Questions