How to make the placeholder not fade out of focus

I'm trying to emphasize the background of the text area, so I made the placeholder value equal 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 fix it?

 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); 
+4
source share
2 answers

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

Two options...

  • background-image should be for one line (with repetition)
  • A contenteditable div .

Credits are sent to @ana for her original answer.

+1
source

As others have said, you should use the background to display constant lines under the text. The only problem is that the background is attached to the element, and not to the text, so scrolling the text does not move the background; and in fact, it is impossible to block scrolling only to full lines.

Therefore, instead, we should make a full text area, so the background is always correct. Then we put this textarea inside the shell element, which determines the height and puts additional content in the overflow. In order for the text area to automatically use the required height, we do not use the textarea element, but instead a contenteditable div.

The background comes from the CSS3 linear gradient, which is designed to set the line height 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> 
+4
source

Source: https://habr.com/ru/post/1495287/


All Articles