Why don't <textarea> and <input> elements respect max-width?

后端 未结 3 1512
情话喂你
情话喂你 2021-01-19 00:34

The following HTML:


         


        
3条回答
  •  误落风尘
    2021-01-19 00:59

    Ran into this very problem this morning. While the first answer relates to mine, I feel further explanation is necessary:

    Working Demo: jsFIDDLE Demo

    1) As stated above, first it's important to set inputs to 100% width at the top of your css file:

    input,textarea {
         width:100%;
         display:block }
    

    2) The input will now, by default, extent 100% of the length of the parent div. So if you want to achieve a "max-width," you need to set width properties within the parent div.

    .input-container {
         max-width: 300px; }
    

    3) Add padding to the parent div to give a fixed space to the right/left of the input

    .input-container {
         padding: 25px 25px;
         max-width: 300px; }
    

    Let me know if something isn't clear.

    jsFIDDLE Demo

提交回复
热议问题