Brad Woods Digital Garden

Notes / CSS / Box sizing

The Warhammer 40k Adeptus Mechanicus symbol

Table of contents

    Wireframe of nested rectangles

    CSS box-sizing

    Planted: 

    Tended: 

    Status: decay

    Hits: 892

    Intended Audience: Front-end developers

    The CSS box-sizing property can be set to:

    • content-box or
    • border-box

    content-box [default]

    When this value is set and you set the width of an element, you are setting the width of the content box. The core of the onion. The total width of the onion will be this width plus the width of the 3 other layers.

    Wireframe of nested rectangles labeled margin, border, padding and content

    border-box

    When this value is set and you set the width of an element, you are setting the width of the border box. This is made up of the onion core and 2 of the layers. The total width of the onion will be this width plus the width of outer-most layer.

    Wireframe of nested rectangles labeled margin, border, padding and content

    border-box is more commonly used than content-box. Use the snippet below to set it on all elements.

    /index.css

    *, ::before, ::after {
    box-sizing: border-box;
    }

    Working with Margins

    When working with margins, their size can be influenced by things other than the margin value. See consistent / varying gaps.

    References

    Feedback

    Have any feedback about this note or just want to comment on the state of the economy?

    Where to next?