Thursday, April 18, 2013

Line-On-Sides Headers

Line-On-Sides Headers

Forums user McAsh wondered how they might code up a design in which there was a large header and a smaller subheader beneath it which featured double horizontal lines jutting out after the text to both the left and right of the centered text. An easy thing to mock up as image, but a much more difficult thing to pull off in CSS because of the variable nature of text (length, size, etc).

I coded up one way to do it which works, but isn't quite perfect. I figured I'd post it here and you all could chime in with better ways if you can think of them.

If the background was a solid color, this would be fairly easy. Apply the lined background to the subhead and center a span in the middle with a bit of padding and a background color to match the solid background. We don't have a solid background color here. Perhaps some trickery using the same background image but fixing the background position would work, but I didn't go there.

Instead, I used the ::before and ::after pseudo elements to create the left and right set of lines. The text is still in a span, which is relatively positioned. The right set is a pseudo element on that span which starts 100% from the left with a bit of margin to push it away and vice versa for the left set. Both are of a fixed height and use border-top and border-bottom to create the lines. Thus no backgrounds are used and the insides of everything is transparent.

The length of the lines is long enough to always break out of the parent container and they are cut off by hidden overflow on that parent.

.fancy {
	line-height: 0.5;
	text-align: center;
.fancy span {
	display: inline-block;
	position: relative;  
.fancy span:before, .fancy span:after {
	content: "";
	position: absolute;
	height: 5px;
	border-bottom: 1px solid white;
	border-top: 1px solid white;
	top: 0;
	width: 600px;
.fancy span:before {
	right: 100%;
	margin-right: 15px;
.fancy span:after {
	left: 100%;
	margin-left: 15px;


There are two things I don't love about this.

  1. The line-height: 0.5 thing in there to center the lines around the text feels like a "magic number" to me which might not be right depending on the font.
  2. The width of the pseudo elements lines isn't exactly what they need to be, but way longer than they need to be and cut off.

Neither are that huge of a big deal but little things that probably could be cleverly fixed.

via CSS Tricks by