How to centre a page with position: absolute but overflow: auto

Hi,

I want to centre (everything on) a 970px wide page whose header, sidebar, article and footer have position: absolute and overflow: hidden except for article whose overflow: auto (so that only the article scrolls if it’s longer than the page)

I used annevankesteren.nl example for the positioning. If I try to change absolute to relative (to the body), the article doesn’t scroll anymore.
(A side problem of the absolute value is that it cancels background-image: radial-gradient for the body.)

I tried to use akaxaka or [URL=“http://www.zachgraeve.com/2006/10/01/center-abosulte-position-div/”]zachgraeve’s example on the body or on a wrapper for centring but when I did everything except the header disappeared (because of position:).

Any (proven) solution ?

P.S. I favour HTML5 and CSS3

Are you trying to center horizontally as well as vertically?

Actually my first question is why does everything have AP?

Please post your code.

I want to centre horizontally. (I don’t think that vertically it would make much a difference, would it ?)
Everything is position: absolute in the example, I assume this is required for the scroll trick to work.


<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta http-equiv="content-language" content="en">
		<meta name="author" content="">
		<meta name="description" content="">
		<meta name="keywords" content="">
		
		<title></title>
		
		<!-- HTML5 tags support for IE http://remysharp.com/2009/01/07/html5-enabling-script/ http://html5shim.googlecode.com/svn/trunk/html5.js -->
		<!--[if lt IE 9]>
			<script type="text/javascript" src="http://domain.net/include/js/html5.js"></script>
		<![endif]-->
		
		<script type="text/javascript" src=""></script>
		<link href="" media="screen" rel="stylesheet" type="text/css" />
		<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
		<style type="text/css">
			body{
/*
			    position: absolute;
    			top: 0px;
    			left: 50%;
    			margin-left: -485px;
*/
				padding: 0px 15px 0px 15px;
				width: 970px;
				color: white;
				background-color:#000000; 
				background-image: -moz-radial-gradient(50% 300px, ellipse cover, #0028c2, #000000 70%); 
				background-image: -webkit-radial-gradient(50% 300px, ellipse cover, #0028c2, #000000 70%); 
				background-image: -o-radial-gradient(50% 50%, circle cover, #2213ab, #000029 33%);
				background-image: -ms-radial-gradient(50% 50%, circle cover, #2213ab, #000029 33%); 
				background-image: radial-gradient(50% 50%, circle cover, #2213ab, #000029 33%);
				background-repeat: no-repeat;
			}
			
			#header_main{
				position: absolute;
				left: 0px;
				right: 0px;
				top: 0px;
				height: 150px;
				overflow: hidden;
			}
			
			#section_main{
				overflow: auto;
				position: absolute;
				left: 215px;
				right: 0px;
				top: 150px;
				bottom: 35px;
				width: 730px;
				background-color: #eeefee;
				color: black;
			}
			
			#nav_main{
				background-color: #c37b00;
			}
			
			#nav_toc{
				position: absolute;
				overflow: hidden;
				top: 150px;
				bottom: 35px;
				left: 0px;
				width: 215px;
				background-color: #001665;
				opacity: 0.75;
				border: 1px solid white;
			}
			
			a{
				color: #c37b00;
				font-weight: bold;
			}
				
			pre{
				white-space: pre-line;
			}
			
			#footer_main{
				position: absolute;
				left: 0px;
				right: 0px;
				bottom: 0px;
				height: 35px;
				overflow: hidden;
			}
		</style>
		
	</head>

	<body>

		<header id="header_main">
			<img src="http://domain.net/include/images/logo/domain.net%20logo%20black%20notINT%20supsamp%20100px.png" alt="domain.net logo" title="Home & About domain.net" />
			domain.net
			tagline
			slogan

			<nav id="nav_main">
				category 1, category 2, category 3, category 4, 
			</nav>

		</header>


		<article id="article_main">
		
			<nav id="nav_toc">
				TOC
				<ul>
					<li><a href="#">What is this site about ?</a></li>
					<li><a href="#">Who runs this site ?</a></li>
					<li><a href="#">How is this site made ?</a></li>
					<li><a href="#">Privacy Policy</a></li>
					<li><a href="#">Legal Statement</a></li>
				</ul>
			</nav>

			<section id="section_main">
				
				<pre id="pre_main">
					<hgroup id="hgroup_main">
						<h1>About domain.net</h1>
					</hgroup>


<strong>• What is this site about ?</strong>

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vestibulum sollicitudin enim, condimentum aliquet est vehicula ac. 
Aliquam vitae imperdiet orci. Vivamus at nibh ut ipsum tempus vestibulum nec eget eros. Suspendisse vestibulum adipiscing ultricies. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Phasellus mi eros, mattis et pharetra vel, pharetra et nisl. Sed in elit vehicula sapien interdum hendrerit. Morbi lacus leo, ultricies eget ullamcorper in, bibendum et arcu. Nunc mollis condimentum ligula at facilisis. 
Nulla dui ipsum, semper non ultricies non, sodales et turpis. Praesent pretium tincidunt ipsum non auctor. Fusce rutrum est sed purus iaculis vel fringilla ipsum blandit. Quisque risus metus, eleifend nec condimentum ut, egestas eget enim. Donec et purus et turpis varius vestibulum. Nunc nunc ipsum, laoreet ac feugiat vitae, facilisis eu metus. Cras tristique gravida fringilla. 
Pellentesque mollis ante eget metus auctor adipiscing. Nam quis libero nec mauris vestibulum lacinia. Sed sit amet sapien in erat gravida mollis.

Morbi at metus orci, eu mattis enim. Phasellus nec arcu nisl. Sed consequat magna vel lacus facilisis facilisis at ac neque. Nullam pharetra consectetur sapien, placerat posuere ligula dapibus at. Integer et pellentesque arcu. Nulla eu ultrices sem. Donec id cursus libero. Phasellus eu mi quis nisi ultrices tempor et sollicitudin libero. Nam quam orci, rhoncus ut varius id, facilisis eget justo. Ut nec dui massa, in faucibus mauris. Cras non lacus sem, eu faucibus est. Proin aliquet, nibh vel aliquam mollis, ipsum lacus egestas erat, vehicula tempor augue nulla convallis odio. Nullam et enim ligula.

Phasellus fringilla condimentum orci, congue pretium nibh vulputate ac. In sed sollicitudin nisi. Cras id malesuada lorem. Suspendisse potenti. Pellentesque dictum accumsan lorem nec elementum. Aenean sit amet odio id nisi varius suscipit. Nunc consectetur convallis gravida. Ut facilisis gravida purus ac porta. Sed adipiscing urna vitae tellus aliquam vel tincidunt lectus lobortis. 
Aenean a risus nec lectus pellentesque rutrum a nec erat. Donec feugiat nisl vitae nisi ultrices bibendum. Nullam diam arcu, vestibulum a gravida eu, lacinia sed diam. Donec odio velit, aliquet a elementum nec, pulvinar a nunc. Pellentesque lorem lectus, condimentum id auctor in, condimentum in arcu. Cras augue urna, pharetra nec pharetra sit amet, vehicula vitae lectus. Ut tristique nisi at eros suscipit molestie. Donec dolor eros, dignissim ut ultricies non, tempus sed nibh. 
					<figure>
						<img src="" alt="" title="" />
						<figcaption>
							image description
						</figcaption>
					</figure>

					<time datetime="2011-07-13T18:28:05Z"></time>
				
				</pre>

			</section>

		</article>

		<footer id="footer_main">

			<aside>
				<a href="">About</a>
				<a href="">Sitemap</a>
				<a href="">Site bug ?</a>
				<abbr title="All Rights Reserved.">&copy;</abbr> domain 2011
			</aside>

		</footer>

	</body>

</html>

Everything is position: absolute in the example, I assume this is required for the scroll trick to work.

Hi,
That is what I suspected you were trying to do by making everything AP. There is really no reason to remove the entire page from the normal flow. Those are some old examples you have linked to. All you really need to do is make your header, footer, & sidebar as position:fixed

By doing that your main content will scroll on it’s own with the body scrollbar, that will also keep it on the far right side of the viewport where users expect it to be.

Here are a couple of examples with centered scrolling content.

Full width header-footer with centered sidebar and content
Centered layout with fixed header-footer-sidebar

Note:

  1. They are only for browsers supporting fixed position, it will work in all modern browsers as well as IE7+
  2. Keep your sidebar content to a minimum or add overflow:auto for added insurance
  3. Be sure to pad out the top and bottom of the scrolling content to preserve it from the fixed header-footer

Ok, but position:fixed is not the only thing I need to change, right ?
I looked at your examples but because there is a lot of code for other things, I can’t figure out what’s the basic code for centred scrolling content.

Okay I spent a long time adding all the code from your example that I thought was necessary, it works but it’s not perfect:
• I can’t find how to position the main content properly (#section_article_main), it should be at the right of the sidebar (opposite of your example). EDIT, just now I added margin, is it the appropriate solution ?
• The scroll bar is always present even if the page is longer than the content and even if I remove overflow-y:scroll;
#section_article_main does appear bellow the header and footer (I thought padding was supposed to prevent this) - this is a problem because it hides the background - is there a way to make it invisible below the header ?
• another issue (while I’m here): I can’t find how to position #nav_main at the bottom of #header_main


<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<meta http-equiv="content-language" content="en">
		<meta name="author" content="">
		<meta name="description" content="">
		<meta name="keywords" content="">
		
		<title></title>
		
		<!-- HTML5 tags support for IE http://remysharp.com/2009/01/07/html5-enabling-script/ http://html5shim.googlecode.com/svn/trunk/html5.js -->
		<!--[if lt IE 9]>
			<script type="text/javascript" src="http://domain.net/include/js/html5.js"></script>
		<![endif]-->
		
		<script type="text/javascript" src=""></script>
		<link href="" media="screen" rel="stylesheet" type="text/css" />
		<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
		
		<style type="text/css">
		
			html{
				height:100%;
				/* overflow-y:scroll; */ /*keep scrollbar position present if FF at all times*/
			}
			
			body{
				height: 100%;
    			margin: 0px;
    			padding: 0px;
				
				color: white;
				background-color:#000000; 
				background-image: -moz-radial-gradient(50% 300px, ellipse cover, #0028c2, #000000 70%); 
				background-image: -webkit-radial-gradient(50% 300px, ellipse cover, #0028c2, #000000 70%); 
				background-image: -o-radial-gradient(50% 50%, circle cover, #2213ab, #000029 33%);
				background-image: -ms-radial-gradient(50% 50%, circle cover, #2213ab, #000029 33%); 
				background-image: radial-gradient(50% 50%, circle cover, #2213ab, #000029 33%);
				background-repeat: no-repeat;
			}
			
			#header_main{
			    position:fixed;
			    z-index:10;
			    left: 0px;
			    top: 0px;
			    height: 150px;
			    width: 100%;
			    
			    text-align: center;
			}
			
			#section_header{
				margin: auto;
				width: 970px;
				
				border: red 1px solid;
				text-align: left;
			}
			
			#nav_main{
				text-align: right;
				
				background-color: #c37b00;
			}

			#article_main{
			    width: 970px;
    			min-height: 100%;
   				margin: 0 auto;
   				
   				border: green 1px solid;
   			}					
   				
			#section_article_main{
				width: 730px;
				padding:150px 0 35px 0; /*padding protects content from sliding under header & footer.*/
				
				background-color: #eeefee;
				color: black;
				border: yellow 2px dashed;
			}
			
			#nav_toc{
				float:left;/*IE8 needs a floated parent for the fixed div to kill RP/AP bug*/
				width: 215px;
			}
			
			#section_nav_toc{
			    position: fixed;
    			width: 214px;/*15px total width with left border*/ 
    			top: 150px;/*total header height*/
    			bottom: 35px;/*total footer height*/
    			overflow: auto;/*insure access to text at smaller viewport heights*/
    			
				background-color: #001665;
				opacity: 0.75;
				border: 1px solid white;			
			}
			
			a{
				color: #c37b00;
				font-weight: bold;
			}
				
			pre{
				white-space: pre-line;
			}
			
			#footer_main{
				position: fixed;
				z-index: 10;
				left: 0px;
				bottom: 0px;
				height: 35px;
				width: 100%;
				
				text-align: center;
			}
			
			#aside_footer_main{
				margin: auto;
				width: 970px;
				
				border: red 1px solid;
				text-align: left;
			}
		</style>
		
	</head>

	<body>

		<header id="header_main">
			<section id="section_header">
			
			<img src="http://domain.net/include/images/logo/domain.net%20logo%20black%20notINT%20supsamp%20100px.png" alt="domain.net logo" title="Home & About domain.net" />
			domain.net
			tagline
			slogan

			<nav id="nav_main">
				category 1, category 2, category 3, category 4, 
			</nav>

			</section>
		</header>


		<article id="article_main">
		
			<nav id="nav_toc">
				<section id="section_nav_toc">
				TOC
				<ul>
					<li><a href="#">What is this site about ?</a></li>
					<li><a href="#">Who runs this site ?</a></li>
					<li><a href="#">How is this site made ?</a></li>
					<li><a href="#">Privacy Policy</a></li>
					<li><a href="#">Legal Statement</a></li>
				</ul>
				</section>
			</nav>

			<section id="section_article_main">
				
				<pre id="pre_main">
					<hgroup id="hgroup_main">
						<h1>About domain.net</h1>
					</hgroup>


