How to make an element width: 100% minus padding?

前端 未结 14 1456
庸人自扰
庸人自扰 2020-11-21 07:33

I have an html input.

The input has padding: 5px 10px; I want it to be 100% of the parent div\'s width(which is fluid).

However using widt

相关标签:
14条回答
  • 2020-11-21 07:54

    Just understand the difference between width:auto; and width:100%; Width:auto; will (AUTO)MATICALLY calculate the width in order to fit the exact given with of the wrapping div including the padding. Width 100% expands the width and adds the padding.

    0 讨论(0)
  • You can do this:

    width: auto;
    padding: 20px;
    
    0 讨论(0)
  • 2020-11-21 08:02

    What about wrapping it in a container. Container shoud have style like:

    {
        width:100%;
        border: 10px solid transparent;
    }
    
    0 讨论(0)
  • 2020-11-21 08:03

    Use padding in percentages too and remove from the width:

    padding: 5%;
    width: 90%;
    
    0 讨论(0)
  • 2020-11-21 08:07

    Use css calc()

    Super simple and awesome.

    input {
        width: -moz-calc(100% - 15px);
        width: -webkit-calc(100% - 15px);
        width: calc(100% - 15px);
    }​
    

    As seen here: Div width 100% minus fixed amount of pixels
    By webvitaly (https://stackoverflow.com/users/713523/webvitaly)
    Original source: http://web-profile.com.ua/css/dev/css-width-100prc-minus-100px/

    Just copied this over here, because I almost missed it in the other thread.

    0 讨论(0)
  • 2020-11-21 08:08

    For me, using margin:15px;padding:10px 0 15px 23px;width:100%, the result was this:

    The solution for me was to use width:auto instead of width:100%. My new code was:

    margin:15px;padding:10px 0 15px 23px;width:auto. Then the element aligned properly:

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