Fork on GitHub

lining.js icon lining.js

In CSS we already have the selector ::first-line to apply style on the first line of element. But there is no selector like ::nth-line(), ::nth-last-line() or even ::last-line. Then I read an article A Call for ::nth-everything from CSS tricks. ::nth-line() is actually really useful in some situation.

There comes Lining.js. It offers you complete down-to-the-line control like this:

<div class="poem" data-lining>Some text...</div>
<style>
.poem .line[first] { /* `.poem::first-line`*/ }
.poem .line[last] { /* `.poem::last-line` */ }
.poem .line[index="5"] { /* `.poem::nth-line(5)` */ }
.poem .line:nth-of-type(-n+2) { /* `.poem::nth-line(-n+2)` */ }
.poem .line:nth-last-of-type(2n) { /* `.poem:::nth-last-line(2n)` */ }
</style>
<script src="YOUR_PATH/lining.min.js"></script>

Supported browsers Lastest Chrome Lastest Firefox Lastest Safari Lastest Mobile Safari Lastest Opera

DEMO
As soon as wolf began to feel that he would like a decent meal, He went and knocked on Grandma's door.
When Grandma opened it, she saw
The sharp white teeth, the horrid grin,
And Wolfie said, 'May I come in?'
Poor Grandmamma was terrified,
'He's going to eat me up!' she cried.
.poem {
    line-height: 40px;
    font-size: 32px;
    font-family: 'Dancing Script' serif;
}
.poem .line:nth-of-type(-n+2) {
    font-size: 42px;
    color: #DA4453;
}
SHOW CODE NEXT
If you want to view paradise
Simply look around and view it
Anything you want to, do it
Want to change the world,
there's nothing to it
There is no life I know
To compare with
pure imagination
.fadeout {
    font-size: 36px;
    line-height: 36px;
    color: hsla(354, 67%, 46%, 1);
    font-family: 'Handlee' serif;
}
.fadeout .line:nth-last-of-type(3) {
   color: hsla(354, 67%, 55%, 1);
   font-size: 80%;
}
.fadeout .line:nth-last-of-type(2) {
   color: hsla(354, 67%, 60%, 1);
   font-size: 70%;
}
.fadeout .line:nth-last-of-type(1) {
   color: hsla(354, 67%, 65%, 1);
   font-size: 60%
}
SHOW CODE NEXT

What is typography

Typography (from the Greek words τύπος typos "form" and γράφειν graphein "to write") is the art and technique of arranging type to make written language most appealing to learning and recognition. The arrangement of type involves selecting typefaces, point size, line length, line-spacing (leading), letter-spacing (tracking), and adjusting the space within letters pairs (kerning[2]). Type design is a closely related craft, sometimes considered part of typography; most typographers do not design typefaces, and some type designers do not consider themselves typographers. In modern times, typography has been put in film, television and online broadcasts to add emotion to communication.
From Wikipedia.

.linenumber .line {
    position: relative;
    margin-left: 32px; /* 16 * 2*/
}
.linenumber .line:before {
    content: attr(index) '.';
    position: absolute;
    right: 10px;
    margin-right: 100%;
    width: 1em;
    font-size: 16px;
    text-align: center;
    color: #999;
}
SHOW CODE NEXT
桃花庵歌 唐寅
桃花塢裡桃花庵,桃花庵下桃花仙。
桃花仙人種桃樹,又摘桃花換酒錢。
酒醒只在花前坐,酒醉還來花下眠。
半醉半醒日復日,花落花開年復年。
但願老死花酒間,不願鞠躬車馬前。
車塵馬足顯者事,酒盞花枝隱士緣。
若將顯者比隱士,一在平地一在天。
若將花酒比車馬,彼何碌碌我何閒。
別人笑我太瘋癲,我笑他人看不穿。
不見五陵豪傑墓,無花無酒鋤作田。
.vtxt {
    font-size: 24px;
    color: #333;
    -webkit-writing-mode: vertical-rl;
    writing-mode: vertical-rl;
}
.vtxt .line:first-of-type {
    font-size: 1.5em;
    color: #D0496A;
}
.vtxt .line:first-of-type small {
    color: #333;
    font-size: .6666666em;
}
SHOW CODE NEXT

The Furthest Distance In The World

by Rabindranath Tagore

The furthest distance in the world
Is not between life and death
But when I stand in front of you
Yet you don't know that I love you.

The furthest distance in the world
Is not when I stand in front of you
Yet you can't see my love
But when undoubtly knowing the love from both
Yet cannot be together.

The furthest distance in the world
Is not being apart while being in love
But when I plainly cannot resist the yearning
Yet pretending you have never been in my heart.

The furthest distance in the world
Is not struggling against the tides
But using one's indifferent heart
To dig an uncrossable river
For the one who loves you.

SHOW CODE
<script src="YOUR_PATH/lining.min.js"></script>
<script src="YOUR_PATH/lining.effect.min.js"></script>

<div data-lining data-effect="rolling">
Your text...
<div>
PREVIEW
USAGE

BASIC USAGE

All you need to do is adding data-lining attribute on your block element and including the lining.min.js script. Then you can write css to control it's line style. For example:

<div class="poem" data-lining>Some text...</div>
<style>
.poem { /* default style for `.poem` */ }
.nolining .poem { /* style for `.poem` when browser don't support lining.js */ }
.poem[data-lining] { /* style for `.poem` when browser support lining.js */ }
.poem[data-lining="end"] { /* style for `.poem` when `text-line` tags created */ }
.poem .line { /* style for lines */ }
</style>
<script src="YOUR_PATH/lining.min.js"></script>

RWD

If you want your line style support Responsive web design. Make sure you add the data-auto-resize attribute. It will automatically relining when window resize event happen.

<div class="poem" data-lining data-auto-resize>Some text...</div>

Other attributes

data-from and data-to help you control which line tags to be created. For example:

<div class="poem"
    data-lining
    data-from="2"
    data-to="3">
First Line.<br/>
Second Line.<br/>
Third Line.<br/>
Fourth Line.<br/>
</div>

After lining, only the second and third line tag will be created. Check out the demo.

And data-line-class help you control the class name of line tags, if you don't want to use the default class name: line.

Javascript

You can also create and manage line tags by javascript. And give you four events to do special things.

<div id="poem">Some text..</div>
<script>
var poem = document.getElementById('poem');
lining.util.on(poem, 'beforelining', function (e) {
    // prevent lining if you want
    e.preventDefault();
}, false);
lining.util.on(poem, 'afterlining', function () {
    // do something after lining
}, false);
lining.util.on(poem, 'beforeunlining', function () {
    // do something before unlining
    // can't prevent unlining
}, false);
lining.util.on(poem, 'afterunlining', function () {
    // do something after unlining
}, false);


// start lining
var poemLining = lining(poem, {
    // optional config
    'autoResize': true,
    'from': 2,
    'to': 3,
    'lineClass': 'my-class'
});
// `unlining` method will remove all line tags.
poemLining.unlining();
// `relining` method will call `unlining` first if needed,
// then start lining again.
poemLining.relining();
</script>

lining.effect.js

lining.effect.js is an extra part of lining.js. It gives you the power to add appearances animation on your lines. Use it like this:

<script src="YOUR_PATH/lining.min.js"></script>
<script src="YOUR_PATH/lining.effect.min.js"></script>

<div data-lining data-effect="rolling">
Your text...
<div>
DOWNLOAD or $ bower install lining.js
or using CDN provided by jsDelivr
<script src="//cdn.jsdelivr.net/lining.js/0.3.2/lining.min.js"></script>