Centering DIV Tags

PseudZ

Expert Member
Joined
May 28, 2008
Messages
1,235
Hellos
Im wondering if anyone knows anything about centering of div tags in html, using Visual Web Developer.
I hate using tables, they just annoying, i find it easier to use divs but they wont center on the screen when viewed in the browser..
Is there anyway to use divs, perhaps using divs inside one big centered table?
Or setting margin properties for the divs?
Any help is appreciated
Thanks
 

sn3rd

Expert Member
Joined
Jan 18, 2008
Messages
4,305
Sorry to say, but div tags' "align" attribute is deprecated, so you should use tables (although I get that you don't want to).
 

JAV

Well-Known Member
Joined
Mar 1, 2008
Messages
165
I assume you want to have the div be the "holder" for the main content of the page, then the following should work:

<div style="width:90%;margin-left:5%;">some text here</div>

Just remember the margin-left value should be halve of 100%-width%, i.e. (100% - width%)/2 = margin-left%
 

PseudZ

Expert Member
Joined
May 28, 2008
Messages
1,235
I do have them aligned in the centre
I make a holder ass said above and then place everything in side, then set position to relative and it works perfectly.. but in ie 6 only not in firefox or opera.. Is there a solution around this?
 

Deenem

Expert Member
Joined
Apr 20, 2005
Messages
1,724
<center><div style="width:80%;"> Hello World </div></center> , would work.

I hate tables too...
 

The Axe Dude

Senior Member
Joined
Feb 14, 2008
Messages
585
Another way to do it, in a style sheet of course :p, would be to have:

#divname
{
margin: 0px auto;
}

That will center the div :)
 

=Scorp1on=

Active Member
Joined
Nov 21, 2007
Messages
34
You can either do it with inline styles or with a seperate CSS sheet.


I don't use inline too much but for example:
HTML:
<div style="width: 80%; margin: 10px auto 0 auto;"</div>

Width can be set in pixels (900px - Good if you want a fixed website size can help in some cases) or percent (80% - if you want it to stretch to a certain percentage of the viewers screen)

Or with a seperate CSS file things can be much neater, example:

To link to your CSS you will enter the following between your <head></head> tags:
HTML:
<link rel="stylesheet" type="text/css" href="StyleSheet.css" />

And your style sheet will look as follows:
Code:
#container {
width: 900px;
margin: 10px auto 0 auto;
}

(Very Basic CSS. CSS can be very advanced and do wonders)

Then your HTML file should look like this:
HTML:
<!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>
    <title>Name</title>
    <link rel="stylesheet" type="text/css" href="StyleSheet.css" />
</head>

<body>
    <div id="container"> (This sets your div to work on all the text between this tag and the closing </div> with the id you made in the CSS [#container])
    <p> Lorem ipsum dolar sit amet... random latin...</p>
</div>
    
</body>

</html>

Edit: And after all that... you got it working lol. Have fun.
 

.Froot.

Executive Member
Joined
Aug 6, 2007
Messages
9,261
I usually use tables too, then just chuck div tags inside of them. Makes for easier development later on.
 

chiskop

Executive Member
Joined
Mar 17, 2006
Messages
9,214
Another way to do it, in a style sheet of course :p, would be to have:

#divname
{
width:800px;
margin: 0px auto;
}

That will center the div :)

This is what I would do, except that you have to declare the width of #divname.
 

PseudZ

Expert Member
Joined
May 28, 2008
Messages
1,235
Thanks :)
another thing is how would you go about putting all the divs inside one big table?
Say centre the table the align divs to the table
 

.Froot.

Executive Member
Joined
Aug 6, 2007
Messages
9,261
You could do that too, but I never do it in my sites. The outermost thing I have is a div and then a table inside with more div's.
 

The Axe Dude

Senior Member
Joined
Feb 14, 2008
Messages
585
As in:

<table>
<tr>
<td>
<div="divname"></div>
</td>
</tr>

</table>

?

Note: This is highly horrible and invalid :)
 
Top