Created
July 25, 2012 01:29
-
-
Save jonathanhefner/3173850 to your computer and use it in GitHub Desktop.
CSS-only technique to keep vertical rhythm with 1-2 line headings
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
<!-- | |
Here is a CSS-only technique to keep vertical rhythm even with | |
2-line headings that have an out-of-sync line-height. Headings | |
with 3 or more lines still break rhythm, but hopefully they are | |
sufficiently uncommon. | |
--> | |
<style type="text/css"> | |
.container { | |
width: 400px; | |
font-size: 16px; | |
/* NOTE subsequent lengths are in px purely for demonstration. | |
* Ideally, a CSS preprocessor would help you put them in em. */ | |
line-height: 24px; | |
} | |
p { | |
margin: 24px 0; | |
} | |
h1 { | |
font-size: 42px; | |
margin: 24px 0; | |
/* a more reasonable line-height for a heading than 1.5em */ | |
line-height: 50px; | |
/* align last line and ::after */ | |
vertical-align: top; | |
/* DEBUG */ | |
/* background: #fdd; */ | |
} | |
h1:first-line { | |
/* align first line and ::before */ | |
vertical-align: bottom; | |
} | |
/**** here's most of the magic ****/ | |
h1:before, h1:after { | |
content: "\feff"; /* zero-width nbsp */ | |
display: inline-block; | |
width: 1px; | |
margin-left: -1px; | |
/* multiple of base line height */ | |
height: 72px; | |
/* DEBUG */ | |
/* background: #ddf; */ | |
} | |
</style> | |
<div class="container"> | |
<h1>Lorem Ipsum</h1> | |
<p> | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do | |
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut | |
enim ad minim veniam, quis nostrud exercitation ullamco laboris | |
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor | |
in reprehenderit in voluptate velit esse cillum dolore eu fugiat | |
nulla pariatur. Excepteur sint occaecat cupidatat non proident, | |
sunt in culpa qui officia deserunt mollit anim id est laborum. | |
</p> | |
<h1>Lorem Ipsum Lorem Ipsum Lorem Ipsum</h1> | |
<p> | |
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do | |
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut | |
enim ad minim veniam, quis nostrud exercitation ullamco laboris | |
nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor | |
in reprehenderit in voluptate velit esse cillum dolore eu fugiat | |
nulla pariatur. Excepteur sint occaecat cupidatat non proident, | |
sunt in culpa qui officia deserunt mollit anim id est laborum. | |
</p> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment