We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 33372
    • 1,611 Posts
    I gave up trying to vertically-center content of unknown height without tables a while back. None of the methods that I tried worked reliably back then, and IE7 seems to break even more of them now. So I just made my peace with having one single-cell table in my layout when I wanted to vertically center things (which turns out to be a lot).

    Anyway I’m doing another design now with a slight twist on the problem in that the top half of the screen should be a light grey and the bottom half a dark grey (basically two horizontal stripes with content centered on top of them). My table method doesn’t quite do the trick here, since I need to set two background colors and place the content in the middle of them (and if I use three rows/cells, IE7 refuses to fix the height of the middle one and just stretch the other two). Seems like a layout that calls for absolute positioning (or for me to give up on the idea of centering it vertically).

    So this prompted me to revisit vertical centering in general and to look for some other method. Preferably this would NOT use a table, and it would need to work in IE6 and 7, Firefox, and Safari, be standards-compliant, and relatively free of CSS hacks that I’ll need to change later when some new browser comes out that handles them differently. Anyone have a suggestion?
      "Things are not what they appear to be; nor are they otherwise." - Buddha

      "Well, gee, Buddha - that wasn't very helpful..." - ZAP

      Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
      • 26435
      • 1,193 Posts
      I know what you mean.
      I did a site that ended up having a lot of tweaks, then a lot of do-overs, then finally the ultra-simple

      container
      head
      content
      sidebar
      footer
      /container

      which displayed evenly on all browsers, but looking at it in IE 7 the footer ends up getting clipped and only displays (very awkwardly) on the bottom of the sidebar. So an image that is supposed to be 780px wide gets jammed up in a 200px wide div.
      What the hell? All the improvements users were promised seem to be thrown out the window in favor of the latest ActiveX technological breakthrough. whatever.

      Now I have to add specific code ( read as extra overhead) to my template to account for IE 6 and IE7. and they are not interchangeable. Did anything even get fixed? I still see gray outlines around my transparent PNGs.

      -sD-
      Dr. Scotty Delicious, Scientist.
        Husband, Father, Brother, Son, Programmer, Atheist, Nurse, Friend, Lover, Fighter.
        All of the above... in no specific order.


        I send pointless little messages
        • 33372
        • 1,611 Posts
        I had to scramble and fix about ten designs when IE7 finally came out. I had tested them using the beta versions before, but somehow I convinced myself that those things wouldn’t be a problem in the final version. In fact I found even more issues once I started to really test with IE7...

        But actually overall I’m pretty happy with IE7. It’s 6 that was the problem. I find it to be far more standards-compliant than any previous version of IE, and from a user’s perspective the interface is simple and effective (better than Firefox, in my opinion).

        A partial list of CSS changes in IE7 can be found here: http://blogs.msdn.com/ie/archive/2006/08/22/712830.aspx

        I’ve had lots of designs break in IE7 because it absolutely will not stretch a div with set height and/or width to fit contents that are larger (which I can respect, although it’s not the way things used to work). Of course it doesn’t treat table cells the same way, which is why my vertical centering hack won’t work with three cells in IE7 (go figure). And I’ve had some minor z-index glitches crop up.

        The PNG transparency issue is definitely fixed, though. You still need to add the AlphaImageLoader filter for background PNGs with transparency in IE6, but IE7 handles this natively. I actually PHP-script my stylesheet to only add that filter for IE6, and someday soon I hope to remove it entirely (what a pathetic hack that was!).

        Oh, and they finally fixed that wacky <base href... /> issue that made it so you couldn’t select text to copy it in IE6...
          "Things are not what they appear to be; nor are they otherwise." - Buddha

          "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

          Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
          • 25663 MODX Staff
          • 12,272 Posts
          CSS vertical centering of arbitrary height is in fact doable ... and it works on all recent browsers (IE6/7, Saf, Opera, Fx, etc). laugh

          <div id="vcenter">
              <div class="content">
                      <div class="wrapper"> <!-- this .wrapper is only needed by IE -->
                          <p>Get your piping hot fresh vertically centered content here... can be any arbitrary height, too!</p>
                      </div>
              </div>
          </div>


          Your main CSS gets this:
          #vcenter {
              display: table; /* standards browsers only */
              height: 400px; /* whatever height you need for the containing wrapper */
              overflow: hidden;
          }
          #vcenter .content {
              display: table-cell;  /* standards browsers only */
              vertical-align: middle;
          }


          Insert a Conditional Comments block in your header to address IE’s little oddities:
          <!--[if IE]>
          <style type="text/css">
          #vcenter {
              position: relative;
          }
          #vcenter .content {
              position: absolute; 
              top: 50%;
          }
          #vcenter .wrapper {
              position: relative;
              top: -50%;
          }
          <style>
          <![endif]-->
          



          Alternately if you don’t mind hacks in your CSS, you can roll it all into one (IE-only sees the rules preceded by a #, sorta like the old underscore hack which IE7 doesn’t see, but IE6 does):
          #vcenter {
              display: table; /* standards browsers only */
              height: 400px; /* whatever height you need for the containing wrapper */
              #position: relative; /* IE only */
              overflow: hidden;
          }
          #vcenter .content {
              #position: absolute; 
              #top: 50%;
              display: table-cell;  /* standards browsers only */
              vertical-align: middle;
          }
          #vcenter .wrapper {
              #position: relative;
              #top: -50%;
          }


          See it in action: http://loudthought.biz/advertising.html
            Ryan Thrash, MODX Co-Founder
            Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
            • 33372
            • 1,611 Posts
            YES! That does in fact seem to work perfectly! Now I can ditch those impure tables that were befouling my code...
              "Things are not what they appear to be; nor are they otherwise." - Buddha

              "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

              Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
              • 26435
              • 1,193 Posts
              Hey Ryan, I also discovered that hack quite by accident.
              I was testing some CSS layouts for my wifes blog, using // to comment out lines that I didn’t want to erase, but I wanted to see what it would look like without. Well, I forgot to remove the// in front of a background color tag and low and behold, a white background showed up in IE when I was expecting the body color to shine through. I never thought about using it to add IE only tags, but that is brilliant. Does it validate?
                Husband, Father, Brother, Son, Programmer, Atheist, Nurse, Friend, Lover, Fighter.
                All of the above... in no specific order.


                I send pointless little messages
                • 25663 MODX Staff
                • 12,272 Posts
                Definitely chokes the validator. smiley
                  Ryan Thrash, MODX Co-Founder
                  Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
                  • 33372
                  • 1,611 Posts
                  Well, yes. It seems to break the validation script (parse errors). So technically that’s not the same as it being declared invalid... wink
                    "Things are not what they appear to be; nor are they otherwise." - Buddha

                    "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                    Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                    • 33372
                    • 1,611 Posts
                    You could also load the IE-only code as a separate dynamic CSS file (e.g., styles_css.php) and have it only add that code when the browser is IE:
                    <?php
                    
                    header("Content-type: text/css");
                    
                    $ua=strtolower($_SERVER['HTTP_USER_AGENT']);
                    if ( (strstr($ua,"win")) && (strstr($ua,"msie")) && !(strstr($ua,"opera")) ){
                    
                    ?>
                    #vcenter {
                        position: relative;
                    }
                    #vcenter .content {
                        position: absolute; 
                        top: 50%;
                    }
                    #vcenter .wrapper {
                        position: relative;
                        top: -50%;
                    }
                    <?php
                    }
                    ?>


                    or as a snippet in the head of a template:

                    $output='';
                    $ua=strtolower($_SERVER['HTTP_USER_AGENT']);
                    if ( (strstr($ua,"win")) && (strstr($ua,"msie")) && !(strstr($ua,"opera")) ){
                    
                    $output.="
                    <style type=\"text/css\">
                    #vcenter {
                        position: relative;
                    }
                    #vcenter .content {
                        position: absolute; 
                        top: 50%;
                    }
                    #vcenter .wrapper {
                        position: relative;
                        top: -50%;
                    }
                    </style>
                    ";
                    
                    return $output;
                    


                    That shouldn’t break W3C validation at all...
                      "Things are not what they appear to be; nor are they otherwise." - Buddha

                      "Well, gee, Buddha - that wasn&#39;t very helpful..." - ZAP

                      Useful MODx links: documentation | wiki | forum guidelines | bugs & requests | info you should include with your post | commercial support options
                      • 25663 MODX Staff
                      • 12,272 Posts
                      The Conditional Comment method in the document header would validate since it’s hack-free.
                        Ryan Thrash, MODX Co-Founder
                        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me