-2

I am trying to add a font-awesome icon to the top-left corner of a textarea. I tried using the following solution

.text-box {
  margin: 30px auto;
  min-width: 480px;
}

.text-container {
  margin: 30px auto;
  white-space: nowrap;
  position: relative;
}

.icon textarea {
  position: absolute;
  top: 50%;
  margin-left: 17px;
  margin-top: 17px;
  z-index: 1;
  color: black;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="text-box">
  <div class="text-container">
    <span class="text-icon">
      <i class="fa fa-list-ol"></i>
    </span>
    <textarea rows="3">
          
    </textarea>
  </div>
</div>

but the alignment is off and the input text is vertically/horizontally centered.

user1876508
  • 12,864
  • 21
  • 68
  • 105

1 Answers1

1

Here is a working example:

.text-box {
  margin: 30px auto;
  min-width: 480px;
}

.text-container {
  margin: 30px auto;
  white-space: nowrap;
  position: relative;
}
.text-container .text-icon {
  position: absolute;
  top: 0;
  left: 0;
}
.text-container textarea {
  position: absolute;
  top: 0;
  z-index: 1;
  color: black;
  background: none;
  padding-left: 15px;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>

<div class="text-box">
  <div class="text-container">
    <span class="text-icon">
      <i class="fa fa-list-ol"></i>
    </span>
    <textarea rows="3">This is the text</textarea>
  </div>
</div>
  1. Note the absolute position of both the textarea and the icon inside the container.
  2. Note the background of the textarea (i removed it so you will see the icon behind it). Another option is the use z-index to move the icon to the front.
Dekel
  • 60,707
  • 10
  • 101
  • 129