4

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

enter image description here

<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);
Muhamad Serawan
  • 445
  • 5
  • 16
  • 7
    I guess you should use `background-image` for this – antyrat Aug 05 '13 at 09:48
  • 8
    A placeholder is *designed* to disappear on focus. I think you’re better off just setting a bottom border in CSS if you want a text field underlined liked a traditional printed form. – Martin Bean Aug 05 '13 at 09:49
  • @antyrat I tried this but there was a problem when i'm scrolling down and up – Muhamad Serawan Aug 05 '13 at 09:49
  • 1
    I have the same problem please help us – Steve Aug 05 '13 at 09:54
  • 1
    use "contenteditable" http://stackoverflow.com/questions/3613184/how-can-i-create-an-html-input-text-area-such-that-i-can-underline-or-format-cer – Tushar Gupta - curioustushar Aug 05 '13 at 09:55
  • 1
    @MuhamadSerawan you are right, i tried it a while a go, and if you scroll down/up, the background image will be wrong placed. – Tarek Aug 05 '13 at 09:57
  • http://stackoverflow.com/questions/8304436/css-styling-text-areas-like-notebook-look – poke Aug 05 '13 at 11:47
  • @poke please read the answer and comments below ,the answer by Chandrakant – Tarek Aug 05 '13 at 11:49
  • http://stackoverflow.com/questions/12790831/styling-textarea-with-background-that-scrolls-with-text – jantimon Aug 05 '13 at 12:04
  • 2
    Here you have a solution: [How to scroll a background image together with text in textarea?](http://stackoverflow.com/questions/12790645/how-to-scroll-a-background-image-together-with-text-in-textarea) `background-image` for a *single-line* or a `contenteditable``div`. – gmo Aug 05 '13 at 12:14
  • @gmo totally working, thank you man, i suggest you to put your comment as an answer so Muhammad can assign it as a right answer – Tarek Aug 05 '13 at 12:25

2 Answers2

4

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>
poke
  • 369,085
  • 72
  • 557
  • 602
1

Here you have a solution: How to scroll a background image together with text in textarea?

Two options...

  1. The background-image must be for a single-line (with repeat)
  2. A contenteditable div.

Credits goes to @ana for her original answer.

Community
  • 1
  • 1
gmo
  • 8,860
  • 3
  • 40
  • 51