WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You

WebArtz - The Web Design Forum
Welcome to WebArtz, Guest!

WebArtz is a nice place for discussions related to web designing and coding. We help our members to code their own website templates with HTML and CSS. We give them advice on various issues.

To know more about WebArtz Forum, visit the About Us page.

At the moment, you are viewing the forum as a guest. As a guest you can't make post and participate in discussions. You need to register and become a member of the forum. Click the register link below and become a part of this forum.

Thank You


You are not connected. Please login or register

View previous topic View next topic Go down  Message [Page 1 of 1]

1Accespted Centering DIV's Sat Jul 17, 2010 6:14 am

RockerMan

RockerMan
Technician
Technician
Centering DIV's
Tricks to center align DIV elements


OK so I get alot of questions on how to center DIV's. Here is a quick tutorial to center DIV's Horizontal and vertical.

So it is as easy as applying a margin to the div:
This will center it horizontal...
Code:
.className{
margin:0 auto;
width:200px;
height:200px;
}
To center a div only horizontally, you need to specify a width, and an auto value for the left and right margins (you do remember the shorthand declarations in CSS don’t you?). This method works on block level elements (divs, paragraphs, h1, etc). To apply it to inline elements (like hyperlinks and images), you need to apply one additional rule – display:block.

To center it both ways it is a little more tricky, you will need the exact dimensions of the div...
Code:
.className{
width:300px;
height:200px;
position:absolute;
left:50%;
top:50%;
margin:-100px 0 0 -150px;
}

By positioning the element absolutely, we can detach it from its surroundings and specify its position in relation to the browser window. Offsetting the div by 50% from the left and the top part of the window, you have its upper-left corner precisely at the center of the page. The only thing we are left to do is to move the div to the left and to the top with half its width and height with a negative margin, to have it perfectly centered.

Thanks for reading!

If you have any questions please just ask me via PM or through the forums Wink

Mike Like a Star @ heaven

Notice : This tutorial is copyrighted by WebArtz Forum. You may not publish it on anywhere without written permission from the administrators.

http://www.graphics-post.com/

2Accespted Re: Centering DIV's Fri Jul 23, 2010 11:25 pm

ankillien

ankillien
Administrator
Administrator
Looks helpful for those who are learning CSS Very Happy
Tutorial Accepted

3Accespted Re: Centering DIV's Sat Jul 24, 2010 12:29 am

RockerMan

RockerMan
Technician
Technician
Thank you Smile

http://www.graphics-post.com/

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum