Box parts
Identify each layer and its purpose.
CodeBhavyaCalculate component sizes, follow the page flow and keep every explanation readable.
Level 11 explained which CSS declarations apply. This lesson explains how their sizes and layout choices affect the space a component needs. You will separate content, padding, border and margin; compare box-sizing modes; follow normal flow; and recognize when overflow or positioning is causing a problem.
Build a workshop card with predictable dimensions and enough room for readable content. Use a controlled box lab to compare declared width with the actual border-box width, then explain a layout issue without hiding the content.
Identify each layer and its purpose.
Calculate a simple horizontal box and use size constraints.
Understand how ordinary blocks and inline content are arranged.
Check overflow, positioning and containing space before adding overrides.
For the block-box examples here, think of four nested areas. Content is the space for text or other children. Padding is the space between that content and the border. The border surrounds the padded area. Margin is space outside the border.
These areas answer different questions. Increase padding when text is too close to the card edge. Increase a margin or a layout gap when separate cards are too close to each other. Changing one can affect the required size, but it does not make the other areas interchangeable.
A background ordinarily paints behind content and padding and extends under the border by default; margin itself is outside that background. The visible color behind a margin often comes from another element. A margin is not an extra colored wall attached to the card.
The actual text or child content area.
Breathing room around content, then a visible or invisible boundary.
Outside spacing relative to neighboring content.
In the ordinary content-box mode, width specifies the content width. The browser then adds horizontal padding and borders to produce the border-box width. A 200px content width does not automatically mean a 200px card from outside border to outside border.
The example calculations use a block box, symmetric pixel values, a definite width and no min/max constraints or layout-driven shrinking. Those assumptions keep the arithmetic clear. A real layout can impose further constraints that change the used size.
.card {
box-sizing: content-box;
width: 200px;
padding: 20px;
border: 4px solid #173451;
}Predict: 200px content plus 40px horizontal padding plus 8px horizontal border produces a 248px border box. Margin, if any, is considered outside that border box.
Height also depends on content, line wrapping and sizing rules. Do not infer a fixed height merely from a stated width; a narrower text area can wrap into more lines.
Padding separates content from the border. It is useful for readable panels, buttons and input controls. A four-value padding shorthand follows top, right, bottom and left. Two values mean vertical then horizontal padding; one value applies to all four sides.
.notice { padding: 12px 20px; }
.card { padding: 10px 16px 20px 24px; }Padding contributes to the box dimensions. In content-box mode, adding it enlarges the border box beyond a stated content width. In border-box mode, it consumes space within the stated border-box width and can reduce the space left for the content.
Padding cannot be negative. When padding needs to depend on writing direction, logical properties such as padding-inline and padding-block express inline and block axes rather than fixed left/right assumptions. Use physical directions for the arithmetic in this lesson and logical properties where the component needs to adapt.
A border surrounds the content and padding. Its width contributes to the box dimensions; its style and color define its appearance. A border declaration normally needs a visible style, such as solid, to paint a visible boundary.
.card { border: 2px solid #bdd5e6; border-radius: 12px; }
button:focus-visible { outline: 3px solid #126d96; outline-offset: 3px; }An outline does not occupy space in the box model in the same way a border does. It is useful for focus indicators because showing focus need not push neighboring content. Check that an outline remains visible rather than being clipped by a surrounding overflow rule.
Border-radius rounds corners; it does not change an elementโs semantic meaning. A rounded div remains a div. Increasing border width can change a content-box cardโs outer size, so inspect it when two components unexpectedly stop fitting side by side.
Margins separate a box from other content. A layout gap separates items or tracks in a supported layout such as Flexbox or Grid. Decide whether spacing belongs to an individual component or to the container arranging the components.
In normal block flow, some vertical margins can collapse rather than add. Two adjacent positive vertical margins of 20px and 30px can produce a 30px collapsed margin under the relevant conditions. Horizontal margins do not collapse in that way, and margins of flex or grid items do not collapse with each other.
Margin collapsing also has parent/child and empty-block cases. Do not memorize โall margins collapse.โ Borders, padding and formatting contexts can change the situation. If a parent appears to move when its first child gains a top margin, inspect those conditions.
.stack { display: flex; flex-direction: column; gap: 16px; }
.stack > .card { margin: 0; }This example assigns spacing to the container. It avoids relying on adjacent child vertical margins for the stack. A later lesson will explore Flexbox alignment in detail.
Use the same values consistently: width 200px, padding 20px on each side, border 4px on each side and margin 10px on each side. There is no layout-driven shrinking or min/max constraint in this example.
Content width = 200px
Horizontal padding = 20px + 20px = 40px
Horizontal border = 4px + 4px = 8px
Border-box width = 200px + 40px + 8px = 248px
Border box plus horizontal margins = 248px + 10px + 10px = 268pxThe last line measures the horizontal space for this simple box plus its two margins. It is not a universal formula for collapsed vertical spacing or every layout algorithm.
If you set width: 100% on a content-box child and add padding and borders, the required border box can exceed the intended containing width. This is a common reason a panel appears wider than its parent. Check the box-sizing mode rather than simply hiding the overflow.
With box-sizing: border-box, the declared width describes the border box. Padding and border fit inside that declared size. Margin remains outside it. Using the previous values, a 200px border box leaves 152px for content after subtracting 40px padding and 8px border.
.card {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 4px solid #173451;
margin: 10px;
}
/* Content width: 200 - 40 - 8 = 152px
Border box + horizontal margins: 200 + 20 = 220px */Border-box does not include margin and does not make oversized padding disappear. The content size cannot become negative; extremely large padding and border can still force a larger minimum border box. The lab uses values well within the declared width so the simple subtraction is valid.
A common project foundation is box-sizing: border-box for elements and pseudo-elements. That is a chosen styling convention, not a claim that every browser element starts in exactly the same mode.
Width, min-width and max-width describe related constraints. A component may grow to the available size but stop at a sensible maximum. Width: 100% with border-box helps a simple card include its padding and border within the intended width.
.card {
box-sizing: border-box;
width: 100%;
max-width: 40rem;
margin-inline: auto;
padding: 1.25rem;
}Auto inline margins can center this constrained block when there is available inline space in this ordinary case. They are not a universal โcenter anythingโ switch across all layouts.
Min-width can prevent shrinking. A long unbroken token can also influence a flex or grid itemโs automatic minimum size. When a column refuses to shrink, inspect the minimum and the content. A targeted min-width: 0 on the relevant item can help it shrink; text wrapping or a local scroller may still be needed.
Let ordinary text panels grow with their content. A fixed height that looks correct for one sentence can clip a longer explanation, a translated label or enlarged text. Use min-height when a baseline size is useful while still allowing growth.
.explanation { min-height: 8rem; height: auto; }
.long-token { overflow-wrap: anywhere; }A height percentage needs a relevant definite basis under the applicable layout rules. Height: 100% is not always โfill the screen.โ Viewport units describe viewport-related sizes, while parent-based percentages follow their own resolution rules.
Wrapping affects height because the same text can occupy more lines in a narrower box. Verify both the width and the readable content flow before trying to force a panel into a matching fixed height. For examples with large code lines or tables, a local horizontal scroller may be more suitable than forcing every token to wrap.
Normal flow is the ordinary arrangement of content before you introduce other layout or positioning behavior. Block boxes in typical horizontal writing appear one after another in the block direction. Inline content flows along lines and wraps when the line needs more room.
A paragraph can become taller when its text wraps, and following content naturally moves down. This behavior is useful for learning pages because explanations vary in length. Absolute positioning every paragraph removes that cooperation and makes overlap likely.
Keep a logical source order for reading and keyboard use. Visual rearrangement alone does not necessarily change the order encountered by assistive technology or focus. A good layout starts with meaningful HTML in a sensible sequence.
A typical block participates as a block in normal flow. Width and height can apply under the relevant box rules.
Text-level content participates in line layout. Ordinary non-replaced inline boxes do not treat width/height like a block box.
A box participates in the inline flow while providing a box that can accept dimensions.
The element type and display value are related but different. A semantic element can have a changed display value.
.badge { display: inline-block; padding: 4px 10px; }Images and some other replaced elements have additional sizing behavior. Avoid turning the shorthand โinline ignores sizeโ into a rule for every possible element. For text spans, use an appropriate box type when a real sized box is needed.
Display affects how a box participates in its surrounding layout and how its children are arranged. A flex or grid container introduces a layout for its direct items, while the container still has a role in its parentโs layout.
Display: none removes the elementโs boxes from layout. Visibility: hidden normally preserves its layout space while hiding it. Opacity: 0 makes it transparent but does not by itself remove its layout, focusability or interaction. These differences matter for hidden panels and keyboard controls.
.actions { display: flex; gap: 12px; }
.cards { display: grid; gap: 16px; }A visually hidden control must not remain an unexplained keyboard stop. Choose the appropriate hidden-state behavior for the component and keep its accessible state synchronized. CSS appearance alone is not an accessibility strategy.
Overflow occurs when content extends beyond an applicable box. Overflow: visible permits it to extend; hidden clips it; auto can provide scrolling when needed. Clip also clips, with different scrolling behavior from hidden. Choose based on how the learner should reach the content.
A table or long code sample can use a local horizontal scrolling wrapper. That avoids making the whole page wider while preserving the information. Make the scroller usable by keyboard where needed and keep a visible focus indicator.
.code-scroller { max-width: 100%; overflow-x: auto; }
.code-scroller:focus-visible { outline: 3px solid #126d96; }
<!-- HTML -->
<div class="code-scroller" tabindex="0" aria-label="Scrollable code example">
<pre>Long code example goes here.</pre>
</div>Do not use overflow: hidden as a general repair for unreadable or oversized teaching content. First find the incorrect size, automatic minimum or unbroken token. Clipping may remove the exact information the learner needs to read.
Static uses normal positioning. Relative offsets a box while its original place in flow remains reserved.
Takes the box out of normal flow and positions it using its containing block. Other content does not automatically reserve its place.
Usually uses the viewport as its reference, but some ancestors can establish a different containing block.
Participates in flow, then is constrained by its scrolling context and containing area when an applicable inset is set.
.card { position: relative; }
.card .badge { position: absolute; top: 12px; right: 12px; }
.section-label { position: sticky; top: 1rem; }The relative card establishes the expected reference for the badge in this simple example. Leave enough room so the badge does not cover the title. For sticky behavior, inspect the scroll container, available travel space and inset. An ancestorโs overflow can change which scrolling context matters.
Z-index orders boxes within stacking rules; a huge number does not automatically escape a lower ancestor stacking context. For a lesson header, measure the header and reserve room for content instead of piling larger z-index values onto overlapping panels.
Use ordinary flow for a reading sequence. Use Flexbox when arranging items along a main axis is central, such as a row of actions or a vertical stack. Use Grid when you want an explicit track arrangement across rows and columns. Both can wrap or create additional structure, so โone-dimensionalโ and โtwo-dimensionalโ describe their design focus rather than an absolute limit on where boxes can appear.
For a row of buttons, the parent can supply display: flex, gap and wrapping. For a card collection, a grid can supply column tracks and gaps. Do not use absolute positioning to imitate a whole ordinary card layout unless the task genuinely needs that arrangement.
.actions { display: flex; flex-wrap: wrap; gap: 12px; }
.cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }This is an introduction to choosing the tool. Level 13 will develop Flexbox, Grid and responsive design in detail, including alignment and how the track layout changes on smaller screens.
Follow the content-box arithmetic, then compare the same declared width in border-box mode. Select a stage or use playback. Each stage states the meaning of the measured width so content size and outside space are not confused.
This content-box example begins with a definite 200px content width.
Content width = 200pxThe demonstration uses symmetric pixel values with no layout shrinking or min/max constraints.
Change the sizing mode and symmetric horizontal values. The preview draws a local block box; the calculated result describes the stated model. It is not a measurement of every possible real CSS layout. This lab deliberately avoids shrinking, min/max constraints and negative content widths.
The pale outer wrapper helps you see the margin space. The cyan area is padding, the dark line is the border and the blue inner area is content. If the preview is narrower than the example, scroll inside the preview to see it; the fixed sizes are teaching values, not a suggested mobile page layout.
Default content-box example: 200px content, 248px border box and 268px including horizontal margins.
Inspect content, padding, border and margin separately in the browser box-model tools.
Look at width, min/max sizes, box-sizing and automatic minimums.
Find out whether the box is in normal flow, a flex/grid item or absolutely positioned.
Locate the overflow cause before clipping or hiding the content.
For a card wider than its parent, check whether width: 100% describes content-box width plus extra padding and border. For a column that will not shrink, check an automatic minimum and long unbroken content. For a footer covered by another panel, check whether a positioned element has left normal flow.
For a sticky label that does not stick as expected, inspect the relevant ancestor scrolling context and whether there is room to move. For clipped text after a font increase, inspect a fixed height or overflow rule.
Change one condition at a time and verify the user-visible result. Avoid making an entire course header or footer part of a broad component override. A successful fix keeps the learning content readable and the page navigable.
No. The declared border-box width includes content, padding and border. Margin remains outside it.
The declared width describes content. Horizontal padding and borders can add to that width and exceed the intended container space.
No. Padding is inside the border around content; margin is outside the border relative to surrounding boxes.
No. Some vertical margins collapse in normal block flow. The conditions matter; horizontal margins and flex/grid item margins do not follow that same collapsing rule.
No. It removes the box from normal flow, so other flow content does not automatically reserve its space.
Its automatic minimum or long unbroken content may constrain it. Inspect the relevant item; a targeted min-width: 0 and suitable wrapping or scrolling may help.
It may clip the explanation. Find the incorrect sizing or content constraint and preserve a way to read all information.
A relevant non-auto inset, an appropriate scrolling context and room within its containing constraints. Ancestor overflow can affect the behavior.
Use width 160px, padding 10px and border 2px per side. Expected border box: 184px; with 10px margins per side: 204px.
Keep those values but set border-box. Expected content width: 136px; border box: 160px; with horizontal margins: 180px.
Set padding to 0px, then 20px with content-box width 200px and border 4px. Expected border widths: 208px then 248px.
Change only margin from 0px to 20px per side. Expected: content and border widths are unchanged; horizontal outside spacing increases by 40px.
Put a long code line in a local overflow-x auto wrapper. Expected: information remains available inside the wrapper without making the whole lesson wider.
Offset a relative badge, then compare an absolute badge inside a relative card. Expected: relative retains its flow place; absolute needs deliberate space planning.
Double an explanationโs text length and increase font size. Expected: an auto-height panel grows rather than clips.
Build a title, explanation, lab and summary in source order. Expected: layout styling preserves a useful reading and keyboard sequence.
The area allocated to the elementโs content.
Space between the content and the border.
The area including content, padding and border.
Spacing outside the border relative to surrounding layout.
The property that changes what a declared width or height includes.
The ordinary arrangement of content under the applicable formatting rules.
The reference area used for certain sizes and positioning calculations.
Content extending beyond the relevant box area.
The combining of certain vertical margins under specific normal-flow conditions.
A grouping within which stacking order is resolved as part of the pageโs stacking rules.
Create a workshop card with a heading, explanation, prerequisites and a native disclosure. Use border-box, readable padding, a visible border and a flexible maximum width. Let the text determine the required height. Put any genuinely long code sample inside a local scrolling wrapper.
Write down a fixed-size test case before switching to a flexible layout: choose width 200px, padding 20px and border 4px. Explain both the content-box and border-box calculations, then verify the browserโs box-model display. State the assumptions that make the numbers meaningful.
Add a small absolute badge inside a relatively positioned card only if you can keep it clear of the title and enlarged text. Use normal flow for the rest of the content. Test a narrow viewport and keyboard focus, and ensure no footer or lesson content is covered.
Success criterion: explain the four areas, predict the simple widths, preserve access to all text and identify the actual cause of a layout problem. Level 13 will extend this work with Flexbox, Grid and responsive design.
Before marking complete, compare both box-sizing modes, calculate the stated example, preserve a long code line without clipping, and explain a normal-flow versus positioned layout choice. Completion stores a local study milestone; it is not an assessment score.