How to Create a Fluid-Width Layout with CSS

See CSS: Tips and Tricks for similar articles.

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

Related Articles

  1. Learn the Very Basics of CSS in One Minute
  2. How to Create a CSS External Style Sheet
  3. How to Align Text with CSS
  4. How to Create a Horizontal Navigation Menu with CSS
  5. How to Create a Fixed-Width Layout with CSS
  6. How to Remove Spacing Between Table Borders with CSS
  7. How to Set a Background Image with CSS
  8. How to Set Text Spacing and Placement in CSS
  9. How to Style a Table with CSS
  10. How to Create Boxes with Rounded Corners in CSS
  11. How to Create a Vertical Navigation Menu with CSS
  12. How to Use the CSS Opacity Property
  13. How to Use Multiple Background Images with CSS
  14. Absolute Positioning with CSS
  15. How to Use the CSS Border Shorthand Property
  16. How to Create CSS Button Links
  17. How to Create a Fluid-Width Layout with CSS (this article)
  18. How to Set Text and Background Color with CSS
  19. How to Create a CSS Embedded Style Sheet
  20. How to Add Inline Styles to CSS
  21. How to Create a Border with CSS
  22. How to Use the CSS Padding Shorthand Property
  23. How to Create a Fly-Out Menu with CSS
  24. How to Use CSS Media Queries in Responsive Design
  25. How to Adjust Margins with CSS
  26. How to Use the CSS Background Shorthand Property
  27. How to Create a Form without Tables Using CSS
  28. How to Modify Fonts in CSS
  29. How to Create a Drop-Down Menu with CSS
  30. How to Apply Padding with CSS
  31. Fixed Positioning with CSS
  32. How to Use CSS Transitions
  33. How to Use the CSS list-style Shorthand Property
  34. How to Change Text Style in CSS
  35. How to Create CSS Sprites
  36. How to Use CSS with Different Media Types
  37. How to Import Style Sheets with @import in CSS
  38. How to Use the CSS White-Space Property
  39. How to Use the CSS Z-index Property
  40. How to Create Drop Shadows with the box-shadow Property in CSS3