Search code examples
htmlcssinput-field

Use just part of the input field with CSS


I have an input field, and inside it on the right side there is a string that displays information to the user.

<div class="my_div_class">
    <input class="my_input_class" type="text" placeholder="Search">
    <span class="my_span_class">6000 available</span>
</div>

Using position relative and absolute, I places the span inside the input field.

However, if the user types a long query, the text will be under the span text.

Is there a way to force the input field to do the horizontal scroll when the user reaches a point before the right margin, ideally without using javascript?


Solution

  • You can add some padding-right to the input box.

    .my_div_class {
      position: relative;
      width: 200px;
    }
    .my_input_class {
      width: 100%;
      padding-right: 100px;
      box-sizing: border-box;
    }
    .my_span_class {
      position: absolute;
      right: 0;
      top: 0;
    }
    <div class="my_div_class">
      <input class="my_input_class" type="text" placeholder="Search">
      <span class="my_span_class">6000 available</span>
    </div>