0

Why is input element does not take up 100% of the width of its container automatically after changing its display to block? Are there some other factors which also have an influence on that? Thanks. Demo see below:


some explanation: 1. I comment out width:100% intentionally because block level element is supposed to take up 100% of its container width.


#container {
  width: 300px;
  margin: auto;
  background-color: red;
}

input[type="text"] {
  display: block;
  opacity:0.5;
  /*width:100%;*/
}
<body>
  <section>
    <div id="container">
      <input type="text">
    </div>
  </section>
</body>
好吃仙人
  • 137
  • 1
  • 10

4 Answers4

0

I'm not an expert, but I'm pretty sure it's because you have commented out width:100%. try decommenting that then it should work

#container {
  width: 300px;
  margin: auto;
  background-color: red;
}

input[type="text"] {
  display: block;
  opacity:0.5;
  width:100%;
}
jgh
  • 88
  • 1
  • 8
0

Changed the code check now

#container {
  width: 300px;margin: auto;
  background-color: red;
}

input[type="text"] {
  opacity:0.5;
  width:100%;
border-width:0;
padding:0;
}
<body>
  <section>
    <div id="container">
      <input type="text">
    </div>
  </section>
</body>
S B
  • 1,363
  • 12
  • 21
0

The input element by default has a border: 2px and a padding: 1px 0 in google chrome

Box model of input element

When you were actually applying a width of 100%, the input actually had a width greater than the actual div outside covering it

width of input(set to width of div) + border + padding > width of div

There is a tiny little white area on the right, in case you uncomment width:100% in your code. That white area actually is the input. If you set the border to zero that's really enough to fix things

#container {
  width: 300px;
  margin: auto;
  background-color: red;
}

input[type="text"] {
  display: block;
  opacity: 0.5;
  width: 100%;
  border: 0
}
<body>
  <section>
    <div id="container">
      <input type="text">
    </div>
  </section>
</body>
Community
  • 1
  • 1
0

Default size of input is 20, so if you do not define size or css rule for your input automatically its size is 20. The best solution is adding width. try this code:

#container
{
  width: 300px;
  margin: auto;
  background-color: red;
}
input[type="text"] 
{
  display: block;
  opacity:0.5;
  width:100%;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

If you want to be responsive it is better to add box-sizing to all element like this:

* 
{
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
}
  • 20 _what_? I tried the above example with: `em`, `ex`, `pc`, `ch`, `%`, `vw`, `rem`, `pt`. Nothing fits the same size. PS.: I didn't tried `px`, `vh` nor print-units like `cm`, `in`, ... – Werner Apr 13 '17 at 11:59
  • Have you try **box-sizing** ? This CSS Specify that elements should have padding and border included in the element's total width and height. – saleh katebi Apr 13 '17 at 12:06
  • Okay, i figured out the unit you are talking about is actually the html-width (``). – Werner Apr 13 '17 at 13:25
  • 20 is borrowers default size of an input. You should determine width for your input. – saleh katebi Apr 16 '17 at 08:12