简体   繁体   English

如何在textarea的占位符之前添加一个图标

[英]How to add an icon right before the placeholder in textarea

I'd like to add an icon right before the placeholder in a textarea but don't know how to do it.我想在文本区域的占位符之前添加一个图标,但不知道该怎么做。 Here is my code:这是我的代码:

<div class="center-part">
  <div class="user-input">
    <textarea class="share" name="share" type="text" 
         placeholder=""> 
    </textarea>
  </div>            
</div>

Thank you.谢谢你。

It is not possible to combine an icon and text in a placeholder.无法在占位符中组合图标和文本。 Because you are only able to use 1 font in it.因为您只能在其中使用 1 种字体。 If you could use different fonts inside the placeholder then this would be possible by using fontAwesome .如果您可以在占位符内使用不同的 fonts ,那么这可以通过使用fontAwesome来实现。

You can add a span (positioned inside the textarea) and shift the placeholder to the right of the icon by doing this:您可以添加一个跨度(位于文本区域内)并将占位符移动到图标的右侧,方法是:

HTML (added span in your code): HTML(在您的代码中添加跨度):

<div class="center-part">
  <div class="user-input">
     <textarea class="share" name="share" type="text" placeholder="&emsp;&emsp;Icon before this"></textarea>
<span class="icon"></span>
  </div>            
</div>

CSS: CSS:

.center-part {
  position: relative;  
  min-height: 50px;
  min-width: 200px;
}

.share {
  min-height: 50px;
  min-width: 200px;
}

.icon {
  position: absolute;
  background: red;
  width: 15px;
  height: 15px;
  left: 10px;
  top: 5px;
}

You can see a demo at Codepen你可以在Codepen看到一个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM