Skip to content

Instantly share code, notes, and snippets.

@benknight
Created January 17, 2013 21:47
Show Gist options
  • Select an option

  • Save benknight/4560101 to your computer and use it in GitHub Desktop.

Select an option

Save benknight/4560101 to your computer and use it in GitHub Desktop.
Demonstating negative margin behavior on absolutely positioned elements.
/*
* 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 */
}
<div id="a">A</div>
<div id="b">B</div>
<div id="c">C</div>
<div id="d">D</div>
// alert('Hello world!');
{"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