Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Automatic line decoration in HTML + CSS

Automatic line decoration in HTML + CSS

Problem

I've thought about this problem quite a bit.

Is there are way, say, to automatically insert a line decoration after each line (row) in a

or just any element that holds text?

For example:

This is the first line


This is the second line


I noticed this problem because underlines aren't ideal, and inserting a div or separating chunks of text in different divs is awkward. Because different characters have different widths in typography, If I used underscores the two lines become uneven in terms of width. I don't think tables can automatically separate a block of text in two rows. Not without some JQuery doing the split.

Is there something that will let define line decorations after lines ?

I need this because the text in the divs are coming from a database. I don't know the length of each line and so it's awkward If I assume a fixed number of characters per line and start chopping words in half at the end of lines,

like,

Hello World Wha


t is up.


Thanks!

Problem courtesy of: William Yang

Solution

Perhaps you can fake it with a "lined paper" effect. Here is one such result: http://css3.wikidot.com/blog:lined-paper-with-css

I'll copy it here to preserve it in case the original site moves or goes down.

The technique is quite simple - all we need is a repeating Background gradient to give the effect of lines across the paper, and a pseudo element on the left to give the ruled margin.

.paper {
    font: normal 12px/1.5 "Lucida Grande", arial, sans-serif;
    width: 300px;
    margin: 0 auto 10px;
    padding: 6px 5px 4px 42px;
    position: relative;
    color: #444;
    line-height: 20px;
    border: 1px solid #d2d2d2;

    background: #fff;
    background: -webkit-gradient(linear, 0 0, 0 100%, from(#d9eaf3), color-stop(4%, #fff)) 0 4px;
    background: -webkit-linear-gradient(top, #d9eaf3 0%, #fff 8%) 0 4px;
    background: -moz-linear-gradient(top, #d9eaf3 0%, #fff 8%) 0 4px;
    background: -ms-linear-gradient(top, #d9eaf3 0%, #fff 8%) 0 4px;
    background: -o-linear-gradient(top, #d9eaf3 0%, #fff 8%) 0 4px;
    background: linear-gradient(top, #d9eaf3 0%, #fff 8%) 0 4px;

    -webkit-background-size: 100% 20px;
    -moz-background-size: 100% 20px;
    -ms-background-size: 100% 20px;
    -o-background-size: 100% 20px;
    background-size: 100% 20px;

    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    border-radius: 3px;

    -webkit-box-shadow: 0 1px 2px rgba(0,0,0,0.07);
    -moz-box-shadow: 0 1px 2px rgba(0,0,0,0.07);
    box-shadow: 0 1px 2px rgba(0,0,0,0.07);
}
.paper::before {
    content: '';
    position: absolute;
    width: 4px;
    top: 0;
    left: 30px;
    bottom: 0;
    border: 1px solid;
    border-color: transparent #efe4e4;
}

In order to get all the text to line up properly on the lines, you need to specifically declare margins and line heights for all text elements. Given we used a background size of 20px above (i.e. it's 20px between the lines), we need to use a line height (or an equivalent line height + margin) of 20px.

.paper h1,
.paper h2 {
    font-size: 16px;
    line-height: 16px;
    margin: 0 0 4px;
}
.paper h3,
.paper h4,
.paper h5 {
    font-size: 14px;
    line-height: 16px;
    margin: 0 0 4px;
}
.paper h4,
.paper h5 {
    font-weight: normal;
}
.paper p {margin: 0 0 20px;}
.paper p:last-child {margin: 0;}
.paper ul {margin: 0 0 20px;}

Browser support

It works perfectly in Webkit and Opera, and almost perfectly in Firefox (the ruled margins on the left are 1 pixel too short at one end). Theoretically it should work in IE10, but I haven't tested it.

Solution courtesy of: Joe Frambach

Discussion

View additional discussion.



This post first appeared on CSS3 Recipes - The Solution To All Your Style Problems, please read the originial post: here

Share the post

Automatic line decoration in HTML + CSS

×

Subscribe to Css3 Recipes - The Solution To All Your Style Problems

Get updates delivered right to your inbox!

Thank you for your subscription

×