LIBRARY: Tutorials Reviews Interviews Editorials Features Business Authors RSS Feed

Simple CSS Centered Layout

Simple CSS Centered Layout
A Creative COW HTML Tutorial


Fernando Mol: Simple CSS Centered Layout
Fernando Mol
Fernando Mol
Monterrey, Mexico

©Copyright 2009 Fernando Mol and Creativecow.net. All Rights Reserved

Article Focus:
This tutorial from CreativeCOW leader, Fernando Mol explains the principles of how to create an horizontally centered layout using HTML and CSS.


Let's start with some basic HTML code. If you use Dreamweaver you can just create a new document and you'll get a blank page, but if you go to CODE VIEW you'll find your document already has some code. If you need more info on how to edit CSS in Dreamweaver you can read the "CSS in Adobe Dreamweaver" tutorial by Abraham Chaffin.

Here's how the starting HTML code should look. I already added it a TITLE.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSS centered layout</title>
</head>

<body>
</body>

</html>

 

Now, let's add some plain text inside the <BODY> tag.

 

<body>
This is the main content in plain text
</body>

 

If you open the document in a browser, the text should show at the top left corner.



The first step to center the content will be to place it inside a container DIV. I'll assign an ID of "wrapper" to the DIV, so later I can add a specific CSS rule for it.

 

<body>
<div id="wrapper">This is the main content in plain text</div>
</body>

 

By now, the content looks the same if you preview it in a browser, but structurally it's very different. Let's see what can we do with a little CSS.

The CSS rule should be inserted inside the document HEADER. We'll add a rule for the "wrapper" DIV, giving it a color of #F00, so we can see what's happening.

 

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>CSS centered layout</title>
<style type="text/css">
<!--
#wrapper {
background-color: #F00;
}
-->
</style>

</head>

 

We can preview the document now and see the red DIV from side to side of the browser window.


Now we can define a fixed width for the DIV. This is very important, otherwise the DIV will be always expanded to the width of the browser window and we'll never be able to see it centered. I'll give it 400px, kind of small for the real world, but good for the purpose of this tutorial.

 

#wrapper {
background-color: #F00;
width: 400px;
}

Finally, we add a left and right auto margins.

 

#wrapper {
background-color: #F00;
width: 400px;
margin-right: auto;
margin-left: auto;


}

That's it! You can place whatever content inside the "wrapper" DIV and it will be horizontally displayed at the center of your browser window.

###

Comments

thank you nice tut! i think
by Jay Niesler
thank you nice tut! i think i need to get into css. thats my problem i think -_-
it can be displayed
by Fernando Mol
it seems that the word "position" can't be displayed correctly.

Now I have a lot of comments on the tutorial (all by me)!

Anyway, if you give the CSS attribute of "position" with a value of "relative" to the centered div, all the inside absolutely positioned elements will be adjusted to the centered div.
again
by Fernando Mol
relative;

Fe de erratas
by Fernando Mol
To align an absolute positioned element inside a centered div you need to add to the centered div the relative; attribute.
Advanced centering?
by Fernando Mol
There's no way to center elements vertically only with CSS2 yet (we'll have to wait to the next generation of browsers).

You can, however, use a table, adding a vertical-align:middle for the table cell.

There are some complex methods on the web, not necessarily compatible with all browsers.

To align an absolute positioned element inside a centered div you need to add to the centered div the relative; attribute.
Vertically centered?
by Andre Taranto
How to make it vertically centered?
I tried:
margin-bottom: auto;
margin-top: auto;

But nothing happens...
Thanx


Related Articles / Tutorials:
Web Design
Full Page Overlay WindowFull Page Overlay Window

In this tutorial you will learn how to create a full page pop-up window that overlays an existing html page. This technique is used often for displaying a video, movie file, picture, or form when a user clicks on an element in a web page.

Tutorial
Abraham Chaffin
Web Design
Start Using MySQL with PHPStart Using MySQL with PHP

Learn about MySQL and how to implement its functionality into your web site allowing you to create dynamic content built from databases. In this tutorial you will learn some basics of using MySQL, PHPMyAdmin, Database Management Systems, and how to display MySQL content in a web page.

Tutorial
Abraham Chaffin
Web Design
Embedding Quicktime MoviesEmbedding Quicktime Movies

Learn the basics and advanced methods of embedding Quicktime Movies into your web site, blog or any html page. Find out what it takes to maintain cross platform compatibility when publishing all your Quicktime Videos. A more advanced technique of using Dreamweaver CS3 to embed your movie using ActiveX javascript embedding is also discussed.

Tutorial
Abraham Chaffin
Web Design
Reality ColdFusion MX: Flash MX Integration

Reality ColdFusion MX: Flash MX Integration

Author/Web developer Michael Hurwicz examines Reality ColdFusion MX: Flash MX Integration by Ben Forta, Randy Drisgill, Dennis Baldwin, Matt Tatam, and Derek Lu. This book is published by Macromedia press. The book discusses goals, logic and development process behind four projects, for which full source code is available on-line. A great way to get a jump start on ColdFusion/Flash integration. Recommended for intermediate Flash and ColdFusion users -- not for beginners.

Review
Michael Hurwicz
Web Design
Mike Gondek reviews Total Training's LiveMotion2

Mike Gondek reviews Total Training's LiveMotion2

CreativeCOW leader, Mike Gondek examines a 3 DVD training series called Live Motion 2.0 by Steve Holmes and published by Total Training.

Review
Mike Gondek
Web Design
Controling Adobe GoLive using Hot-Key settings

Controling Adobe GoLive using Hot-Key settings

In this article, Ron Lindeboom shows newer and other GoLive users how to make GoLive act the way you want it to. Have you ever wished you could set the hot-key short-cuts the way you wanted instead of the way that the Adobe engineers set them? Well you can do it and it's very easy to do. Here's how...

Tutorial
Ron Lindeboom
Recent Articles / Tutorials:
Adobe Premiere Pro
Adobe Premiere Pro: Edit Faster with Overlay Editing

Adobe Premiere Pro: Edit Faster with Overlay Editing

Editor, VFX artist, post-house owner, and plug-in developer Simon Ubsdell shows you how to edit faster and more easily using the very useful but not often known-about Overlay edit function in Premiere Pro. This Quick Tip tutorial also offers tips on grouping.

Tutorial
Simon Ubsdell
Art of the Edit
Always Be Editing: Sculptors & Bricklayers Revisited

Always Be Editing: Sculptors & Bricklayers Revisited

Do you edit like a sculptor, or like a bricklayer? It seems a simple enough question, but as longtime editor, post house owner, and VFX software developer Simon Ubsdell shows, the implications for how this affects the way you edit can be profound. His advice, regardless of where you land on the spectrum? Always be editing.


Simon Ubsdell
Audio Professionals
Hearing The Handmaid's Tale: Jane Tattersall's Sound Career

Hearing The Handmaid's Tale: Jane Tattersall's Sound Career

With Emmy, BAFTA, Motion Picture Sound Editors Golden Reel, Canadian Screen, and Directors Guild of Canada Awards among many more, Jane Tattersall shares insights into a career in sound editing entering its fourth decade with two young women just beginning their own careers in the field. Their conversation begins with Jane's work on the Hulu hit series, The Handmaid's Tale.

Feature, People / Interview
Kylee Peña
Art of the Edit
Indie Film Sound Editing: A This Guy Edits Tutorial

Indie Film Sound Editing: A This Guy Edits Tutorial

ACE Award-nominated picture editor Sven Pape ("This Guy Edits") speaks with Sundance Award-winning sound editor Ugo Derouard on The 5 Five Steps of Audio Post Production: Sound Editing, Spotting, Dialog Editing, Sound Design, and Sound Mixing, paying special attention to the specific needs of, and techniques that can work best for, independent filmmakers.

Tutorial
Sven Pape
MORE
© 2017 CreativeCOW.net All Rights Reserved
[TOP]