Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

HTML Forum


You are currently viewing our HTML Forum as a guest. Please register to participate.
Login



Post a Project »

Find a Professional HTML Freelancer!

Find a Freelancer to help you with your HTML projects

FREE Outsourcing eBook!

Reply
Centering with absolute positioning
Old 11-09-2007, 10:48 PM Centering with absolute positioning
rareillusions's Avatar
Average Talker

Posts: 19
Trades: 0
I just started into web design, and I'm trying to center a layout with div layers, but I want the div layers to use absolute positioning on the layout, while still being centered.. but it's not working... help?
rareillusions is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-09-2007, 11:09 PM Re: Centering with absolute positioning
joder's Avatar
Flipotron

Posts: 6,442
Name: James
Location: In the ocean.
Trades: 0
Oh the horrors, I mean fun, of absolute positioning.

It's real easy to help you since all problems are the same. Providing a link or code would just hinder us.
joder is offline
Reply With Quote
View Public Profile
 
Old 11-10-2007, 12:02 AM Re: Centering with absolute positioning
vangogh's Avatar
Post Impressionist

Latest Blog Post:
Why Responsive Design?
Posts: 10,815
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
You won't be able to center something with absolute positioning beyond one screen resolution. When you absolutely position something you are telling it to always be in the exact same place no matter what.

You can use trial and error to position something in the center at one resolution, but as soon as you change resolution it won't be centered any more.

A better to center a layout horizontally on the screen is to wrap a div around everything.

<body>
<div id="wrapper">
the rest of your code here
</div>
</body>

and then use css to center it

div#wrapper {width:760px; margin: 0 auto}

The width is up to you, but you must specify a width. You can use %, em, px or any other valid unit of measurement, but you must use a width.

You only need to specify a margin-left and a margin-right and both need to be set to auto. I generally use the shortcut as I did above.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 11-10-2007, 09:30 AM Re: Centering with absolute positioning
rareillusions's Avatar
Average Talker

Posts: 19
Trades: 0
Thanks for your help, I'll try it
rareillusions is offline
Reply With Quote
View Public Profile
 
Old 11-10-2007, 11:25 AM Re: Centering with absolute positioning
joder's Avatar
Flipotron

Posts: 6,442
Name: James
Location: In the ocean.
Trades: 0
But your probably going to have more than one column. If you need more help just post the code and we will be glad to offer help
joder is offline
Reply With Quote
View Public Profile
 
Old 11-10-2007, 11:46 AM Re: Centering with absolute positioning
xprmnt's Avatar
Extreme Talker

Posts: 212
Name: GiorgosK
Location: Geoland.org - Greece
Trades: 0
As vangogh said no way to center position:absolute; elements (unless you want to use javascript !?!?)

... you can position the main element in the center and then you can absolute position the rest of the elements inside it and their positioning will be relative to that main element ...
__________________
GiorgosK
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

Last edited by xprmnt; 11-10-2007 at 12:02 PM.. Reason: javascript comment added
xprmnt is offline
Reply With Quote
View Public Profile Visit xprmnt's homepage!
 
Old 11-10-2007, 11:59 AM Re: Centering with absolute positioning
rareillusions's Avatar
Average Talker

Posts: 19
Trades: 0
that's what I want to do!! how do i do that?
rareillusions is offline
Reply With Quote
View Public Profile
 
Old 11-10-2007, 05:41 PM Re: Centering with absolute positioning
LadynRed's Avatar
Defies a Status

Posts: 10,017
Location: Tennessee
Trades: 0
Quote:
you can position the main element in the center and then you can absolute position the rest of the elements inside it and their positioning will be relative to that main element
BUT - ONLY if you set your main element to position:relative, otherwise any positioned elements INSIDE it would still be positioned relative to the BODY.
__________________
Web Goddess & Web Standards Evangelist :) - Tables Be Gone !!

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

LadynRed is offline
Reply With Quote
View Public Profile
 
Old 11-10-2007, 07:55 PM Re: Centering with absolute positioning
vangogh's Avatar
Post Impressionist

Latest Blog Post:
Why Responsive Design?
Posts: 10,815
Name: Steven Bradley
Location: Boulder, Colorado
Trades: 0
rareillusions do what I suggested above, but add position: relative to the wrapper div. Then you should be able to position things inside it that will use the wrapper div as the origin for your positioning.
__________________
l Search Engine Friendly Web Design |
Please login or register to view this content. Registration is FREE

l Tips On Marketing, SEO, Design, and Development |
Please login or register to view this content. Registration is FREE

l
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
vangogh is offline
Reply With Quote
View Public Profile Visit vangogh's homepage!
 
Old 11-10-2007, 08:33 PM Re: Centering with absolute positioning
xprmnt's Avatar
Extreme Talker

Posts: 212
Name: GiorgosK
Location: Geoland.org - Greece
Trades: 0
Yes of course LadyNred

rareillusions
try this sample code: (as VanGogh describes but with different approach to centering since I want vertical centering also)

Quote:
<html>
<head><title> Content relative to center </title>
<style>
body,html { height:100%;}
#wrapper{
position:relative;
border:1px solid red;
width: 300px;
margin-left:-150px;
left:50%;
height: 200px;
margin-top:-100px;
top:50%;
}
#one {
position: absolute;
top: -100px;
left:0px;
height: 30px;
width: 30px;
background-color: orange;
}
</style>
</head>
<body>

<div id="wrapper">
wrapper
<div id="one">one</div>
</div>

</body>
</html>
just remember:

this is not a "perfect center" a distance from top not always the same as the distance from bottom edge of the browser but I think its the best one can get with just css ...

in wrapper css
margin-left = - width/2 and
margin-top = - height/2
__________________
GiorgosK
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
xprmnt is offline
Reply With Quote
View Public Profile Visit xprmnt's homepage!
 
Old 11-11-2007, 08:58 AM Re: Centering with absolute positioning
rareillusions's Avatar
Average Talker

Posts: 19
Trades: 0
So, if i put position:relative in the wrapper css, would I just put the postion I want it in the wrapper text too or make a different place like xprmnt? and if I do that, would I use absolute postioning or would I use the margins like xprmnt?

Ok I tried it, and neither way worked, here's my css code

Quote:
div#wrapper {width:800px; margin: 0 auto
position: relative
margin-left:-150px;
left:50%;}
div#updates {position: absolute;
top: 10px;
left:0px;
width: 240px;}
div#sidebar {position: absolute;
top: 400px;
left:0px;
width: 180px;}
div#main {position: absolute;
top: 400px;
left:180px;
width: 300px;}

Last edited by rareillusions; 11-11-2007 at 09:49 AM..
rareillusions is offline
Reply With Quote
View Public Profile
 
Old 11-11-2007, 10:03 AM Re: Centering with absolute positioning
LadynRed's Avatar
Defies a Status

Posts: 10,017
Location: Tennessee
Trades: 0
It is not a good idea to use absolute positioning. It removes the elements from the document flow and IE6 (especially) has some nasty bugs related to absolute positioning.

You're much better off avoiding positioning like that and use floats and the normal document flow, the position only when absolutely necessary.
__________________
Web Goddess & Web Standards Evangelist :) - Tables Be Gone !!

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

LadynRed is offline
Reply With Quote
View Public Profile
 
Old 11-11-2007, 04:31 PM Re: Centering with absolute positioning
rareillusions's Avatar
Average Talker

Posts: 19
Trades: 0
But I have no idea on how to do that, so right now I'm going to stick with this until I get more advanced
rareillusions is offline
Reply With Quote
View Public Profile
 
Old 11-12-2007, 09:22 AM Re: Centering with absolute positioning
LadynRed's Avatar
Defies a Status

