0

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?

Stickers
  • 75,527
  • 23
  • 147
  • 186
Bruno
  • 252
  • 1
  • 8

2 Answers2

1

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>
Stickers
  • 75,527
  • 23
  • 147
  • 186
  • Thanks! In my example I had to add a combination of width and padding-right. Just by adding padding-right, the input field was simply growing to the right and the issue was still there. – Bruno Jan 19 '17 at 15:21
  • 1
    @Bruno I also used box-sizing: border-box; in the example, so padding and borders will be part of the total width and height, that might help. – Stickers Jan 19 '17 at 15:23
1
.my_input_class {
  padding-right: 1em; // Replace `1em` with the desired amount of padding
}
fvgs
  • 21,412
  • 9
  • 33
  • 48