Skip to content

Instantly share code, notes, and snippets.

@maxnordlund
Last active March 30, 2017 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxnordlund/7c99114d15d7d067c3ebe71656c68b1f to your computer and use it in GitHub Desktop.
Save maxnordlund/7c99114d15d7d067c3ebe71656c68b1f to your computer and use it in GitHub Desktop.
Pure CSS for `hr` element with caption
$size: 1rem;
$background: white;
hr[title] {
line-height: 0;
&::after {
background: $background;
content: attr(title);
// Leaving width on auto allows it to be adjusted according to the content
// but we need to specify the height for the positioning to work
height: $size;
line-height: $size;
// Give it a bit of breathing room
padding: 0 $size;
// Horizontally center it
display: table;
margin: 0 auto;
// Vertically center it above the ruler
position: relative;
top: -($size / 2);
}
}
@maxnordlund
Copy link
Author

maxnordlund commented Mar 30, 2017

Just specify the caption text with the title attribute, like so:

<hr title="OR" />

Will render a horizontal ruler, aka a line, with the text "OR" in the center.

In pure css that becomes:

hr[title] {
    line-height: 0;
}

hr[title]::after {
    background: white;
    content: attr(title);
    display: table;
    height: 1rem;
    line-height: 1rem;
    margin: 0 auto;
    padding: 0 1rem;
    position: relative;
    top: -0.5rem;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment