Centering CSS

Reply

Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Centering CSS

 
0
  #1
Jul 17th, 2007
Suppose there is a very simple web page.

HTML and CSS Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. #logo{
  5. background-color: red;
  6. position: absolute;
  7. top: 0px;
  8. left: 0px;
  9. width: 100px;
  10. height: 100px;
  11. }
  12.  
  13. #banner{
  14. background-color: blue;
  15. position: absolute;
  16. top: 0px;
  17. left: 100px;
  18. width: 500px;
  19. height: 100px;
  20. }
  21.  
  22. #content{
  23. background-color: green;
  24. position: absolute;
  25. top: 100px;
  26. left: 0px;
  27. width: 600px;
  28. height: 400px;
  29. }
  30. </style>
  31. </head>
  32. <body>
  33. <div id="logo"></div>
  34. <div id="banner"></div>
  35. <div id="content"></div>
  36. </body>
  37. </html>

How would i go about centering these acurately considering differing screen configurations?

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Centering CSS

 
0
  #2
Jul 17th, 2007
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Centering CSS

 
0
  #3
Jul 17th, 2007
Thanks for the reply. I tried that previously but couldn't get it to work. The problem was the CSS positioning.

Thanks again.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 52
Reputation: GiorgosK is an unknown quantity at this point 
Solved Threads: 1
GiorgosK's Avatar
GiorgosK GiorgosK is offline Offline
Junior Poster in Training

Re: Centering CSS

 
0
  #4
Jul 17th, 2007
This works, just added the wrapper

change the heights and width and margins according to your needs

  1. <html>
  2. <head>
  3. <style type="text/css">
  4. #wrapper{
  5. width:500px;
  6. height:500px;
  7. position:relative;
  8. margin-left:-250px;
  9. left:50%;
  10. margin-top:-250px;
  11. top:50%;
  12. }
  13. #logo{
  14. background-color: red;
  15. position: absolute;
  16. top: 0px;
  17. left: 0px;
  18. width: 100px;
  19. height: 100px;
  20. }
  21.  
  22. #banner{
  23. background-color: blue;
  24. position: absolute;
  25. top: 0px;
  26. left: 100px;
  27. width: 500px;
  28. height: 100px;
  29. }
  30.  
  31. #content{
  32. background-color: green;
  33. position: absolute;
  34. top: 100px;
  35. left: 0px;
  36. width: 600px;
  37. height: 400px;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div id="wrapper">
  43. <div id="logo"></div>
  44. <div id="banner"></div>
  45. <div id="content"></div>
  46. </div>
  47. </body>
  48. </html>
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Centering CSS

 
0
  #5
Jul 17th, 2007
GiorgosK that works great in IE but it doesn't render properly in Firefox. I'll mess around with it a bit to see if i can get it to work.

This is what i have using the other way.

HTML and CSS Syntax (Toggle Plain Text)
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. body{text-align: center;}
  5.  
  6. #container{
  7. margin: 0 auto;
  8. width: 600px;
  9. height: 600px;
  10. text-align: left;
  11. border-top: 1px solid blue;
  12. border-bottom: 1px solid blue;
  13. border-right: 1px solid blue;
  14. border-left: 1px solid blue;
  15. }
  16.  
  17. #logo{
  18. background-color: red;
  19. top: 100px;
  20. left: 0px;
  21. width: 100px;
  22. height: 100px;
  23. }
  24.  
  25. #banner{
  26. background-color: blue;
  27. top: 0px;
  28. left: 100px;
  29. width: 500px;
  30. height: 100px;
  31. }
  32.  
  33. #content{
  34. background-color: green;
  35. top: 100px;
  36. left: 0px;
  37. width: 600px;
  38. height: 600px;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <div id="container">
  44. <div id="logo"></div>
  45. <div id="banner"></div>
  46. <div id="content"></div>
  47. </div>
  48. </body>
  49. </html>

The problem i'm having is that the container div positions the other divs one on top of the other instead of this...

1122222
1122222
3333333
3333333
3333333


I think it's got something to do with the position attribute but i'm not making sense of them yet. Any suggestions anyone?
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 52
Reputation: GiorgosK is an unknown quantity at this point 
Solved Threads: 1
GiorgosK's Avatar
GiorgosK GiorgosK is offline Offline
Junior Poster in Training

Re: Centering CSS

 
0
  #6
Jul 17th, 2007
I did not check it with firefox actually

put this in the style tag

body, html {
height:100%;
}

it should work with Firefox now
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Centering CSS

 
0
  #7
Jul 17th, 2007
Thanks a lot for your help, it works superbly.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 52
Reputation: GiorgosK is an unknown quantity at this point 
Solved Threads: 1
GiorgosK's Avatar
GiorgosK GiorgosK is offline Offline
Junior Poster in Training

Re: Centering CSS

 
1
  #8
Jul 17th, 2007
No problem Cerberus,

rep is always appreciated if you did find my post valuable.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the HTML and CSS Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC