A centered card can stubbornly stay against one side after you try to set CSS padding to auto. For an HTML div, padding needs a valid size; use that size for the inner space and a margin or layout rule to position the card.
HTML div padding: valid values and the box model
Padding is the space between an element’s content and its border. It also extends the area covered by the element’s background. The browser accepts nonnegative lengths such as 16px and 1rem, percentages, and calculations that resolve to valid values. It does not accept auto as a padding value. The MDN padding reference lists the syntax.
The css padding auto combination is invalid because auto is outside that syntax. The browser ignores an invalid declaration. If an earlier valid padding rule applies, that rule can still determine the padding; otherwise, the initial padding is zero. In a shorthand declaration such as padding: 1rem auto, the invalid value makes the whole declaration invalid, rather than just the horizontal part.
For HTML div padding, choose the space the content actually needs. A fixed 1rem gives consistent breathing room as the container changes size. Separate values let you control each axis: padding: 1rem 2rem sets the top and bottom to 1rem and the left and right to 2rem. With the default content-box sizing, padding adds to a declared content width. With border-box, it is included in the declared width.
Center the box and keep its inner space
This example centers a card in a wrapper while leaving space around the text inside it:
<div class="wrapper">
<div class="card">A centered card with room around its text.</div>
</div>
.wrapper {
width: 100%;
}
.card {
box-sizing: border-box;
width: 100%;
max-width: 32rem;
margin: 0 auto;
padding: 1rem 2rem;
border: 1px solid #555;
}
The card can fill a narrow wrapper, but it stops growing at 32rem. When the wrapper is wider, the horizontal auto margins share the remaining space. On a narrow screen there may be no spare horizontal space to share, so the card fills the wrapper. Its padding still separates text from the border. This is a practical replacement for trying to use auto padding in CSS to center the card.
For a more complex layout, put display: flex and justify-content: center on the parent instead. Use align-items: center as well if the parent has vertical space and its children need centering on that axis. Set padding on the child separately. Flex alignment controls the item’s position; the child’s padding controls the space inside its border.
How percentage padding is calculated
Percentage padding may look automatic, but it follows a specific reference size. In a typical horizontal writing mode, all four sides are calculated from the containing block’s width, including the top and bottom. For example, a block with padding: 5% inside a 400px-wide containing block gets 20px of padding on each side. If that width becomes 600px, each side becomes 30px. In other writing modes the reference is the containing block’s inline size.
This makes percentage padding responsive to the container’s width, but it does not distribute leftover space to center the element. When sizing an HTML div with percentage padding, account for the resulting space on both sides, especially if the element also has a set width. box-sizing: border-box can make the total width easier to manage.
Find the failed declaration
If changing the value has no visible effect, inspect the element in browser developer tools. An invalid padding: auto declaration is typically crossed out or flagged; the computed styles show which valid rule supplied the padding. Replace it with a length or percentage. If the box still sits to one side, give it room within its parent and use horizontal auto margins or a parent layout rule to center it.
