Forum Moderators: not2easy

Message Too Old, No Replies

Vertical Align Problem

Center Div Tag on Page with Body Background Visible Problem

         

Sherif

9:59 pm on Jun 22, 2010 (gmt 0)

10+ Year Member



Hey Guys....


I've been trying to find a solution for a problem i'm facing, but i can't seem to find a clear solution....

I am currently working on a website, and i am trying to create a layout similar to bing[dot]com where i will have a div tag that is centered in the browser vertically and horizontally.. centering horizontally is very easy using the auto left and right margin.. the problems begin to appear when i am trying to center the div tag vertically.

Here is the code that I was able to compile and reached a midway solution:

<style type="text/css">
body {
margin: 0px;
padding: 0px;
min-width:900px;
}
#pagebackground {
position: absolute;
height: 100%;
width: 100%;
background-image: url(assets/gradient.jpg);
background-repeat: repeat-x;
background-position: 0px 0px;
}

#container {
margin-right: auto;
margin-left: auto;
position: absolute;
top: 50%;
width: 100%;
margin-top:-300px;
min-width: 900px;
}
#picture {
position: relative;
width: 900px;
margin-top: 0px;
margin-right: auto;
margin-bottom: 0px;
margin-left: auto;
}

</style></head>

<body>
<div id="pagebackground"></div>
<div id="container"><div id="picture"><img src="assets/logo.jpg" width="900" height="600" alt="Company Logo" /></div>




</div>

</div>
</div>
</body>
}



There are 3 problems i'm facing now....

1) When i am scaling the browser, the image gets cropped from the top especially when i reduce the browser height to the minimum, and i guess it has something to do with the -ve margin. but the problem is that with out it, I don't get the vertical center effect.

2) the image doesn't stop at the end of the pagebackground div tag... the code above has a 100% height, but when i make it a fixed height, the picture div tag flows over it [especially when i'm trying to test the concept]..

3) i'm trying to create a gradient background covering the whole page behind the centered div tag, but since the height is unknown, then its really hard to make it appear.. there is a gradient image that is supposed to repeat along the x-axis. [in the current situation, the gradient seems to be working, but i have a feeling that this is structurally incorrect.]

I'm kind of confused... so can you please guide me on how to do the layout similar to bing[dot]com as mentioned above with the top fixed navigation bar, and the centered div tag... it seems that i am not sure how to structure my layout, and i can't clearly grasp the concept of vertical alignment.



Your help and support will be really appreciated.

Sincerely,
Sherif

mihai2u

12:45 pm on Jun 23, 2010 (gmt 0)

10+ Year Member



Hi,

CSS-only solutions for the problem you are trying to fix are quite complex to achieve cross-browser functional layouts.

The easiest solution is to wrap everything within a table, and use vertical-align:middle on the td.

If you still want to implement a table-less pure-css solution google for "css vertical centering". There are plenty of recopies available.
I have personally implemented Yuhu's solution with success a few times in the past.

mihai2u

12:46 pm on Jun 23, 2010 (gmt 0)

10+ Year Member



I ment "recipies" not recopies :P

mihai2u

12:51 pm on Jun 23, 2010 (gmt 0)

10+ Year Member



Regarding #3

I would suggest creating a much taller gradient which you will position: left center and repeat-x and applying it on the body element.

Also apply this styling as well:
html, body {height:100%;}

Let me know how this evolves.

Sherif

4:33 pm on Jun 24, 2010 (gmt 0)

10+ Year Member



Thank mihai2u,

- Regarding #3... everything is now working well in terms of the gradient and its alignment. Thanks for the advice... it seems that adding the "html height 100%" with body tag solved the problem.


- Regarding post 1 and 2, is there any advice on how to go about solving these problems
(adding a fixed top bar, and stopping the picture from cropping when the window gets extra small?)


Your help will be much appreciated.



Sincerely,
Sherif Malek

mihai2u

6:38 pm on Jun 26, 2010 (gmt 0)

10+ Year Member



#3 - Great, I'm glad you got it fixed.

@#1,2

These are the easy steps I would take to get it covered easily.

body
- topbar (absolute positioned, top:0)
- table (height:100%)
-- td (vertical-align:middle;}
--- div.spacer {height:Xpx; - the height of the fixed topbar} -> to avoid the topbar from overlapping your items.
--- your item(s) that need to be vertically centered

Hope you'll be following them easily.