0% found this document useful (0 votes)
26 views

CSS Horizontal Align

The document discusses different methods for horizontally aligning block elements in CSS, including: 1. Centering elements using the margin property by setting left and right margins to auto. 2. Left and right aligning using absolute positioning and setting the element's position property. 3. Left and right aligning using the float property. The document also notes some cross-browser compatibility issues and ways to avoid problems in older browsers, such as setting doctypes and default body margins and padding.

Uploaded by

Ankit Saha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

CSS Horizontal Align

The document discusses different methods for horizontally aligning block elements in CSS, including: 1. Centering elements using the margin property by setting left and right margins to auto. 2. Left and right aligning using absolute positioning and setting the element's position property. 3. Left and right aligning using the float property. The document also notes some cross-browser compatibility issues and ways to avoid problems in older browsers, such as setting doctypes and default body margins and padding.

Uploaded by

Ankit Saha
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

8/4/13

CSS Horizontal Align

Search w3schools.c Select Language HOME HTML C SS JAVASC RIPT JQUERY XML ASP.NET PHP SQL MORE...
R EFER ENC ES | EXAMPLES

Get Certified
Study Web Technologies and get a diploma at w3schools.com

CSS Basic
CSS HOME CSS Introduction CSS Syntax CSS Id & Class CSS How To

CSS Horizontal Align


Previous Next Chapter

CSS Styling
Styling Backgrounds Styling Text Styling Fonts Styling Links Styling Lists Styling Tables

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: <h1> <p> <div> For aligning text, see the CSS Text chapter. In this chapter we will show you how to horizontally align block elements for layout purposes.

Related Searches: Container Div HTML And XHTML Font Color W eb Design Services Logo Design Services Block Elements Layout W ith CSS W eb Publishing

CSS Box Model


CSS Box Model CSS Border CSS Outline CSS Margin CSS Padding

CSS Advanced
CSS Grouping/Nesting CSS Dimension CSS Display CSS Positioning CSS Floating CSS Align CSS Pseudo-class CSS Pseudo-element CSS Navigation Bar CSS Image Gallery CSS Image Opacity CSS Image Sprites CSS Media Types CSS Attribute Selectors CSS Summary

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 IE8 and earlier, 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
. c e n t e r { m a r g i n l e f t : a u t o ; m a r g i n r i g h t : a u t o ; w i d t h : 7 0 % ; b a c k g r o u n d c o l o r : # b 0 e 0 e 6 ; }
Try it yourself Tip: Aligning has no effect if the width is 100%.

CSS Examples
CSS Examples CSS Quiz CSS Certificate

Note: In IE5 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:

CSS References
CSS Reference CSS Selectors CSS Reference Aural CSS Web Safe Fonts CSS Units CSS Colors CSS Color Values CSS Color Names CSS Color HEX

Example
. r i g h t { p o s i t i o n : a b s o l u t e ; r i g h t : 0 p x ; w i d t h : 3 0 0 p x ; b a c k g r o u n d c o l o r : # b 0 e 0 e 6 ; }
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 a problem with IE8 and earlier, 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, IE8 and earlier versions 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:

w3schools.com/css/css_align.asp

1/3

8/4/13
Example

CSS Horizontal Align

b o d y { m a r g i n : 0 ; p a d d i n g : 0 ; } . c o n t a i n e r { p o s i t i o n : r e l a t i v e ; w i d t h : 1 0 0 % ; } . r i g h t { p o s i t i o n : a b s o l u t e ; r i g h t : 0 p x ; w i d t h : 3 0 0 p x ; b a c k g r o u n d c o l o r : # b 0 e 0 e 6 ; }
Try it yourself

Left and Right Aligning Using the float Property


One method of aligning elements is to use the float property:

Example
. r i g h t { f l o a t : r i g h t ; w i d t h : 3 0 0 p x ; b a c k g r o u n d c o l o r : # b 0 e 0 e 6 ; }
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 a problem with IE8 and earlier when using the float property. If the !DOCTYPE declaration is missing, IE8 and earlier versions 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
b o d y { m a r g i n : 0 ; p a d d i n g : 0 ; } . r i g h t { f l o a t : r i g h t ; w i d t h : 3 0 0 p x ; b a c k g r o u n d c o l o r : # b 0 e 0 e 6 ; }
Try it yourself

Previous

Next Chapter

W3Schools' Online Certification


The perfect solution for professionals who need to balance work, family, and career building. More than 10 000 certificates already issued!

Get Your Certificate

w3schools.com/css/css_align.asp

2/3

8/4/13

CSS Horizontal Align


The HTML Certificate documents your knowledge of HTML. The HTML5 Certificate documents your knowledge of advanced HTML5. The CSS Certificate documents your knowledge of advanced CSS. The JavaScript Certificate documents your knowledge of JavaScript and HTML DOM. The jQuery Certificate documents your knowledge of jQuery. 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).

Top 10 Tutorials
HTML Tutorial HTML5 Tutorial C SS Tutorial C SS3 Tutorial JavaScript Tutorial jQuery Tutorial SQL Tutorial PHP Tutorial ASP.NET Tutorial XML Tutorial

Top 10 References
HTML/HTML5 Reference C SS 1,2,3 Reference C SS 3 Browser Support JavaScript HTML DOM XML DOM PHP Reference jQuery Reference ASP.NET Reference HTML C olors

Examples
HTML Examples C SS Examples XML Examples JavaScript Examples HTML DOM Examples XML DOM Examples AJAX Examples ASP.NET Examples Razor Examples ASP Examples SVG Examples

Quizzes
HTML Quiz HTML5 Quiz XHTML Quiz C SS Quiz JavaScript Quiz jQuery Quiz XML Quiz ASP Quiz PHP Quiz SQL Quiz

Color Picke

Statistics

Browser Stat Browser OS Browser Disp

RE P O RT E RRO R

HO ME

TO P

P RI N T

FO RU M

A BO U T

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use and privacy policy. C opyright 1999-2013 by Refsnes Data. All Rights Reserved.

w3schools.com/css/css_align.asp

3/3

You might also like