开发者

Vertical Centering, Unknown Height, Other Content on Page

开发者 https://www.devze.com 2023-01-24 16:48 出处:网络
Alright, so this is basically the usual \"how to vertically center with CSS\" question, but with some catches.

Alright, so this is basically the usual "how to vertically center with CSS" question, but with some catches.

  1. No Javascript. HTML and CSS only. CSS3 is fine as long as it's reasonably well supported by today's browsers.

  2. The element's content, and therefore height, is not known. It may be anywhere from a few dozen to a few hundred pixels. In the future I might even have a script adding and removing elements inside, so it may change height as the user is interacting with it.

  3. There is other content on the page - a navigation bar at the left and a menu at the top. These need to be accessible.

The approach I've used is the three-container-div method using display: table-cell, as documented at: http://www.jakpsatweb.cz/css/css-vertical-center-solution.html this solves issues 1 and 2, but not 3.

http://imgh.us/vcenter.jpg shows the design and the problem. The yellow box is the innermost container. The red and green dotted boxes around the entire page (which have become somewhat blurred together due to JPEG encoding) are the outermost and middle containers respectively. (The out-of-place footer is a separate issue...)

The problem with this layout is the outer containers cover the entire page, and this makes it impossible to click on the navbar, because it's now "under" those containers. Z-index can move them to the bottom, but then it becomes impossible to click on anything inside the red box, because it's now "under" the page's main content box. (XHTML only allows a single element inside the <body>, so I've just wrapped it all in a <div>.) Even if the outer two containers have z-index: -100 and the inner container has z-index: 200, it still ends up under the main content box for some reason. (I did try various position attributes.)

The only solution I've seen is a new CSS3 property, pointer-events, that would in theory allow me to make events pass through the transparent containers as I'd expect; however this seems to be quite new and not yet supported by most browsers outside of SVG, and I imagine I'd have the same trouble as with Z-index.

I do want the element at the center of the page, not the center of the content area (i.e. ignoring navbars in the calculation of position), so placing the container inside the content area isn't an ideal solution. (I'm using this style on the login page as well, which has no navbars, and it'd look a bit strange if the "centered" elements were centered relative to a navbar that isn't always visible.)

In summary what I need is to center, without using Javascript, an element of unknown h开发者_StackOverflow中文版eight on a page with other content at its edges, without covering any of the content with an invisible layer (and thus making it unclickable).


While this is obviously an old question and the OP has undoubtedly solved this problem, I figured I'd add a link to Chris Coyier's marvelous write-up on how to deal with this issue for future wayfarers in need of a similar solution.

http://css-tricks.com/centering-in-the-unknown/

0

精彩评论

暂无评论...
验证码 换一张
取 消