Log in   Register a New Account

Accessify Forum - Discuss Website Accessibility

New to the forum?

Only an email address is required.

Register Here

Already registered? Log In

Vertical centering in CSS

Reply with quote Anyone one know any decent ways to center an element (a div, say) vertically in the page.

I know countless ugly, browser specific hacks. I was just wondering if there was a nice and proper css way to do this?

I'm talking about centreing in the veiwport really. e.g. like many lightbox implimentaions do. But with css not js...

All suggestions greatfull received Smile

I've been trying to play with % units for height, but that simply seems to not do anything Sad

creator of Talklets
Talklets ,
Reply with quote If you know the height of an element precisely in px or approximately in em, you can use position: absolute for this. Not tested:
Code:
#foo {
 top: 50%;
 margin-top: -5em; /* height is about 10em */
 position: absolute;
}
If you don't want it to move when the user scrolls, swap absolute for fixed.
Reply with quote I think you may need a
Code:
display: table;
in there because I have never done it without a real table. This is something I have been meaning to crack for years.

Johan De Silva / Portfolio
Reply with quote Cheers

The
Code:
display:table;
is a new concept to me Embarassed

I'm not a css guy realy, and had only ever use 'none', 'block' or 'inline' as values.

Will definately play with... Smile

creator of Talklets
Talklets ,
Reply with quote I think it is a popper CSS2 standard behavior however not implemented in anything less than IE8 so works in standard compliant browsers like IE8/FF3 etc... If you crack it with this you may need to conditional statement a new CSS file for IE6 and IE7 giving the top margin a fixed height. If you do it you will be one of the first to do it hack free without a table. However it is regretful that the majority of clients I am seeing still use IE6 Crying or Very sad

Johan De Silva / Portfolio
Reply with quote Have you tried the 'negative margin kludge' which Cerb mentioned?
You may also need to specify a height and/or min-height of 100% on the html, body selectors in order for the viewport to expand appropriately, but it should work - which is to say that I've used it numerous times before and know it to work in IE6/7 and all varieties of modern browsers.

See also: css dead center
Reply with quote Wonderful, thankyou, all.

Got enough new stuff to play with to keep me busy.

Cheers.

Will report back once finished playing.

creator of Talklets
Talklets ,
Reply with quote
Johan007 wrote:
...... However it is regretful that the majority of clients I am seeing still use IE6 Crying or Very sad


About 8% IE6 (or IE6 compatible) on my company site. I am a bit surprised you have so many IE6 users. W3Schools stats suggest you are a bit unusual. What is your target audience?
Reply with quote June 2008 for Project Cerbera, according to AWstats:
Code:
IE      7.0  507,679  36.8%
IE      6.0  403,898  29.3%
Firefox 2.*  286,334  22.6%
Firefox 3.0   34,993   2.5%
Opera   *.*   33,830   2.4%
Safari  *.*   25,162   1.8%
IE      8.0    1,123   0.0%
Anyone looking forwards to supporting 3 Microsoft browsers, 2 Mozilla browsers, Opera 9.5 and the most recent/prevalent Safari?
Reply with quote According to StatCounter for a small site that I look after:

Perc. Browser Name Version
38% MSIE 7.0
31% MSIE 6.0
20% Firefox 2.0.0
4% Firefox 3.0
3% Safari 1.2
2% Mozilla 5.0
1% MSIE 5.0
1% Opera 9.50

.
Reply with quote
Cerbera wrote:
.........Anyone looking forwards to supporting 3 Microsoft browsers, 2 Mozilla browsers, Opera 9.5 and the most recent/prevalent Safari?


Must admit I have assumed that Firefox users will move pretty quickly across to FF3 - which is the only version of Ff I have on my computers.

For IE I presently have 5.01, 5.5, 6 and 7. Guess I'll ditch 5.x when I load IE8.
Reply with quote
kiwibrit wrote:
What is your target audience?
People who own websites. I work for a design and development agency and they always pick up on it. Luckyl the other designer runs IE6 so I check it on his. If you design for FF and IE6 you rarely get problems with IE7, but if you design in IE7 you often get margin problems and have to use padding in the next div up.

Johan De Silva / Portfolio
Reply with quote Ah, I see, they check with IE6. FWIW, I use conditional statements to bring in styling sheets for IE6 and IE7 - and until recently checked that nothing major was broken in IE5.5. I run checks using them, as well as Opera, FF, and Safari.
Reply with quote
Bill Posters wrote:
See also: css dead center
Yes I am very impressed they have done it with only 2 DIV tags (possibly one if I can try and style the <body> but seems to fail in IE so far). However you must maintain a fixed height for it to work but I suppose it is not a problem because most dead centre sites have a fixed height to stop the site jumping up and down when navigating page to page Confused

EDIT: This is now fixed when Quirks mode is disabled reducing the number of DIVs to 1

kiwibrit wrote:
I use conditional statements to bring in styling sheets for IE6 and IE7
I used too as well but I strongly do not recommend it as it then becomes more style sheets and CSS values to manage and your CSS looks messy. Especially when the IE6 fix's are easy. All you have to do is simply use padding in the containing layer instead of margin values where it brakes, some times just use overflow:hidden. Once you get the hang of it the CSS becomes easy to write. The only time I use conditional statements these days is for alpha transparency alternative GIF on links or Alpha transparency to PNG fix JavaScript file.

Johan De Silva / Portfolio
Reply with quote
Johan007 wrote:
EDIT: This is now fixed when Quirks mode is disabled reducing the number of DIVs to 1

Why wouldn't you be using standards-compliant mode in the first place?
I've been using nowt but Strict doctypes for years. Seems like common sense to me. Certainly makes my job easier by levelling the playing field as much as possible from the get-go.


Besides that, can't say I've ever had too much trouble with…

Code:
html, body {
   height: 100%;
   padding: 0;
   margin: 0;
}

#wrap {
   position: absolute;
   left: 50%;
   top: 50%;
   width: 700px;
   height: 400px;
   margin-left: -350px; /* minus half width */
   margin-top: -200px; /* minus half height */
   background: #ccc
}


Been a while since I used centering, but just did a quick check in FF, Saf, IE6, IE7 and it centers just fine.

Display posts from previous:   

Page 1 of 2

Goto page 1, 2  Next

All times are GMT

  • Reply to topic
  • Post new topic