position-relative-nested-001

Vertical centering with negative and positive top positioning

WeasyPrint

This browser

Assertion
Equal 50% positive and negative positioning can vertically center objects.

Source

 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 2 <html>
 3     <head>
 4         <title>CSS Test: Vertical centering with negative and positive top positioning</title>
 5         <link rel="author" title="Microsoft" href="http://www.microsoft.com/">
 6         <link rel="help" href="http://www.w3.org/TR/CSS21/visuren.html#positioning-scheme">
 7         <meta name="flags" content="">
 8         <meta name="assert" content="Equal 50% positive and negative positioning can vertically center objects.">
 9         <style type="text/css">
10             #outer
11             {
12                 background: green repeat-x center url('support/red_box.png'); /* red_box.png is a 100px by 100px red square */
13                 height: 300px;
14                 width: 200px;
15             }
16             #inner
17             {
18                 background-color: red;
19                 height: 100px;
20                 position: relative;
21                 top: 50%
22             }
23             #inner-most
24             {
25                 background-color: green;
26                 height: 150px; /* 150px == top-half of red_box.png's height + #inner's height */
27                 position: relative;
28                 top: -50%; /* 50% is 50% of the height of #inner-most's containing block which is #inner */
29                 /* to move the top of the #inner-most box from pixel 150 on the y-axis of #outer to pixel 100 on the y-axis of #outer */
30             }
31         </style>
32     </head>
33     <body>
34         <p>Test passes if there is no red visible on the page.</p>
35         <div id="outer">
36             <div id="inner">
37                 <div id="inner-most"></div>
38             </div>
39         </div>
40     </body>
41 </html>