Posts: 10,017
Location: Tennessee
Trades: 0
You don't need advanced css to center a page, and you'll find that using the normal document flow is just easier - it's like swimming WITH the current as opposed to swimming AGAINST it.

You want to center a page ? It's easy:
http://www.simplebits.com/notebook/2...centering.html

or
http://css-discuss.incutio.com/?page...ngBlockElement

And for learning about the different types of layouts:
http://css-discuss.incutio.com/?page=CssLayouts
__________________
Web Goddess & Web Standards Evangelist :) - Tables Be Gone !!

Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE

LadynRed is offline
Reply With Quote
View Public Profile
 
Old 11-13-2007, 03:31 AM Re: Centering with absolute positioning
xprmnt's Avatar
Extreme Talker

Posts: 212
Name: GiorgosK
Location: Geoland.org - Greece
Trades: 0
Quote:
Originally Posted by rareillusions View Post
So, if i put position:relative in the wrapper css, would I just put the postion I want it in the wrapper text too or make a different place like xprmnt? and if I do that, would I use absolute postioning or would I use the margins like xprmnt?

Ok I tried it, and neither way worked, here's my css code
The code I posted was actually tested in IE6,7 Firefox 2, Opera 9 and IT DOES WORK ...

The code effectivelly puts a big square block in the middle and a smaller one RELATIVE to that ...

what did you want it to do ??? maybe I misunderstood ...
,
__________________
GiorgosK
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
xprmnt is offline
Reply With Quote
View Public Profile Visit xprmnt's homepage!
 
Old 11-16-2007, 05:26 PM Re: Centering with absolute positioning
rareillusions's Avatar
Average Talker

Posts: 19
Trades: 0
I think your telling me the right way,I think it's just all messed up.

Here's the first. With this, you can still see the header picture, but all the text is pushed down, and won't move up...
P.s. Both codes really aren't as long as they look, and there are some missing HTML codes, but I'll add those later
Quote:
<style type="text/css">
div#wrapper {width:800px; margin: 0 auto
position: relative; left:50%;
background: #f2f5ca url('http://i214.photobucket.com/albums/cc286/rare-illusions/layouts/layout11table.jpg') repeat-y;
}
#header{
width:800px;
height:600px;
margin:0 auto;
background: transparent url('http://i214.photobucket.com/albums/cc286/rare-illusions/layouts/layout11copy.jpg');}
div#updates {position: absolute;
top: 0px;
left: 110px;
position: relative;
float:left;
width: 240px;}
div#sidebar {position: absolute;
left: 80px;
top:100px;
position: relative;
float:left;
width: 240px;}

div#main {position: absolute;
top: 400px;
left: 180px;
width: 500px;}
</style>
<center>
<body>
<div id="wrapper">
<div id="header">
<img src="http://i214.photobucket.com/albums/c...yout11copy.jpg">
<center>
<div id="wrapper">
<div id="updates">
<h5>Updates</h5><p class="updatetext" align="justify">
Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates

</p></div>
<div id="wrapper">
<div id="sidebar" >
Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar
Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar

</div>
<div id="main">
Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main
Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main

</p>
</div>
</tr></td></body></html>
And here's the other code where you can't see the picture, but the text is in the right place. I'll red the code that I removed

Quote:
<style type="text/css">
div#wrapper {width:800px; margin: 0 auto
position: relative; left:50%;
background: #f2f5ca url('http://i214.photobucket.com/albums/cc286/rare-illusions/layouts/layout11table.jpg') repeat-y;
}
#header{
width:800px;
height:600px;
margin:0 auto;
background: transparent url('http://i214.photobucket.com/albums/cc286/rare-illusions/layouts/layout11copy.jpg');}
div#updates {position: absolute;
top: 0px;
left: 110px;
position: relative;
float:left;
width: 240px;}
div#sidebar {position: absolute;
left: 80px;
top:100px;
position: relative;
float:left;
width: 240px;}

div#main {position: absolute;
top: 400px;
left: 180px;
width: 500px;}
</style>
<center>
<body>
<div id="wrapper">
<div id="header">
<img src="http://i214.photobucket.com/albums/cc286/rare-illusions/layouts/layout11copy.jpg">
<center>
<div id="wrapper">
<div id="updates">
<h5>Updates</h5><p class="updatetext" align="justify">
Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates Updates

</p></div>
<div id="wrapper">
<div id="sidebar" >
Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar
Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar Sidebar



</div>
<div id="main">
Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main
Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main Main

</p>
</div>
</tr></td></body></html>
Thanks for the help, and I'm also have the background repeat enough on the top and bottom
rareillusions is offline
Reply With Quote
View Public Profile
 
Old 11-19-2007, 04:11 AM Re: Centering with absolute positioning
xprmnt's Avatar
Extreme Talker

Posts: 212
Name: GiorgosK
Location: Geoland.org - Greece
Trades: 0
RareIllusions

consider fixing some of the html wrong doings in your code

lots of the tags are misplaced and some are orphaned

consider using something like this as a skeleton for you html pages

Quote:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> title of Document </TITLE>
<META NAME="Generator" CONTENT="">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">

<!-- your styles and linking to other external files goes in here -->

</HEAD>

<BODY>

<!-- your html should go in here -->

</BODY>
</HTML>
- you incorrectly did not include opening <html> tag and <head></head> section

- there are some </td> </tr> all the way down that should not be there

- your <center></center> tags should be inside the <body></body>
and not wrapping around the opening <body>
I would not use <center> actually since its obsolete/deprecated

- you ideally should not use the same id for more than one element
(here you are using it 3 times "wrapper")


and to tell you the truth I am not quite sure of what you are trying to accomplish with the code above ... maybe you should explain to us when you post again

but if you just want to center the content horizontally all you need is VanGogh's suggestion or this in your <style> tags
Quote:
body {
margin: 0px auto;
width: 760px;
}
__________________
GiorgosK
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
xprmnt is offline
Reply With Quote
View Public Profile Visit xprmnt's homepage!
 
Old 11-19-2007, 04:34 AM Re: Centering with absolute positioning
Super Spam Talker

Posts: 755
Name: Barry O' Brien
Location: Ireland
Trades: 0
you can use %'s set it left = 10% width = 80%
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
audiomad is offline
Reply With Quote
View Public Profile Visit audiomad's homepage!
 
Old 11-21-2007, 02:00 PM Re: Centering with absolute positioning
rareillusions's Avatar
Average Talker

Posts: 19
Trades: 0
i want to position my text in the right places while have a background picture repeating with the main text but keeping everything centered
rareillusions is offline
Reply With Quote
View Public Profile
 
Old 11-21-2007, 03:25 PM Re: Centering with absolute positioning
xprmnt's Avatar
Extreme Talker

Posts: 212
Name: GiorgosK
Location: Geoland.org - Greece
Trades: 0
I think you should focus on each one seperatelly instead of trying to accomplish all at once ... you description is still somewhat confusing ...

if you had followed VanGogh's instructions then you would be there already

try now a more complete example
Quote:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title> title of document </title>
<meta name="generator" content="">
<meta name="author" content="">
<meta name="keywords" content="">
<meta name="description" content="">

<style>
div#wrapper {
background: url(images/image.jpg);
width:760px;
margin: 0 auto
}

</style>
</head>

<body>
<div id="wrapper">
your text your text your text your text your text your text
your text your text your text your text your text your text
your text your text your text your text your text your text
your text your text your text your text your text your text
your text your text your text your text your text your text
your text your text your text your text your text your text
your text your text your text your text your text your text


</div>
</body>
</html>
only then come back for more questions

you can also consider learning the basics from w3schools.com HTML/CSS tutorials
before anything else ....
__________________
GiorgosK
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE


Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
xprmnt is offline
Reply With Quote
View Public Profile Visit xprmnt's homepage!
 
Reply     « Reply to Centering with absolute positioning

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.15447 seconds with 12 queries