Initially, with the margin-left
set to auto
, the margins and padding of the div are all zero width, while the borders have a width of 1px.
For block-level elements in normal flow, the following equation must be true:
'margin-left' + 'border-left-width' + 'padding-left' + 'width'
+ 'padding-right' + 'border-right-width' + 'margin-right'
= width of containing block
However, when all the values are set as such, the equation becomes impossible, causing the div to be considered "over-constrained." Consequently, the margin-right property is adjusted to rectify this issue, positioning the content box on the left side of the container.
By setting margin-left:auto
, the div is no longer over-constrained, and the margin-left value is recalculated accordingly to satisfy the equation mentioned earlier. This results in the content box being located on the right side of the container.
Understanding this equation can provide valuable insights into various CSS layout behaviors. Further information can be found at https://drafts.csswg.org/css2/visudet.html#blockwidth