-
Notifications
You must be signed in to change notification settings - Fork 707
/
Copy pathOverview.bs
524 lines (439 loc) · 19.6 KB
/
Overview.bs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<pre class='metadata'>
Title: CSS Box Model Module Level 3
Shortname: css-box
Level: 3
Status: REC
Prepare for TR: yes
Date: 2024-04-11
Implementation Report: https://wpt.fyi/results/css/css-box
Work Status: Completed
Group: csswg
ED: https://drafts.csswg.org/css-box-3/
TR: https://www.w3.org/TR/css-box-3/
Previous Version: https://www.w3.org/TR/2022/CRD-css-box-3-20221103/
Abstract: This specification describes the margin and padding properties, which create spacing in and around a CSS box.
Editor: Elika J. Etemad / fantasai, Apple, http://fantasai.inkedblade.net/contact, w3cid 35400
Ignored Terms: internal table elements, block layout
Issue Tracking: CSSWG GitHub https://github.com/w3c/csswg-drafts/issues
</pre>
<pre class='link-defaults'>
spec:css-sizing-3; type:dfn; text:size
</pre>
Introduction {#intro}
============
<p><em>This subsection is not normative.</em>
<p>CSS describes how each element
and each string of text in a source document
is laid out by transforming the <a>element tree</a>
into a set of <a>boxes</a>,
whose size, position, and stacking level on the <a>canvas</a>
depend on the values of their CSS properties.
Note: <a href="https://www.w3.org/TR/css-cascade/">CSS Cascading and Inheritance</a>
describes how properties are assigned to elements in the box tree,
while [[css-display-3#intro]] describes how the <a>element tree</a>
is transformed into the <a>box tree</a>.
<p>Each CSS <a>box</a>
has a rectangular content area,
a band of padding around the content,
a border around the padding,
and a margin outside the border.
The <a>sizing properties</a> [[css-sizing-3]],
together with various other properties that control layout,
define the size of the content area.
The box styling properties--
'padding' and its longhands,
'border' and its longhands,
and 'margin' and its longhands--
define the sizes of these other areas.
[=Margins=] and [=padding=] are defined in this module.
[=Borders=] are similarly defined in [[css-backgrounds-3]].
Note: This module only defines the [=physical=] per-side [=longhand=] properties.
Additional, [=flow-relative=] [=longhand=] properties
are defined in [[css-logical-1]].
Note that both sets of [=longhand=] properties control the same [=margins=]/[=padding=]:
they are just different ways of indexing each side.
<details class="note">
<summary>History of CSS Box module and the definition of Block Layout</summary>
<p>This module <a href="https://www.w3.org/TR/2018/WD-css3-box-20180731/">originally contained</a>
the CSS Level 3 specification prose relating to
box generation (now defined in [[css-display-3]]),
the box model (defined here),
as well as block layout (now only defined in [[CSS2]] Chapters 9 and 10).
Since its maintenance was put aside during the development of CSS2.1,
its prose was severely outdated by the time CSS2 Revision 1
was finally completed.
Therefore, the block layout portion of the prose has been retired,
to be re-synched to <a href="https://www.w3.org/TR/CSS2">CSS2</a> and updated
as input to a new Block Layout module at some point in the future.
It is being split apart from this module
and from the <a href="https://www.w3.org/TR/css-display/">CSS Display Module</a>
both because of the practical concern that it would be a huge amount of work
and also in recognition that CSS now has multiple layout models
(<a href="https://www.w3.org/TR/css-flexbox/">Flex Layout</a>,
<a href="https://www.w3.org/TR/css-grid/">Grid Layout</a>,
<a href="https://www.w3.org/TR/css-position/">Positioned Layout</a>,
and <a href="https://www.w3.org/TR/css-tables/">Table Layout</a>,
in addition to Block Layout)
which each deserve their own parallel module.
</details>
<h3 id="values">
Value Definitions</h3>
This specification follows the <a href="https://www.w3.org/TR/CSS2/about.html#property-defs">CSS property definition conventions</a> from [[!CSS2]]
using the <a href="https://www.w3.org/TR/css-values-3/#value-defs">value definition syntax</a> from [[!CSS-VALUES-3]].
Value types not defined in this specification are defined in CSS Values & Units [[!CSS-VALUES-3]].
Combination with other CSS modules may expand the definitions of these value types.
In addition to the property-specific values listed in their definitions,
all properties defined in this specification
also accept the <a>CSS-wide keywords</a> as their property value.
For readability they have not been repeated explicitly.
<h3 id="placement">
Module Interactions</h3>
<p>This module replaces the definitions of the margin and padding properties
defined in [[!CSS2]] sections 8.1, 8.2, 8.3 (but not 8.3.1), and 8.4.
<p>All properties in this module apply to the
''::first-line'' and ''::first-letter'' pseudo-elements.
The CSS Box Model {#box-model}
=================
<p export>Each box has a <dfn>content area</dfn>
(which contains its content--
text, descendant boxes, an image or other <a>replaced element</a> content, etc.)
and optional surrounding
<dfn lt="padding area">padding</dfn>,
<dfn lt="border area">border</dfn>,
and <dfn lt="margin area">margin areas</dfn>;
the size of each area is specified by corresponding properties,
and can be zero
(or in the case of margins, negative).
The following diagram shows how these areas relate
and the terminology used to refer to the various parts of the box:
<div class="figure">
<p><img src="images/box.png" alt="Diagram of a typical box, showing the
content, padding, border and margin areas">
<p class="caption">The various areas and edges of a typical box.
</div>
The background of the content, padding, and border areas of a box
is specified by its 'background' properties.
The border area can additionally be painted with a border style
using the 'border' properties.
Margins are always transparent.
See [[css-backgrounds-3]].
The margin, border, and padding can be broken down into
top, right, bottom, and left segments,
each of which can be controlled independently
by its corresponding property.
Boxes and Edges {#box-edges}
---------------
The perimeter of each of the four areas
(content, padding, border, and margin)
is called an <dfn export lt="box edge | edge" for="box">edge</dfn>,
and each [=edge=] can be broken down into
a top, right, bottom, and left side.
Thus each <a>box</a> has four [=edges=]
each composed of four sides:
<dl export>
<dt><dfn>content edge</dfn> or <dfn>inner edge</dfn>
<dd>
The content edge surrounds
the rectangle given by the width and height of the box,
which often depend on the element's content
and/or its <a>containing block</a> size.
The four sides of the <a>content edge</a> together
define the box's <dfn>content box</dfn>.
<dt><dfn>padding edge</dfn>
<dd>
The padding edge surrounds
the box’s padding.
If the padding has zero width on a given side,
the padding edge coincides with the content edge on that side.
The four sides of the <a>padding edge</a> together
define the box's <dfn>padding box</dfn>,
which contains both the
<a lt="content area">content</a>
and <a>padding areas</a>.
<dt><dfn>border edge</dfn>
<dd>
The border edge surrounds the box’s border.
If the border has zero width on a given side,
the border edge coincides with the padding edge on that side.
The four sides of the <a>border edge</a> together
define the box's <dfn>border box</dfn>,
which contains the box’s
<a lt="content area">content</a>,
<a lt="padding area">padding</a>,
and <a>border areas</a>.
<dt><dfn>margin edge</dfn> or <dfn>outer edge</dfn>
<dd>
The margin edge surrounds the box’s margin.
If the margin has zero width on a given side,
the margin edge coincides with the border edge on that side.
The four sides of the <a>margin edge</a> together
define the box's <dfn>margin box</dfn>,
which contains the all of the box’s
<a lt="content area">content</a>,
<a lt="padding area">padding</a>,
<a lt="border area">border</a>,
and <a>margin areas</a>.
</dl>
Fragmentation {#fragmentation}
-------------
When a box <a href="https://www.w3.org/TR/css-break-3/#fragmentation-model">fragments</a>--
is broken, as across lines or across pages, into separate <a>box fragments</a>--
each of its boxes
(<a>content box</a>, <a>padding box</a>, <a>border box</a>, <a>margin box</a>)
also fragments.
How the content/padding/border/margin areas react to fragmentation
is specified in [[css-break-3]]
and controlled by the 'box-decoration-break' property.
Box-edge Keywords {#keywords}
-----------------
The following <dfn><<box>></dfn> CSS keywords are defined for use
in properties (such as 'transform-box' and 'background-clip')
that need to refer to various box edges:
<dl dfn-type=value>
<dt><dfn dfn-for="<box>,<visual-box>,<layout-box>,<shape-box>,<geometry-box>,<paint-box>,<coord-box>">content-box</dfn>
<dd>
Refers to the [=content box=] or [=content edge=].
(In an SVG context, treated as ''<box>/fill-box''.)
<dt><dfn dfn-for="<box>,<visual-box>,<layout-box>,<shape-box>,<geometry-box>,<paint-box>,<coord-box>">padding-box</dfn>
<dd>
Refers to the [=padding box=] or [=padding edge=].
(In an SVG context, treated as ''<box>/fill-box''.)
<dt><dfn dfn-for="<box>,<visual-box>,<layout-box>,<shape-box>,<geometry-box>,<paint-box>,<coord-box>">border-box</dfn>
<dd>
Refers to the [=border box=] or [=border edge=].
(In an SVG context, treated as ''<box>/stroke-box''.)
<dt><dfn dfn-for="<box>,<layout-box>,<shape-box>,<geometry-box>">margin-box</dfn>
<dd>
Refers to the [=margin box=] or [=margin edge=].
(In an SVG context, treated as ''<box>/stroke-box''.)
<dt><dfn dfn-for="<box>,<geometry-box>,<paint-box>,<coord-box>">fill-box</dfn>
<dd>
Refers to the [=object bounding box=] or its edges.
(In a CSS box context, treated as ''<box>/content-box''.)
<dt><dfn dfn-for="<box>,<geometry-box>,<paint-box>,<coord-box>">stroke-box</dfn>
<dd>
Refers to the [=stroke bounding box=] or its edges.
(In a CSS box context, treated as ''<box>/border-box''.)
<dt><dfn dfn-for="<box>,<geometry-box>,<coord-box>">view-box</dfn>
<dd>
Refers to the nearest [=SVG viewport=] element’s
<dfn dfn lt="SVG viewport origin box" local-lt="origin box">origin box</dfn>,
which is a rectangle with the width and height
of the initial SVG <a spec=svg2>user coordinate system</a>
established by the <{svg/viewBox}> attribute for that element,
positioned such that its top left corner is anchored
at the coordinate system origin.
(In a CSS box context, treated as ''<box>/border-box''.)
Note: When the <{svg/viewBox}>
includes non-zero <var ignore>min-x</var> or <var ignore>min-y</var> offsets,
this [=origin box=] does not actually correspond
to the visible region rectangle defined by the <{svg/viewBox}>!
<!-- This is a legacy compat requirement.
See https://github.com/w3c/csswg-drafts/issues/4662 -->
</dl>
For convenience, the following value types are defined
to represents commonly used subsets of <<box>>:
<pre class="prod">
<dfn><<visual-box>></dfn> = content-box | padding-box | border-box
<dfn><<layout-box>></dfn> = <<visual-box>> | margin-box
<dfn><<paint-box>></dfn> = <<visual-box>> | fill-box | stroke-box
<dfn><<coord-box>></dfn> = <<paint-box>> | view-box
</pre>
Margins {#margins}
=======
<dfn export lt="margin">Margins</dfn> surround the border edge of a box,
providing spacing between boxes.
The <dfn export>margin properties</dfn> specify the thickness
of the <a>margin area</a> of a box.
The 'margin' <a>shorthand property</a>
sets the margin for all four sides
while the margin <a>longhand properties</a> only set their respective side.
This section defines the <a>physical</a> 'margin' <a>longhands</a>.
(Additional <a>flow-relative</a> 'margin' <a>longhands</a>
are defined in [[css-logical-1]].)
Note: Adjoining margins in <a>block layout</a> can <em>collapse</em>.
See <a href="https://www.w3.org/TR/CSS2/box.html#collapsing-margins">CSS2§8.3.1 Collapsing Margins</a>
for details.
Also, margins adjoining a <a>fragmentation break</a> are sometimes truncated.
See [[css-break-3#break-margins]] for details.
Page-relative (Physical) Margin Properties: the 'margin-top', 'margin-right', 'margin-bottom', and 'margin-left' properties {#margin-physical}
-------------------------------------------
<pre class="propdef">
Name: margin-top, margin-right, margin-bottom, margin-left
Value: <<length-percentage>> | auto
Initial: 0
Applies to: all elements except <a>internal table elements</a>
Inherited: no
Logical property group: margin
Percentages: refer to <a>logical width</a> of containing block
Computed value: the keyword ''margin/auto'' or a computed <<length-percentage>> value
Animation type: by computed value type
</pre>
These properties set the top, right, bottom, and left
<a>margin</a> of a <a>box</a>, respectively.
Negative values for margin properties are allowed,
but there may be implementation-specific limits.
Note: Application to [=internal ruby boxes=] [[CSS-RUBY-1]]
is not defined in this specification.
Margin Shorthand: the 'margin' property {#margin-shorthand}
-----------------
<pre class="propdef">
Name: margin
Value: <<'margin-top'>>{1,4}
Initial: 0
Applies to: see individual properties
Inherited: no
Percentages: refer to <a>logical width</a> of containing block
Computed value: see individual properties
Animation type: by computed value type
</pre>
The 'margin' property is a shorthand property for setting
'margin-top', 'margin-right', 'margin-bottom', and 'margin-left'
in a single declaration.
If there is only one component value,
it applies to all sides.
If there are two values,
the top and bottom margins are set to the first value
and the right and left margins are set to the second.
If there are three values,
the top is set to the first value,
the left and right are set to the second,
and the bottom is set to the third.
If there are four values
they apply to the top, right, bottom, and left, respectively.
<div class="example">
The following code demonstrates some possible 'margin' declarations.
<pre>
body { margin: 2em } /* all margins set to 2em */
body { margin: 1em 2em } /* top & bottom = 1em, right & left = 2em */
body { margin: 1em 2em 3em } /* top=1em, right=2em, bottom=3em, left=2em */
</pre>
The last rule of the example above is equivalent to the example below:
<pre>
body {
margin-top: 1em;
margin-right: 2em;
margin-bottom: 3em;
margin-left: 2em; /* copied from opposite side (right) */
}
</pre>
</div>
Padding {#paddings}
=======
<dfn export>Padding</dfn> is inserted between the content edge
and the padding edge of a box,
providing spacing between the content and the border.
The <dfn export>padding properties</dfn> specify the thickness
of the <a>padding area</a> of a box.
The 'padding' <a>shorthand property</a>
sets the padding for all four sides
while the padding <a>longhand properties</a> only set their respective side.
This section defines the <a>physical</a> 'padding' <a>longhands</a>.
(Additional <a>flow-relative</a> 'padding' <a>longhands</a>
are defined in [[css-logical-1]].)
Note: Backgrounds specified on the box
are by default laid out and painted within the padding edges.
(They are additionally painted underneath the border,
in the <a>border area</a>.)
This behavior can be adjusted
using the 'background-origin' and 'background-clip' properties.
Page-relative (Physical) Padding Properties: the 'padding-top', 'padding-right', 'padding-bottom', and 'padding-left' properties {#padding-physical}
-------------------------------------------
<pre class="propdef">
Name: padding-top, padding-right, padding-bottom, padding-left
Value: <<length-percentage [0,∞]>>
Initial: 0
Applies to: all elements except: <a>internal table elements</a> other than table cells
Inherited: no
Logical property group: padding
Percentages: refer to <a>logical width</a> of containing block
Computed value: a computed <<length-percentage>> value
Animation type: by computed value type
</pre>
These properties set the top, right, bottom, and left
<a>padding</a> of a <a>box</a>, respectively.
Negative values for padding properties are invalid.
Note: Application to [=internal ruby boxes=] [[CSS-RUBY-1]]
is not defined in this specification.
Padding Shorthand: the 'padding' property {#padding-shorthand}
-----------------
<pre class="propdef">
Name: padding
Value: <<'padding-top'>>{1,4}
Initial: 0
Applies to: see individual properties
Inherited: no
Percentages: refer to <a>logical width</a> of containing block
Computed value: see individual properties
Animation type: by computed value type
</pre>
The 'padding' property is a shorthand property for setting
'padding-top', 'padding-right', 'padding-bottom', and 'padding-left'
in a single declaration.
If there is only one component value,
it applies to all sides.
If there are two values,
the top and bottom padding are set to the first value
and the right and left padding are set to the second.
If there are three values,
the top is set to the first value,
the left and right are set to the second,
and the bottom is set to the third.
<div class="example">
The following code demonstrates some possible 'padding' declarations.
<pre>
body { padding: 2em } /* all padding set to 2em */
body { padding: 1em 2em } /* top & bottom = 1em, right & left = 2em */
body { padding: 1em 2em 3em } /* top=1em, right=2em, bottom=3em, left=2em */
</pre>
The last rule of the example above is equivalent to the example below:
<pre>
body {
padding-top: 1em;
padding-right: 2em;
padding-bottom: 3em;
padding-left: 2em; /* copied from opposite side (right) */
}
</pre>
</div>
Borders {#borders}
=======
<dfn export lt="border">Borders</dfn> fill the <a>border area</a>,
to visually delineate the edges of the box,
The <dfn export>border properties</dfn> specify the thickness
of the <a>border area</a> of a box,
as well as its drawing style and color.
See [[css-backgrounds-3]] for the definition of these properties,
including their [=physical=] longhands.
(Additional [=flow-relative=] border longhands
are defined in [[css-logical-1]].)
Changes {#changes}
=======
Recent Changes {#changes-recent}
--------------
There have only been minor editorial adjustments
since the <a href="https://www.w3.org/TR/css-box-3/">6 April 2023 Recommendation</a>:
* Adding some subheadings to [[#box-model]] and moving a paragraph accordingly.
* Changing “document tree” to “element tree” in the [[#intro]]
to align with the terminology used in [[css-display-3]].
* Adding the “Logical Property Group” line to the property definition tables.
Changes Since CSS Level 2 {#changes-since-L2}
-------------------------
The following changes have been made to this module
since <a href="https://www.w3.org/TR/CSS2/box.html">CSS Level 2</a>:
<ul>
<li>Marked application to internal ruby boxes as undefined in this level.</li>
<li>Adapting the prose slightly to account for vertical <a>writing modes</a>.
<li>Cross-linking to relevant concepts in [[css-break-3]] and [[css-backgrounds-3]].
<li>Providing a [[#keywords|centralized common definition]]
of keywords that reference the various box edges
for use in properties defined outside this spec
(e.g. 'background-clip')
to avoid repetition (and the resulting inevitable synchronization errors).
</ul>
Privacy Considerations {#priv}
===============================================
No privacy considerations have been reported on this module.
Security Considerations {#sec}
===============================================
No security considerations have been reported on this module.