chihung
9/17/2019 - 8:53 AM

Horizontal Type Line Behind Text

Horizontal Type Line Behind Text

/**
 * Horizontal Type Line Behind Text
 * Inspired by this discussion @ CSS-Tricks: http://css-tricks.com/forums/discussion/comment/51357#Comment_51357
 * Available on jsFiddle: http://jsfiddle.net/ericrasch/jAXXA/
 * Available on Dabblet: http://dabblet.com/gist/2045198
 * Available on GitHub Gist: https://gist.github.com/2045198
 */

h2 {
    font: 33px sans-serif;
    margin-top: 30px;
    position: relative;
    text-align: center;
    text-transform: uppercase;
    z-index: 1;
}

h2:before {
    border-top: 2px solid #dfdfdf;
    content:"";
    margin: 0 auto; /* this centers the line to the full width specified */
    position: absolute; /* positioning must be absolute here, and relative positioning must be applied to the parent */
    top: 15px; left: 0; right: 0; bottom: 0;
    width: 95%;
    z-index: -1;
}

h2 span {
    /* to hide the lines from behind the text, you have to set the background color the same as the container */
    background: #fff;
    padding: 0 15px;
}

h2.double:before {
    /* this is just to undo the :before styling from above */
    border-top: none;
}

h2.double:after {
    border-bottom: 1px solid blue;
    -webkit-box-shadow: 0 1px 0 0 red;
    -moz-box-shadow: 0 1px 0 0 red;
    box-shadow: 0 1px 0 0 red;
    content: "";
    margin: 0 auto; /* this centers the line to the full width specified */
    position: absolute;
    top: 45%; left: 0; right: 0;
    width: 95%;
    z-index: -1;
}​
<!-- content to be placed inside <body>…</body> -->

<!-- In this example, the text will appear to have a strikethrough -->
<h2>Strikethrough title</h2>

<!-- We can get rid of the strikethrough look by adding a span tag and applying a background color to the span. Applying a background to the h2:after won't work because it won't adjust and hug the text. -->
<h2><span>Line-behind title</span></h2>

<!-- We can achieve a double-lined strikethrough by adding a box-shadow to the tag. You could probably also just use a top & bottom border, then position both. -->
<h2 class="double"><span>Double-lined title</span></h2>​
{"view":"split","seethrough":"","prefixfree":"1","page":"all"}