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:
Production Designer Hannah Beachler on the Go Creative Show

Production Designer Hannah Beachler on the Go Creative Show

Film and broadcast production designer Hannah Beachler talks with Go Creative Show host Ben Consoli about her work on Creed, Moonlight, and the upcoming Black Panther. Hannah Beachler is a prolific production designer with an affinity for realistic design that emphasizes emotional drama. Over the past few years, Hannah’s designed some of the hottest films like, Miles Ahead, Creed, Moonlight, Beyonce’s Lemonade, and the much anticipated upcoming film Black Panther. Hannah and I discuss the role of a production designer, how she collaborates with the camera department, working with director Ryan Coogler and how she finds inspiration for each of her films.

Feature, People / Interview
Ben Consoli
The Other Side of GoPro's November 30, 2016 Announcement

The Other Side of GoPro's November 30, 2016 Announcement

When GoPro announced on November 30, 2016 that they would be cutting 200 full-time jobs, closing the entertainment division, and that their president would be leaving the company, the internet came alive with those who were predicting the company's demise. But there really is another side to the story and as is often the case, the loudest voices are not always the ones with the real story. In this article, Creative COW's co-founder, Ronald Lindeboom, looks at GoPro's announcement and gives his thoughts from his point of view. As Lindeboom concludes at the end of the story, "That GoPro stumbled is not surprising to me, what is surprising is that they had such a remarkable unbroken string of success until 2016."

Editorial, Feature, Business
Ronald Lindeboom
NVIDIA GPUs Speed Nickelodeon’s Animation Workflow on Albert

NVIDIA GPUs Speed Nickelodeon’s Animation Workflow on Albert

Premieres Friday, December 9, at 7:00 p.m. (ET/PT) -- Albert is Nickelodeon’s first original animated TV movie, telling the story of a tiny Douglas fir tree named Albert who loves Christmas and dreams of being the Empire City Christmas tree.

Feature, Project
Gail Laguna
Letters to the COW Team
Creative COW's Brand New News Dept. Features and Functions

Creative COW's Brand New News Dept. Features and Functions

Creative COW's co-founders have been quietly at work cleaning up and rebuilding the Creative COW News section. While they've been at work, they've been adding new features and functions that will make the news department much more useful in the days ahead. If it's been a while since you've visited the news section, visit it soon but read this short introduction to what it is and how things are working. We hope these new changes make your research and keeping on top of industry news much more productive.

Editorial, Feature, Business
Ronald Lindeboom
MORE
© 2017 CreativeCOW.net All Rights Reserved
[TOP]