<strong>• What is this site about ?</strong>

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum vestibulum sollicitudin enim, condimentum aliquet est vehicula ac. 
Aliquam vitae imperdiet orci. Vivamus at nibh ut ipsum tempus vestibulum nec eget eros. Suspendisse vestibulum adipiscing ultricies. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Phasellus mi eros, mattis et pharetra vel, pharetra et nisl. Sed in elit vehicula sapien interdum hendrerit. Morbi lacus leo, ultricies eget ullamcorper in, bibendum et arcu. Nunc mollis condimentum ligula at facilisis. 
Nulla dui ipsum, semper non ultricies non, sodales et turpis. Praesent pretium tincidunt ipsum non auctor. Fusce rutrum est sed purus iaculis vel fringilla ipsum blandit. Quisque risus metus, eleifend nec condimentum ut, egestas eget enim. Donec et purus et turpis varius vestibulum. Nunc nunc ipsum, laoreet ac feugiat vitae, facilisis eu metus. Cras tristique gravida fringilla. 
Pellentesque mollis ante eget metus auctor adipiscing. Nam quis libero nec mauris vestibulum lacinia. Sed sit amet sapien in erat gravida mollis.

Morbi at metus orci, eu mattis enim. Phasellus nec arcu nisl. Sed consequat magna vel lacus facilisis facilisis at ac neque. Nullam pharetra consectetur sapien, placerat posuere ligula dapibus at. Integer et pellentesque arcu. Nulla eu ultrices sem. Donec id cursus libero. Phasellus eu mi quis nisi ultrices tempor et sollicitudin libero. Nam quam orci, rhoncus ut varius id, facilisis eget justo. Ut nec dui massa, in faucibus mauris. Cras non lacus sem, eu faucibus est. Proin aliquet, nibh vel aliquam mollis, ipsum lacus egestas erat, vehicula tempor augue nulla convallis odio. Nullam et enim ligula.

