如何为文本区域中的每一行设置底部边框?

16

我想知道如何在 <textarea> 的每一行上添加一个 border-bottom 线,但是我只能让最后一行显示这个样式。

是否有办法实现这个效果?

.input-borderless {
  width: 80%;
  border-top: 0px;
  border-bottom: 1px solid green;
  border-right: 0px;
  border-left: 0px;
  background-color: rgb(241,250,247);
  text-decoration: none;
  outline: none;
  display: block;
  padding: 10px 0;
  margin: 20px 0;
  font-size: 1.6em;
}
<textarea rows="3" class="input-borderless" placeholder="Describe the project"></textarea>


1
使用所需高度的线条为其添加背景图像。 - Nikolay Ermakov
我希望这个能够响应式。难道没有其他的方法吗? - Becky
你能详细描述一下你所说的“响应式”吗? - Stickers
如果我给它添加背景图片,那么在较小的屏幕上它的大小会变得奇怪。难道没有其他方法吗? - Becky
1
您可以使用媒体查询来更改background-size属性,并使背景图像在不同的视口中按照您的要求进行缩放。此外,您的类名似乎非常矛盾。 - nabrown
1
可能是在每行文本下方带有线条的Textarea的重复问题。 - user4157770
5个回答

12

您可以使用渐变作为背景图像,以获得类似下划线的效果:

JSFiddle

textarea
{
  line-height: 4ch;
  background-image: linear-gradient(transparent, transparent calc(4ch - 1px), #E7EFF8 0px);
  background-size: 100% 4ch;
}

1
我会更进一步地使用 ch 单位,这是字符“0”的高度。然后你可以调整字体大小,一切都仍然有效。 - Donnie D'Amato
1
这是一种现代化的解决方案,完全实现了CSS(没有重复的背景图片等),应该被接受为答案! - loretoparisi

5
这个想法是使用“层”,将文本区域作为顶层,多行内容作为底层。
我在容器
元素上使用了伪元素来创建底部图层,因为伪类:before和:after不能用在

我尝试了这个,但无法使其工作。我在某种程度上将其调整为我的代码。我不知道我做错了什么... https://jsfiddle.net/LyadLn5t/#&togetherjs=zFJxkJ1jrI - Becky
我用更好的方法更新了答案。顺便说一下,你评论中的fiddle几乎是空的,也许你粘贴了错误的链接。 - Stickers

3

上周我发现了 contenteditable。今天早上,我突然想到一个想法:在文本域中使用SVG背景没有JS的帮助是不能滚动的,但是我敢打赌,在DIV中它会滚动得很好。看吧!

#wrapper {
  display: inline-block;
  width: 10em;
  height: 4em;
  border: 1px solid gray;
  overflow: auto;
}
#paper {
  min-height: 4em;
  line-height: 1em;
  vertical-align: bottom;
  background-size: 1px 1em;
  background-repeat: repeat;
  background-position: 0 0;  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1 1em'%3E%3Crect id='background' height='1em' width='1' y='0' x='0' fill='%23f1faf7'/%3E %3Cline id='underline' y2='1em' x2='0' y1='1em' x1='1' stroke='%23008000' fill='none'/%3E %3C/svg%3E");
  /*for placeholder, see https://codepen.io/flesler/pen/AEIFc */
}
<textarea id="TA"></textarea>
<div id="wrapper"><div contenteditable="true" id="paper">I am trying to figure out how to add a border-bottom line for every row within a, however I am only able to get the very bottom row's border-bottom to do this.</div></div>


1

所有内容,减去内部填充。

$(document).ready(function() {
  $('textarea.styleme').scroll(function(event) {
    $(this).css({'background-position': '0 -' + $(this).scrollTop() + 'px'})
  })
})
textarea.styleme {
  width: 80%;
  border: 0px;
  /*background-color: rgba(241,250,247, .5);
  background-image: url('https://placehold.it/350x100');*/
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 20'%3E%3Crect id='background' height='20' width='10' y='0' x='0' fill='%23f1faf7'/%3E %3Cline id='underline' y2='20' x2='0' y1='20' x1='10' stroke='%23008000' fill='none'/%3E %3C/svg%3E");
  background-size: 10px 20px;
  background-repeat: repeat;
  background-position: 0 0;
  text-decoration: none;
  outline: none;
  display: block;
  /*padding: 10px 0;*/
  margin: 20px 0;
  font-size: 1.6em;
  line-height: 1em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea spellcheck="false" class="styleme" placeholder="Describe the project" rows="5">Vestibulum vel sem porttitor, suscipit odio sit amet, egestas arcu. Nam varius felis eu ligula pellentesque vestibulum. Pellentesque tempor, nisi sit amet fringilla consequat, ex erat malesuada dui, non dapibus nunc felis laoreet nibh. Maecenas elementum commodo enim quis hendrerit. Ut sit amet malesuada dui. Praesent dolor purus, mollis vel venenatis eget, malesuada sed nulla. Sed at dolor lobortis, malesuada tortor ut, ultrices enim. Nullam posuere dolor massa. Nullam dignissim, dolor at tempus sagittis, massa eros semper quam, a posuere massa massa et neque. Praesent finibus massa eu interdum auctor. Vestibulum id risus massa.</textarea>


0

如何在<textarea>中下划线所有文本

您的问题说您想要在<textarea>中每一行都有一个border-bottom线,因此我的答案可能不完全符合您的需求。我会发布它以防对您或其他人有用。

textarea { text-decoration: underline; }
<textarea></textarea>

jsFiddle

示例输出:

enter image description here


他不是要求文本下划线,而是要求在文本区域的每一行上画线。 - artuc
这已经包含在我的答案中了。请阅读第一段。@artuc - Michael Benjamin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接