CSS Horizontal Align
In CSS, several properties are used to align elements horizontally.
Aligning Block Elements
A block element is an element that takes up the full width available, and
has a line break before and after it.
Examples of block elements:
For aligning text, see the
CSS Text chapter.
In this chapter we will show you how to horizontally align block elements for
layout purposes.
Center Aligning Using the margin Property
Block elements can be aligned by setting the left and right margins to
"auto".
Note: Using margin:auto will not work in Internet Explorer, unless a !DOCTYPE
is declared.
Setting the left and right margins to auto specifies that they should split the available
margin equally. The result is a centered element:
Example
.center
{
margin-left:auto;
margin-right:auto;
width:70%;
background-color:#b0e0e6;
} |
Try it yourself »
|
Tip: Aligning has no effect if the width is 100%.
Note: In IE 5 there is a margin handling bug for block elements. To
make the example above work in IE5, add some extra code. Try it yourself
Left and Right Aligning Using the position Property
One method of aligning elements is to use absolute positioning:
Example
.right
{
position:absolute;
right:0px;
width:300px;
background-color:#b0e0e6;
} |
Try it yourself »
|
Note: Absolute positioned elements are removed from the
normal flow, and can overlap elements.
Crossbrowser Compatibility Issues
When aligning elements like this, it is always a good idea to predefine
margin and padding for the <body> element. This is to avoid visual differences
in different browsers.
There is also another problem with IE when using the position property. If
a container element (in our case <div class="container">) has a
specified width,
and the !DOCTYPE declaration is missing, IE will add a 17px margin on
the right side. This seems to be space reserved for a scrollbar. Always set the !DOCTYPE
declaration when using the position property:
Example
body
{
margin:0;
padding:0;
}
.container
{
position:relative;
width:100%;
}
.right
{
position:absolute;
right:0px;
width:300px;
background-color:#b0e0e6;
} |
Try it yourself »
|
Left and Right Aligning Using the float Property
One method of aligning elements is to use the float property:
Example
.right
{
float:right;
width:300px;
background-color:#b0e0e6;
} |
Try it yourself »
|
Crossbrowser Compatibility Issues
When aligning elements like this, it is always a good idea to predefine
margin and padding for the <body> element. This is to avoid visual differences
in different browsers.
There is also another problem with IE when using the float property. If
the !DOCTYPE declaration is missing, IE will add a 17px margin on
the right side. This seems to be space reserved for a scrollbar. Always set the !DOCTYPE
declaration when using the float property:
Example
body
{
margin:0;
padding:0;
}
.right
{
float:right;
width:300px;
background-color:#b0e0e6;
} |
Try it yourself »
|
Start Creating a stunning, Flash website. It's easy and free!
Wix.com offers you a simple, powerful, drag & drop editing platform to create stunning Flash websites, layouts, and more.
With added e-commerce features such as search engine visibility and professional tools, Wix is the ultimate solution for creating a spectacular site.
 |
W3Schools' Online Certification Program
The perfect solution for professionals who need to balance work, family, and career building.
More than 4500 certificates already issued!
|
The HTML Certificate documents your knowledge of HTML, XHTML, and CSS.
The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM.
The XML Certificate documents your knowledge of XML, XML DOM and XSLT.
The ASP Certificate documents your knowledge of ASP, SQL, and ADO.
The PHP Certificate documents your knowledge of PHP and SQL (MySQL).
|