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?

2 Answers 2

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>

Sign up to request clarification or add additional context in comments.

2 Comments

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 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.
1
.my_input_class {
  padding-right: 1em; // Replace `1em` with the desired amount of padding
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.