Phasellus fringilla condimentum orci, congue pretium nibh vulputate ac. In sed sollicitudin nisi. Cras id malesuada lorem. Suspendisse potenti. Pellentesque dictum accumsan lorem nec elementum. Aenean sit amet odio id nisi varius suscipit. Nunc consectetur convallis gravida. Ut facilisis gravida purus ac porta. Sed adipiscing urna vitae tellus aliquam vel tincidunt lectus lobortis. 
Aenean a risus nec lectus pellentesque rutrum a nec erat. Donec feugiat nisl vitae nisi ultrices bibendum. Nullam diam arcu, vestibulum a gravida eu, lacinia sed diam. Donec odio velit, aliquet a elementum nec, pulvinar a nunc. Pellentesque lorem lectus, condimentum id auctor in, condimentum in arcu. Cras augue urna, pharetra nec pharetra sit amet, vehicula vitae lectus. Ut tristique nisi at eros suscipit molestie. Donec dolor eros, dignissim ut ultricies non, tempus sed nibh. 
									
					<figure>
						<img src="" alt="" title="" />
						<figcaption>
							image description
						</figcaption>
					</figure>

					<time datetime="2011-07-13T18:28:05Z"></time>
				
				</pre>

			</section>

		</article>

		<footer id="footer_main">

			<aside id="aside_footer_main">
				<a href="">About</a>
				<a href="">Sitemap</a>
				<a href="">Site bug ?</a>
				<abbr title="All Rights Reserved.">&copy;</abbr> domain 2011
			</aside>

		</footer>

	</body>

</html>

Hard to get a follow up on this site, but I’ll try.

I solved my previous issues and have new ones:

• When I link to a bookmark on the page with href="# the scrolling is offset, the text where I set the id appears too high, it’s behind the header. Is there a fix ? with JS perhaps ?

• When the sidebar is longer than the page, the extra text is impossible to see. css-lab.com’s example write “overflow: auto;/insure access to text at smaller viewport heights/” but I don’t see how it does this. I don’t want the sidebar to scroll with the main content, it should have its own scrolling (when needed). How can I do this ?

You shouldn’t need to do anything more than add overflow:auto assuming that you have set the height using top and bottom and not with a real height.

Hm ok I’ve done it. I was hoping that I could keep a min-height but apparently it’s asking too much from CSS. Now I have to choose between the sidebar being always longer or always shorter than its content.

By the way, what exactly determines what moves on a page and what doesn’t ? I’ve looked at the properties of both and they are quite the same. Is it only the combination of longer/larger text with overflow:scroll/auto ?

You’ll need to clarify that statement as out of context it doesn’t really make much sense. If we are still talking about the fixed positioned sidebar then it can’t ever have a height or content will be unreachable should it be outside the viewport.

By the way, what exactly determines what moves on a page and what doesn’t ? I’ve looked at the properties of both and they are quite the same. Is it only the combination of longer/larger text with overflow:scroll/auto ?

Again you lost me a bit there :slight_smile: Only fixed elements do not scroll with the page. All other elements will scroll with the page. If you add overflow scroll to a fixed height box then you can scroll within that box but the whole box will still scroll with the rest of the document.

You may need to post a link or some current code so we can answer more specifically.

BEFORE:


            #section_nav_toc {
                position: fixed;
                width: 214px;/*15px total width with left border*/
                top: 215px;/*total header height + offset */
                overflow: auto;/*insure access to text at smaller viewport heights*/
                min-height: 347px; /* less padding */
                padding: 0px;
}

the height either was at minimum or fitting the content but there was no way to see it if it was longer than the page

AFTER:


#section_nav_toc {
                position: fixed;
                width: 214px;/*15px total width with left border*/
                top: 215px;/*total header height + offset */
                overflow: auto;/*insure access to text at smaller viewport heights*/
                height: 347px; /* less padding */
                padding: 0px;
}

the height is always the same and there’s a dedicated scroll bar, the element doesn’t scroll with the page

What I wished to have was a height matching its content and a scrollbar added when that content is longer than the page. Possible ? Update: seems not, it needs either a height or bottom, otherwise no scrollbar when longer than the page.

Hmmm well then I choose to do different from the css-lab.com example. Now the main content is fixed as well and has its own scroll bar. This solves the bookmark offset problem mentioned earlier as well as keeping the nice inner box shadow visible all the time - it might even have improved with IE.


            #section_article_main {
            	position: fixed;
            	overflow: auto;
            	margin-left: 215px;
            	top: 142px;
            	bottom: 55px;
                width: 725px;
                min-height: 428px;
                padding: 0px;
}

