Add padding without changing overall width

后端 未结 4 1027
一整个雨季
一整个雨季 2021-01-04 03:05

How can I add padding to an element without adding on top of the predefined width?

It\'s good to define the width of a column to make a clean grid for the layout; bu

4条回答
  •  余生分开走
    2021-01-04 03:27

    Use box-sizing, it makes padding inclusive: https://developer.mozilla.org/en-US/docs/CSS/box-sizing

    Example:

    div {
        box-sizing:border-box;
        -moz-box-sizing:border-box; /* Firefox */
        -webkit-box-sizing:border-box; /* Safari */
    }
    

    It's worth noting that this won't work on IE7.

    For IE8, please see the top answer to this Q: box-sizing: border-box => for IE8?

提交回复
热议问题