Created
September 7, 2013 12:54
-
-
Save 0xBADC0FFEE/6475341 to your computer and use it in GitHub Desktop.
From http://coding.smashingmagazine.com/2013/08/09/absolute-horizontal-vertical-centering-css/ Absolute horizontal and vertical centering in css
This file contains 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
.Absolute-Center { | |
margin: auto; | |
position: absolute; | |
top: 0; left: 0; bottom: 0; right: 0; | |
} |
This file contains 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
.Center-Container { | |
position: relative; | |
} | |
.Absolute-Center { | |
width: 50%; | |
height: 50%; | |
overflow: auto; | |
margin: auto; | |
position: absolute; | |
top: 0; left: 0; bottom: 0; right: 0; | |
} |
This file contains 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
.Absolute-Center.is-Fixed { | |
position: fixed; | |
z-index: 999; | |
} |
This file contains 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
.Absolute-Center.is-Right { | |
left: auto; right: 20px; | |
text-align: right; | |
} | |
.Absolute-Center.is-Left { | |
right: auto; left: 20px; | |
text-align: left; | |
} |
This file contains 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
.Absolute-Center.is-Responsive { | |
width: 60%; | |
height: 60%; | |
min-width: 200px; | |
max-width: 400px; | |
padding: 40px; | |
} |
This file contains 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
.Absolute-Center.is-Overflow { | |
overflow: auto; | |
} |
This file contains 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
.Absolute-Center.is-Resizable { | |
min-width: 20%; | |
max-width: 80%; | |
min-height: 20%; | |
max-height: 80%; | |
resize: both; | |
overflow: auto; | |
} |
This file contains 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
/* | |
<img src="http://placekitten.com/g/500/200" class="Absolute-Center is-Image" alt="" /> | |
*/ | |
.Absolute-Center.is-Image { | |
height: auto; | |
} | |
.Absolute-Center.is-Image img { | |
width: 100%; | |
height: auto; | |
} |
This file contains 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
/* | |
Modernizr Test for Variable Height Content | |
Modernizr.testStyles('#modernizr { display: table; height: 50px; width: 50px; margin: auto; position: absolute; top: 0; left: 0; bottom: 0; right: 0; }', function(elem, rule) { | |
Modernizr.addTest('absolutecentercontent', Math.round(window.innerHeight / 2 - 25) === elem.offsetTop); | |
}); | |
*/ | |
.absolutecentercontent .Absolute-Center.is-Variable { | |
display: table; | |
height: auto; | |
} |
This file contains 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
.is-Negative { | |
width: 300px; | |
height: 200px; | |
padding: 20px; | |
position: absolute; | |
top: 50%; left: 50%; | |
margin-left: -170px; /* (width + padding)/2 */ | |
margin-top: -120px; /* (height + padding)/2 */ | |
} |
This file contains 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
.is-Transformed { | |
width: 50%; | |
margin: auto; | |
position: absolute; | |
top: 50%; left: 50%; | |
-webkit-transform: translate(-50%,-50%); | |
-ms-transform: translate(-50%,-50%); | |
transform: translate(-50%,-50%); | |
} |
This file contains 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 class="Center-Container is-Table"> | |
<div class="Table-Cell"> | |
<div class="Center-Block"> | |
<!-- CONTENT --> | |
</div> | |
</div> | |
</div> | |
*/ | |
.Center-Container.is-Table { display: table; } | |
.is-Table .Table-Cell { | |
display: table-cell; | |
vertical-align: middle; | |
} | |
.is-Table .Center-Block { | |
width: 50%; | |
margin: 0 auto; | |
} |
This file contains 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 class="Center-Container is-Inline"> | |
<div class="Center-Block"> | |
<!-- CONTENT --> | |
</div> | |
</div> | |
*/ | |
.Center-Container.is-Inline { | |
text-align: center; | |
overflow: auto; | |
} | |
.Center-Container.is-Inline:after, | |
.is-Inline .Center-Block { | |
display: inline-block; | |
vertical-align: middle; | |
} | |
.Center-Container.is-Inline:after { | |
content: ''; | |
height: 100%; | |
margin-left: -0.25em; /* To offset spacing. May vary by font */ | |
} | |
.is-Inline .Center-Block { | |
max-width: 99%; /* Prevents issues with long content causes the content block to be pushed to the top */ | |
/* max-width: calc(100% - 0.25em) /* Only for IE9+ */ | |
} |
This file contains 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
.Center-Container.is-Flexbox { | |
display: -webkit-box; | |
display: -moz-box; | |
display: -ms-flexbox; | |
display: -webkit-flex; | |
display: flex; | |
-webkit-box-align: center; | |
-moz-box-align: center; | |
-ms-flex-align: center; | |
-webkit-align-items: center; | |
align-items: center; | |
-webkit-box-pack: center; | |
-moz-box-pack: center; | |
-ms-flex-pack: center; | |
-webkit-justify-content: center; | |
justify-content: center; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment