943,644 Members | Top Members by Rank

Ad:
Jul 17th, 2007
0

Centering CSS

Expand Post »
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.
Similar Threads
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 17th, 2007
0

Re: Centering CSS

Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Jul 17th, 2007
0

Re: Centering CSS

Thanks for the reply. I tried that previously but couldn't get it to work. The problem was the CSS positioning.

Thanks again.
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 17th, 2007
0

Re: Centering CSS

This works, just added the wrapper

change the heights and width and margins according to your needs

html Syntax (Toggle Plain Text)
  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>
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
GiorgosK is offline Offline
52 posts
since Jun 2007
Jul 17th, 2007
0

Re: Centering CSS

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?
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 17th, 2007
0

Re: Centering CSS

I did not check it with firefox actually

put this in the style tag

body, html {
height:100%;
}

it should work with Firefox now
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
GiorgosK is offline Offline
52 posts
since Jun 2007
Jul 17th, 2007
0

Re: Centering CSS

Thanks a lot for your help, it works superbly.
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 17th, 2007
1

Re: Centering CSS

No problem Cerberus,

rep is always appreciated if you did find my post valuable.
Reputation Points: 17
Solved Threads: 1
Junior Poster in Training
GiorgosK is offline Offline
52 posts
since Jun 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in HTML and CSS Forum Timeline: Text is dropping out of place
Next Thread in HTML and CSS Forum Timeline: Don't know where to post this website question...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC