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.
- 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>
- 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">
- Inside the stylesheet, start by resetting margins, padding, and borders:
* { margin:0; padding:0; border:0; }
- Next, add a border to the wrapper div:
#wrapper { border: 1px solid #000; }
- 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; }
- 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; }
- 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; }
- Use a the clearer div to force the main section to extend its height when the content div expands.
.clearer { clear: both; }
- The finished CSS should look like the following:
Open the HTML page in a browser. This code renders 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; }
Related Articles
- Learn the Very Basics of CSS in One Minute
- How to Create a CSS External Style Sheet
- How to Align Text with CSS
- How to Create a Horizontal Navigation Menu with CSS
- How to Create a Fixed-Width Layout with CSS
- How to Remove Spacing Between Table Borders with CSS
- How to Set a Background Image with CSS
- How to Set Text Spacing and Placement in CSS
- How to Style a Table with CSS
- How to Create Boxes with Rounded Corners in CSS
- How to Create a Vertical Navigation Menu with CSS
- How to Use the CSS Opacity Property
- How to Use Multiple Background Images with CSS
- Absolute Positioning with CSS
- How to Use the CSS Border Shorthand Property
- How to Create CSS Button Links
- How to Create a Fluid-Width Layout with CSS (this article)
- How to Set Text and Background Color with CSS
- How to Create a CSS Embedded Style Sheet
- How to Add Inline Styles to CSS
- How to Create a Border with CSS
- How to Use the CSS Padding Shorthand Property
- How to Create a Fly-Out Menu with CSS
- How to Use CSS Media Queries in Responsive Design
- How to Adjust Margins with CSS
- How to Use the CSS Background Shorthand Property
- How to Create a Form without Tables Using CSS
- How to Modify Fonts in CSS
- How to Create a Drop-Down Menu with CSS
- How to Apply Padding with CSS
- Fixed Positioning with CSS
- How to Use CSS Transitions
- How to Use the CSS list-style Shorthand Property
- How to Change Text Style in CSS
- How to Create CSS Sprites
- How to Use CSS with Different Media Types
- How to Import Style Sheets with @import in CSS
- How to Use the CSS White-Space Property
- How to Use the CSS Z-index Property
- How to Create Drop Shadows with the box-shadow Property in CSS3