How can I make a TextArea 100% width without overflowing when padding is present in CSS?

前端 未结 15 2087
失恋的感觉
失恋的感觉 2020-11-29 14:44

I have the following CSS and HTML snippet being rendered.

<
相关标签:
15条回答
  • 2020-11-29 14:47

    The answer to many CSS formatting problems seems to be "add another <div>!"

    So, in that spirit, have you tried adding a wrapper div to which the border/padding are applied and then putting the 100% width textarea inside of that? Something like (untested):

    textarea
    {
      width:100%;
    }
    .textwrapper
    {
      border:1px solid #999999;
      margin:5px 0;
      padding:3px;
    }
    <div style="display: block;" id="rulesformitem" class="formitem">
      <label for="rules" id="ruleslabel">Rules:</label>
      <div class="textwrapper"><textarea cols="2" rows="10" id="rules"/></div>
    </div>

    0 讨论(0)
  • 2020-11-29 14:48

    How about negative margins?

    textarea {
        border:1px solid #999999;
        width:100%;
        margin:5px -4px; /* 4px = border+padding on one side */
        padding:3px;
    }
    
    0 讨论(0)
  • 2020-11-29 14:49

    Use box sizing property:

    -moz-box-sizing:border-box; 
    -webkit-box-sizing:border-box; 
    box-sizing:border-box;
    

    That will help

    0 讨论(0)
  • 2020-11-29 14:51

    I was looking for an inline-styling solution instead of CSS solution, and this is the best I can go for a responsive textarea:

    <div style="width: 100%; max-width: 500px;">
      <textarea style="width: 100%;"></textarea>
    </div>
    
    0 讨论(0)
  • 2020-11-29 14:51

    For people who use Bootstrap, textarea.form-control can lead to textarea sizing issues as well. Chrome and Firefox appear to use different heights with the following Bootstrap CSS:

    textarea.form-conrtol{
        height:auto;
    }
    
    0 讨论(0)
  • 2020-11-29 14:56

    Why not forget the hacks and just do it with CSS?

    One I use frequently:

    .boxsizingBorder {
        -webkit-box-sizing: border-box;
           -moz-box-sizing: border-box;
                box-sizing: border-box;
    }
    

    See browser support here.

    0 讨论(0)
提交回复
热议问题