faqts : Computers : Programming : Languages : JavaScript : DHTML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

186 of 193 people (96%) answered Yes
Recently 7 of 10 people (70%) answered Yes

Entry

How do I center a block element like a DIV in NN6?

May 26th, 2000 08:58
Martin Honnen,


CSS 2 which NN6 implements allows that by
  - setting a width on the DIV
  - setting margin-left and margin-right to auto
That way the left and right margin of the DIV will be equal meaning the 
block level element becomes centered.
Example

<HTML>
<HEAD>
</HEAD>
<BODY>
<DIV STYLE="width: 50%; margin-left: auto; margin-right: auto;
            background-color: orange; color: white;"
>
JavaScript.FAQTs.com
</DIV>

<DIV STYLE="width: 200px; background-color: orange; color: white;"
     ONCLICK="this.style.marginLeft = this.style.marginRight = 'auto';"
>
Click to center this div.
</DIV>
</BODY>
</HTML>