Solution 1 :

You need to specify width manually, as input doesn’t react as a div.

Most of the time, absolutely positioned elements that have height and
width set to auto are sized so as to fit their contents

https://developer.mozilla.org/en-US/docs/Web/CSS/position

.wrapper {
  position: relative;
  width: 300px;
  height: 300px;
  background-color: blue;
}

div, input{
  position: absolute;
  left: 20px;
  right: 20px;
  height: 20px;
}

div {
  top: 0;
  background-color: red;
}

input {
  top: 20px;
  background-color: orange;
  width: calc(100% - 40px); /* does not react as a div, need to manually specify width */
  border: 0; /* remove default border */
  padding: 0; /* remove default padding */
}
<div class="wrapper">
  <div></div>
  <input>
</div>

Problem :

Is there a simple way to let the input grow from left to right like the div in my example do? Why does the input tag (and also button) not work like a div in this case (is the missing </input> the problem for tags like this)?

Example HTML:

<div class="wrapper">
  <div></div>
  <input>
</div>

Example CSS:

.wrapper {
  position: relative;
  width: 300px;
  height: 300px;
  background-color: blue;
}

div {
  position: absolute;
  top: 0;
  left: 20px; /* <-- working */
  right: 20px; /* <-- working */
  height: 20px;
  background-color: red;
}

input {
  position: absolute;
  top: 20px;
  left: 20px; /* <-- isn't working */
  right: 20px; /* <-- isn't working */
  height: 20px;
  background-color: orange;
  border: 0;
}

Example Fiddle

Comments

Comment posted by Debsmita Paul

you have a typo in

Comment posted by Pepe

Sry, was too fast, thx, but it doesnt solve the problem.

Comment posted by Warden330

input and button elements have a standard-format other than divs. That makes them behave differently too. You will have to add a width manually to such elements

Comment posted by Pepe

Ah, ok, “width: calc” do the trick, good to know that this is working for elements like this – thx for help!

By