www.webdeveloper.com
Results 1 to 4 of 4

Thread: Fixed position relative to parent container

  1. #1
    Join Date
    Sep 2011
    Posts
    31

    Fixed position relative to parent container

    I did a lot of research on this but apparently it's not possible with pure html/css as position:fixed only fixes it to browser window, so I thought I'd ask in this section.
    I have a page container, and inside it are two sections. The left section is a vertical nav bar with some links, while the right section is the actual page content. I want the right side to be scrollable, but the left side to be fixed to the page container (ie won't scroll when I scroll that page), but be able to move when I scroll the entire browser window.

    Code:
    <div id="pagecontainer">
    
        <div id="linkspage">
    
            <div class="sidelinks"> <!--UNSCROLLABLE-->
                <a href="#page1" class="link">Link 1</a>
                <p>
                <a href="#page2" class="link">Link 2</a>
                <p>
                <a href="#page3" class="link">Link 3</a>
                <p>
                <a href="#page4" class="link">Link 4</a>
                <p>
            </div>
    
            <div class="linkscontent"> <!--SCROLLABLE-->
                content of links page
            </div>
    
        </div>
    
        //OTHER PAGES
    
    </div>
    I've already tried to set #pagecontainer and #linkspage to position:relative, and .sidelinks to position:absolute, that doesn't do anything as far as I can tell... or it just doesn't do what I want.

    Here's a picture:

    fixed.png

    Thanks.

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787

  3. #3
    Join Date
    Mar 2009
    Posts
    492
    Assuming you have used CSS to arrange both divs to be side by side in the parent container, you might try something like:

    Code:
    window.onload = function(){
       var side, content;
        side = document.getElementById('sidelinks');
        content = document.getElementById('linkscontent'); //to be scrolled
        content.style.display = "none";  //remove for measurement
        content.style.height = side.offsetHeight + 'px';
        content.style.overflow = "scroll";
        content.style.display = "block"; //make visible again
    }

  4. #4
    Join Date
    Dec 2012
    Location
    NSW Australia
    Posts
    73
    Maybe this will be an answer to your request.

    I would like to remind you that this can be achieved without JavaScript and it is always better to do things with css first.

    HTML Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <title>odd ball</title>
    <style>
    .page{background-color:lime;margin:1em;padding:1em;position:relative;max-width:52em;}.stuf1{position:relative;background-color:red;height:30em}.link1{position:absolute;width:10em;top:1px;left:1px;background-color:#CCC}.tent1{position:absolute;width:40em;height:30em;top:1px;left:11em;background-color:pink;overflow:scroll}.stuf2{position:relative;background-color:blue;height:30em}.link2{position:absolute;width:10em;top:1px;left:1px;background-color:#eee}.tent2{position:absolute;width:40em;height:30em;top:1px;left:11em;background-color:purple;overflow:scroll}.stuf3{position:relative;background-color:green;height:30em}.link3{position:absolute;width:10em;top:1px;left:1px;background-color:#999}.tent3{position:absolute;width:40em;height:30em;top:1px;left:11em;background-color:yellow;overflow:scroll}
    </style>
    </head>
    <body>
    <div class="page">
        <div class="stuf1">
    	    <div class="link1">
    		    <ul>
    			    <li>link one</li>
    				<li>link two</li>
    				<li>link three</li>
    				<li>link four</li>
    				<li>link five</li>
    			</ul>
    		</div>
    		<div class="tent1">
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 1</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 2</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 3</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 4</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 5</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 6</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 7</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 8</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 9</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 10</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 11</p>
    		</div>
    	</div>
    	<hr />
    	<div class="stuf2">
    	    <div class="link2">
    		    <ul>
    			    <li>link one</li>
    				<li>link two</li>
    				<li>link three</li>
    				<li>link four</li>
    				<li>link five</li>
    			</ul>
    		</div>
    		<div class="tent2">
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 1</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 2</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 3</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 4</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 5</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 6</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 7</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 8</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 9</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 10</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 11</p>
    		</div>
    	</div>
    	<hr />
    	<div class="stuf3">
    	    <div class="link3">
    		    <ul>
    			    <li>link one</li>
    				<li>link two</li>
    				<li>link three</li>
    				<li>link four</li>
    				<li>link five</li>
    			</ul>
    		</div>
    		<div class="tent3">
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 1</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 2</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 3</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 4</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 5</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 6</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 7</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 8</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 9</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 10</p>
    		    <p>content of links page</p><p>content of links page</p><p>content of links page 11</p>
    		</div>
    	</div>
    </div>
    </body>
    </html>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles