7 ms·
Just in case anyone's unaware... it's entirely possible to vertically center elements of variable height too in pure CSS... it's just "unsemantic", because it r
by crazygringo 13y ago
Just in case anyone's unaware... it's entirely possible to vertically center elements of variable height too in pure CSS... it's just "unsemantic", because it relies on the fact that only table cells have "vertical-align:middle".
But that's how I center every one of my "modal dialogs" on a site -- the whole modal dialog is inside a <table> with position:fixed and top/bottom/left/right:0. Then, just give the <td> a "vertical-align:middle", and then put your desired visible dialog <div> inside it.
It's annoying to have to wrap something inside of a <table><tr><td> just to vertically center something, but it sure beats using JavaScript to do it. (You might be able to do it just with the display:table-cell property too, but it's been a long time since I checked browser compatibility.)
- bhauer 13y agoAt least in every browser I've tested, display:table-cell works, as long as that is inside another element with display:table. This allows one to retain pedantic purism of all divs.
- syncerr 13y agoIt's not as big a deal anymore, but table-cell is not supported in IE6-7. http://caniuse.com/css-table http://caniuse.com/css-table
- gcb0 13y ago>not a big deal Browser still has more users than Firefox had some years ago when we started talking about supporting all browsers. Unfortunately.
- w1ntermute 13y agoIt's funny how so many web devs just ignore older browsers out of a sense of superiority towards users of those browsers. I've even heard some of these guys say "they're not my target audience, so why should I care?" They are, of course, inevitably, Apple fanbois. Fortunately, they are not usually in charge of making business decisions at any significant companies.
- saraid216 13y agoIs it considered ignoring them if I go down to the shooting range, put the IE6 logo on the target, and let loose on a weekly basis?
- alexfringes 13y agoI just woke up from a nap in 2009. I hope. Gonna go invent chatroulette now.
- kaeawc 13y agoIts funny how many people spend so much time supporting old browsers out of a sense of superiority towards web devs who ignore them. I've even heard some of these guys call us "Apple fanbois".
- PavlovsCat 13y agoI'm ignoring older browser because it's the difference between a lot of pain and a modicum of joy. (Also, most of the actually cool stuff these days? "Sorry, must be this modern to ride") Also, if you keep supporting older browser, people will keep using them. If they keep using them, they will likely have vulnerabilities. So in a sense, dropping support for old browsers is like tough love.. it's not an easy choice for anyone to lose out on however small a fraction of visitors/users, but sometimes you just have to do it, "for the greater good".
- usaphp 13y agoAccording to stats, only 0.8% of users are using IE6-7. Is there any logical reason to spend quite a lot of time to adjust your css to work properly for 0.8% of your users? And if you are building a premium product - I don't really think this 0.8% are the one who are going to purchase it...
- city41 13y agoYou should look at your own stats to decide that. IE7 accounts for 4% of our users, so we do make some concessions for them.
- alternize 13y agoaround 2% here... but by far some of the best paying ones (corporates).
- emn13 13y agowow, that's just.... painful. Why the heck would a company stick to IE7? It's not like the compatibility modes of later IE's don't work or something...
- daledavies 13y agoInability to work out how to deploy later versions of IE and push out compatibility view lists using group policy (I'm no windows networking expert but this has to be possible right?)
- alternize 13y agoin this particular case, the company is global and has a fixed load set for their computers/notebooks. the last version was compiled together around 5 years ago. when you have to support multiple thousands employees, having a stable environment is key. upgrading becomes an expensive and slow process. the next load set at that company is currently in testing for the last 9 months (!) and will upgrade to ie9 and office 2010 - both will be around for a few years to come, their extended support periods end in 2020.
- chriscoyier 13y agousing translate() you can center variable width/height stuff too (without the table stuff): http://css-tricks.com/centering-percentage-widthheight-elements/ http://css-tricks.com/centering-percentage-widthheight-eleme...
- crazygringo 13y agoWow, that's awesome. As soon as I get to drop IE8 support, I'll think about using that. I had no idea you could use % within transforms that referred to the size of the element itself (since all other % in CSS refer to the size of a parent element). Thanks!
- arxpoetica 13y ago...like you said...all that cruft...
- dntrkv 13y agodisplay: table-cell is my preferred method and it's supported by IE8 and up.
- rimantas 13y ago> It's annoying to have to wrap something inside of a > <table><tr><td> just to vertically center something And you dont have to. Because your container does not have to be a table, and you don't need cells and rows. Just use display: (table|table-cell). See for an example: http://css-tricks.com/centering-in-the-unknown/ http://css-tricks.com/centering-in-the-unknown/ It is not OK to use table and friends in you HTML just for styling purposes. It's totally OK to use display: table* in your CSS, because CSS has no semantics, its purpose is styling.
- deleted 13y ago[deleted]
- binarymax 13y agoThanks for that link, it is very enlightening. So much of CSS just feels like a total hack. putting something in the middle of an element should not be so hard! negative margins? ...absurd! Why doesnt CSS just have a property like 'placement: center middle' or something.
- lbenes 13y agoCSS is the epitome of design by committee, only worse. It’s worse because some members had a vested interest for the standard to fail. As a result, we get threads like this from designers, and programmers; they're in a worse bind. It’s just not possible to efficiently hardware accelerate CSS. Of JS/DOM/CSS in HTML5, CSS is the worst standard. Depreciation is the best we can hope for. I’m sure MS and Mozilla will fight it tooth and nail, but maybe Google will pull a Dart/Go here and give us something decent to work with.
- emn13 13y agoI've got exactly the opposite opinion: of JS/HTML/CSS, CSS is y far the best standard. People complain because it doesn't address their specific needs explicitly (e.g. vertical centering) - but that way lies madness; a huge spec that caters for every case explictly, and probably has lots of corner cases and interactions meaning that cross-browser support would be even harder than now. CSS is limited - and that's a good thing. Would you want to implement complex layout in JS, say? And how that would look when window size changes, or the page is zoomed, or some font isn't available? The declarative nature of CSS is what makes it work; and the key aspect of declarative here is that it represents a state, not an algorithm - it's not turing complete. You'll never be able to do everything in CSS; and that's by design.
- 101percent 13y agoHow'd this idiotic comment get to the top? Tables? Really? Have you coded anything in the last 5 years? So many noobs in this thread, you have to use this method: http://stackoverflow.com/a/10568273 http://stackoverflow.com/a/10568273
- yetanotherjosh 13y agoI'm surprised nobody has mentioned the display:inline-block approach to vertical centering. It's a bit awkward for a couple reasons but it is responsive to dynamic content (no fixed dimensions are required), and it does not require tables. It does require a non-semantic (albeit empty) "shim" div. I'm not sure how well it fares in Ye Olde IE 6.0 but hopefully this is not a concern to most webdevs today. Here's a jsfiddle to illustrate it. http://jsfiddle.net/qu5jq/ http://jsfiddle.net/qu5jq/