You shouldn’t use height or min-height with a position:fixed sidebar because once the users screen is smaller than the height then you will have content below the fold that is unreachable. Instead you should position the sidebar using a combination of top and bottom (no height needed) so that the sidebar expands and contracts with the page and the overflow:auto ensures that a scrollbar will appear when the container is too small to display all the content.

Fixed position elements are an accessibility nightmare and really should only be used in small doses.

I think I’d need to see the full working page that you have now (or at least the whole code) in order to make better suggestions and answer your other questions.

http://protostype.free.fr/index-sitepointversion2.php

If I use top and bottom the div is always going to be as long as the page and if there is little content it’s going to create a huge blank that i think is ugly.
I used min-height before, to default to a golden number ratio whenever possible.

Hi,

For modern browsers only (ie9+) you could set up a media query based on the height of the window and adjust the height of the fixed element.

e.g.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Untitled Document</title>
<style type="text/css">
.fixed {
	width:200px;
	top:100px;
	left:0;
	background:red;
	overflow:auto;
	height:500px;
}
 @media all and (max-height: 600px) {
.fixed { height:400px }
}
@media all and (max-height: 500px) {
.fixed { height:300px }
}
@media all and (max-height: 400px) {
.fixed { height:200px }
}
</style>
</head>

<body>
<div class="fixed">
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
		<p>test</p>
</div>
</body>
</html>


I don’t see any other options apart from using top and bottom as mentioned before (e.g. top:200px bottom :200px)

That rotated menu looks familiar:)

I’m afraid that media queries don’t help. I want the height to match the content, not the window.

Make your mind up :slight_smile: I already told you how to match the content but you said you didn’t want blank space.

If I use top and bottom the div is always going to be as long as the page and if there is little content it’s going to create a huge blank that i think is ugly.

Now you are saying you want to match the content div which you would do using top and bottom.
e.g.


#section_nav_toc {
    bottom: 55px !important;
    height:auto;
    top: 215px;
}

Or set a max-height with the bottom:55px:
e.g.


#section_nav_toc {
    bottom: 55px !important;
    height:auto;
    max-height: 347px;
    top: 215px;
}

That’s about all the combinations there are :slight_smile:

Maybe you misunderstood me. I always wanted height to match content. The blank I don’t want is the blank inside the div below the content, not below the div.

I chose a min, max and auto height, same for #section_article_main. I think I’m done with this thread. Thanks a lot Paul !

P.S. if you’re bored you’re welcome to help me with http://www.sitepoint.com/forums/showthread.php?787234-Fixing-a-style-sheet-for-IE-7-8-9

I’m back…
I noticed (in FF5) that the main content scroll bar can’t be scrolled if it’s clicked near the right menu, instead the cursor selects text.
It scrolls if clicked below or with mouse scroll.
Any fix ?

Pretty pathetic that I can’t edit my post after 30min because after having registered 7 years ago and made 83 posts I’m still considered a potential spammer.

The URL of the latest version: http://protostype.free.fr/index-dev.php

In Firefox and Webkit browsers (Safari and Chrome) the menu on the right is making the scrollbar initially inaccessible to the mouse. The scrollbar only comes to the front if #section_article_main is clicked. This is due to the menu’s li elements being higher in the stacking order and overlapping the scrollbar.

To fix this, try giving #section_article_main z-index: 10;