Reputation: 445
I'm trying to underline the background of the text area so i made the placeholder value equals to '__', this will make the text area like this
<textarea placeholder="____________________________________________"></textarea>
I tried this but the lines disappeared when i change the value of the text area, so how can i make it fixed?
var textarea=document.getElementById("textarea");
var value="";
for(var i=textarea.getAttribute("rows"); i > 1;i--)
for(var j=textarea.getAttribute("cols"); j >=0 ;j--)
value+="_";
$('#textarea').attr('placeholder',value);
Upvotes: 4
Views: 1444
Reputation: 387567
As others said, you should use a background to display permanent lines below the text. The only problem is that the background is attached to the element, not to the text, so scrolling the text will not move the background; and it’s not really possible to lock the scrolling to full lines only.
So instead, we have to make the text area full height, so the background is always correct. Then we put that textarea inside a wrapper element which defines the height and puts the further content in an overflow. To make the text area automatically use the necessary height, we don’t use a textarea element but instead a contenteditable div.
The background comes from a CSS3 linear gradient which is designed to fit the line height of the text inside:
.textbox {
border: 1px solid #CCC;
margin: auto;
width: 600px;
height: 100px;
overflow-y: auto;
}
.textbox div[contenteditable] {
font: 16px/18px monospace;
padding: 0px 10px;
min-height: 100%;
/* line-height - 1px for 1px high lines */
background-image: linear-gradient(to bottom, transparent 17px, #CCC 0px);
/* height is equal to line-height */
background-size: 100% 18px;
}
<div class="textbox">
<div contenteditable>
Text here.<br />
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget
dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes,
nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium
quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet
nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae,
justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus.
Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula
porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in,
viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet.
Quisque rutrum. Aenean imperdiet. Etiam ultricies nisi vel augue. Curabitur
ullamcorper ultricies nisi. Nam eget dui. Etiam rhoncus. Maecenas tempus, tellus
eget condimentum rhoncus, sem quam semper libero, sit amet adipiscing sem neque sed
ipsum. Nam quam nunc, blandit vel, luctus pulvinar, hendrerit id, lorem. Maecenas
nec odio et ante tincidunt tempus. Donec vitae sapien ut libero venenatis faucibus.
Nullam quis ante. Etiam sit amet orci eget eros faucibus tincidunt. Duis leo. Sed
fringilla mauris sit amet nibh. Donec sodales sagittis magna. Sed consequat, leo
eget bibendum sodales, augue velit cursus nunc.
</div>
</div>
Upvotes: 4
Reputation: 9000
Here you have a solution: How to scroll a background image together with text in textarea?
Two options...
background-image
must be for a single-line (with repeat)contenteditable div
.Credits goes to @ana for her original answer.
Upvotes: 1