• google plus

How to Create a Fluid-Width Layout with CSS

Pages that are designed to use the full width of the browser are often described as having a "fluid" or "liquid" layout. That's because one or more of the columns must be automatically resized according to the size of the browser window.

In this how to, you'll learn how to create a fluid-width layout.

Pages that are designed to use the full width of the browser are often described as having a "fluid" or "liquid" layout. That's because one or more of the columns must be automatically resized according to the size of the browser window.

In this how to, you'll learn how to create a fluid-width layout.

  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 fluid layout</title>
    </head>
    <body>
    <div id="wrapper">
    	<div id="header">
    		<h1>Two-column fluid 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 fluid.</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="fluid-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. The finished CSS should look like the following:
    
    * {
    	margin:0;
    	padding:0;
    	border:0;
    }
    #wrapper {
    	border: 1px solid #000;
    }
    #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.
About Webucator

Webucator provides instructor-led training to students throughout the US and Canada. We have trained over 90,000 students from over 16,000 organizations on technologies such as Microsoft ASP.NET, Microsoft Office, Azure, Windows, Java, Adobe, Python, SQL, JavaScript, Angular and much more. Check out our complete course catalog.