How to Create a Fixed-Width Layout with CSS

  • google plus

In Brief...

A "fixed-width" layout is one in which the layout of the page is contained within a wrapper that doesn't adjust its size when the width of the browser changes.

In this how to, you'll learn how to create a 2-column fixed-width layout.

Take our Advanced CSS Training course for free.

See the Course Outline and Register

Instructions

  1. Start with the following simple web page containing four content areas: header, footer, menu, and content.
    
    <!DOCTYPE HTML>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Two-column fixed layout</title>
    </head>
    <body>
    <div id="wrapper">
    	<div id="header">
    		<h1>Two-column fixed layout</h1>
    	</div>
    	<div id="main">
    		<div id="menu">
    			<h2>Menu</h2>
    			<p>This column is fixed.</p>
    			<p>This column is fixed.</p>
    			<p>This column is fixed.</p>
    			<p>This column is fixed.</p>
    			<p>This column is fixed.</p>
    		</div>
    		<div id="content">
    			<h2>Content</h2>
    			<p>This column is fixed.</p>
    		</div>
    		<div class="clearer"></div>
    	</div>
    	<div id="footer">footer</div>
    </div>
    </body>
    </html>
    	
  2. Create a file for an external styesheet and link to it from the HTML using the following tag:
    
    <link href="fixed-two-column.css" rel="stylesheet">
    	
  3. Inside the stylesheet, start by resetting margins, padding, and borders:
    * {
    	margin:0;
    	padding:0;
    	border:0;
    }
  4. Next, add a border to the wrapper div:
    #wrapper {
    	border: 1px solid #000;
    }
  5. Add a bottom border to the header and give it some padding and a background:
    #header {
    	border-bottom: 1px solid #000;
    	padding: 10px;
    	background-color: #eee;
    }
  6. Float the menu nav left so that the content article will come up to its right edge. We'll also add some other styles to make it more readable and give it a width of 180px and padding of 10px, which gives it a total width of 200px: 180px + (2 x 10px):
    #menu {
    	width: 180px;
    	float: left;
    	padding: 10px;
    	border-right: 1px solid #000;
    }
  7. Set the left margin of the content div to the total width of the menu div. We'll also add some other styles to make it look nicer:
    
    #content {
    	margin-left: 200px;
    	border-left: 1px solid #000;
    	padding: 10px;
    	line-height: 2em;
    }
    
  8. Use a the clearer div to force the main section to extend its height when the content div expands.
    
    .clearer {
    	clear: both;
    }
    		
  9. At this point, you have a fluid-width layout. To convert it to a fixed-width layout, simply add a fixed with to the #wrapper and set the margins to auto. Setting the margins to auto will cause the left and right margins to be equal no matter how wide the browser window is, which will cause your fixed-width layout to be positioned in the center of the browser.
    
    #wrapper {
    	border: 1px solid #000;
    	width: 950px;
    	margin: auto;
    }
    	
  10. The finished CSS should look like the following:
    
    * {
    	margin:0;
    	padding:0;
    	border:0;
    }
    #wrapper {
    	border: 1px solid #000;
    	width: 950px;
    	margin: auto;
    }
    #header {
    	border-bottom: 1px solid #000;
    	padding: 10px;
    	background-color: #eee;
    }
    #menu {
    	width: 180px;
    	float: left;
    	padding: 10px;
    	border-right: 1px solid #000;
    }
    #content {
    	margin-left: 200px;
    	border-left: 1px solid #000;
    	padding: 10px;
    	line-height: 2em;
    }
    .clearer {
    	clear: both;
    }
    
    		
    Open the HTML page in a browser. This code renders the following:
    2-column fluid

Author: Chris Minnick

Chris is a prolific author and trainer, and the CEO of WatzThis?. His published books include Writing Computer Code, JavaScript for Kids, Coding with JavaScript For Dummies, Beginning HTML5 and CSS3 For Dummies, Webkit For Dummies, CIW eCommerce Certification Bible, and New Riders' XHTML.

Discuss