Created
January 17, 2013 21:47
-
-
Save benknight/4560101 to your computer and use it in GitHub Desktop.
Demonstating negative margin behavior on absolutely positioned elements.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * Demonstating negative margin behavior on absolutely positioned elements. | |
| * | |
| * Conclusion: Browser only changes position when the margin is set on the same side as the position offset. | |
| * E.g. style rule has margin-top and top set, or margin-bottom and bottom. See A & C. | |
| */ | |
| div { | |
| position: absolute; | |
| width: 100px; | |
| height: 100px; | |
| line-height: 100px; | |
| text-align: center; | |
| background: #eee; | |
| } | |
| #a { | |
| top: 100px; | |
| left: 0; | |
| margin-top: -100px; /* Brings it up 100px */ | |
| } | |
| #b { | |
| top: 100px; | |
| left: 100px; | |
| margin-bottom: -100px; /* No change in position */ | |
| } | |
| #c { | |
| bottom: 100px; | |
| left: 200px; | |
| margin-bottom: -100px; /* Brings it down 100px */ | |
| } | |
| #d { | |
| bottom: 100px; | |
| left: 300px; | |
| margin-top: -100px; /* No change in position */ | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <div id="a">A</div> | |
| <div id="b">B</div> | |
| <div id="c">C</div> | |
| <div id="d">D</div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // alert('Hello world!'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"all"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment