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

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

I have the following CSS and HTML snippet being rendered.

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

    If you pad and offset it like this:

    textarea
    {
        border:1px solid #999999;
        width:100%;
        padding: 7px 0 7px 7px; 
        position:relative; left:-8px; /* 1px border, too */
    }
    

    the right side of the textarea perfectly aligns with the right side of the container, and the text inside the textarea aligns perfectly with the body text in the container... and the left side of the textarea 'sticks out' a bit. it's sometimes prettier.

    0 讨论(0)
  • 2020-11-29 15:09

    I came across another solution here that is so simple: add padding-right to the textarea's container. This keeps the margin, border, and padding on the textarea, which avoids the problem that Beck pointed out about the focus highlight that chrome and safari put around the textarea.

    The container's padding-right should be the sum of the effective margin, border, and padding on both sides of the textarea, plus any padding you may otherwise want for the container. So, for the case in the original question:

    textarea{
        border:1px solid #999999;
        width:100%;
        margin:5px 0;
        padding:3px;
    }
    .textareacontainer{
        padding-right: 8px; /* 1 + 3 + 3 + 1 */
    }
    

    <div class="textareacontainer">
        <textarea></textarea>
    </div>
    
    0 讨论(0)
  • 2020-11-29 15:11

    If you're not too bothered about the width of the padding, this solution will actually keep the padding in percentages too..

    textarea
    {
        border:1px solid #999999;
        width:98%;
        margin:5px 0;
        padding:1%;
    }
    

    Not perfect, but you'll get some padding and the width adds up to 100% so its all good

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