Created
October 9, 2012 07:12
-
-
Save anthonyringoet/3857126 to your computer and use it in GitHub Desktop.
Center with calc(), tip from Lea Verou presentation
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 with calc(), tip from Lea Verou presentation | |
*/ | |
body{ | |
/* housekeeping */ | |
background: #ffa; | |
min-height: 100%; | |
font-family:sans-serif; | |
} | |
section{ | |
background:#0f0; | |
width:60%; | |
margin:0 calc(50% - 30%); | |
} | |
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
<!-- tip from http://lea.verou.me/more-css-secrets --> | |
<section> | |
<p>Centered inside</p> | |
</section> |
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
{"view":"split","fontsize":"100","seethrough":"","prefixfree":"1","page":"all"} |
I’ve just stumbled across this looking for Lea’s talk. calc() only makes sense when you have mixed value types like % and px. As you are only using percentages in your example you don’t need calc. Just do the math:
margin:0 calc(50% - 30%);
=> margin:0 20%;
Here's Lea's slides: http://lea.verou.me/more-css-secrets/#padding-auto
This is what she really does:
section {
width: 700px;
padding: 0 calc(50% - 350px);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great tip.
I also add
margin: 0 auto
as a fallback ifcalc
is not supported. The background does not spread on the whole page, but at least the content is centered :)