|
| 1 | +<!DOCTYPE html> |
| 2 | +<link rel="help" href="https://drafts.csswg.org/css-grid-1/#grid-item-sizing"> |
| 3 | +<link rel="help" href="https://www.w3.org/TR/css-sizing-3/#fit-content-size"> |
| 4 | +<link rel="author" title="Sammy Gill" href="mailto:sammy.gill@apple.com"> |
| 5 | +<meta name="assert" content="A grid item with an automatic size whose self-alignment is not stretch (and does not behave as stretch) is sized to its fit-content size in that axis, rather than filling its grid area."> |
| 6 | +<script src="/resources/testharness.js"></script> |
| 7 | +<script src="/resources/testharnessreport.js"></script> |
| 8 | +<link rel="stylesheet" href="/fonts/ahem.css"> |
| 9 | +<style> |
| 10 | + /* Ahem at 20px with a line-height of 1 makes every glyph a 20px by 20px box, |
| 11 | + so "XXXXX" is 100px wide and one line tall. */ |
| 12 | + .grid { display: grid; font: 20px/1 Ahem; } |
| 13 | + .wide-area { grid-template: 100px / 200px; } |
| 14 | + .narrow-area { grid-template: 100px / 60px; } |
| 15 | + |
| 16 | + .item { background: green; } |
| 17 | + |
| 18 | + /* Isolate the axis under test by giving the item a fixed size in the other axis. */ |
| 19 | + .automatic-width { width: auto; height: 40px; } |
| 20 | + .automatic-height { width: 100px; height: auto; } |
| 21 | +</style> |
| 22 | +<body> |
| 23 | +<div id="log"></div> |
| 24 | + |
| 25 | +<!-- Inline axis. The grid area is 200px wide and the item's max-content size is |
| 26 | + 100px, so a stretched item is 200px wide and a fit-content sized item is |
| 27 | + 100px wide. --> |
| 28 | +<div class="grid wide-area"><div class="item automatic-width" id="stretch-inline" style="justify-self: stretch">XXXXX</div></div> |
| 29 | +<div class="grid wide-area"><div class="item automatic-width" id="start-inline" style="justify-self: start">XXXXX</div></div> |
| 30 | +<div class="grid wide-area"><div class="item automatic-width" id="center-inline" style="justify-self: center">XXXXX</div></div> |
| 31 | +<div class="grid wide-area"><div class="item automatic-width" id="end-inline" style="justify-self: end">XXXXX</div></div> |
| 32 | + |
| 33 | +<!-- The fit-content size is clamped by the stretch-fit size, so an item in a |
| 34 | + 60px area whose min-content size is 40px and whose max-content size is |
| 35 | + 100px is 60px wide. --> |
| 36 | +<div class="grid narrow-area"><div class="item automatic-width" id="center-inline-clamped" style="justify-self: center">XX XX</div></div> |
| 37 | + |
| 38 | +<!-- Block axis. The grid area is 100px tall and the item's content is one 20px |
| 39 | + line tall, so a stretched item is 100px tall and a fit-content sized item |
| 40 | + is 20px tall. --> |
| 41 | +<div class="grid wide-area"><div class="item automatic-height" id="stretch-block" style="align-self: stretch">XXXXX</div></div> |
| 42 | +<div class="grid wide-area"><div class="item automatic-height" id="start-block" style="align-self: start">XXXXX</div></div> |
| 43 | +<div class="grid wide-area"><div class="item automatic-height" id="center-block" style="align-self: center">XXXXX</div></div> |
| 44 | +<div class="grid wide-area"><div class="item automatic-height" id="end-block" style="align-self: end">XXXXX</div></div> |
| 45 | + |
| 46 | +<script> |
| 47 | +function itemMetrics(id) { |
| 48 | + const item = document.getElementById(id); |
| 49 | + const gridRect = item.parentElement.getBoundingClientRect(); |
| 50 | + const itemRect = item.getBoundingClientRect(); |
| 51 | + return { |
| 52 | + width: itemRect.width, |
| 53 | + height: itemRect.height, |
| 54 | + left: itemRect.left - gridRect.left, |
| 55 | + top: itemRect.top - gridRect.top, |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +test(() => { |
| 60 | + const metrics = itemMetrics("stretch-inline"); |
| 61 | + assert_equals(metrics.width, 200, "used width"); |
| 62 | + assert_equals(metrics.left, 0, "offset from the grid container's left edge"); |
| 63 | +}, "justify-self: stretch on an item with an automatic width uses the stretch-fit size"); |
| 64 | + |
| 65 | +test(() => { |
| 66 | + const metrics = itemMetrics("start-inline"); |
| 67 | + assert_equals(metrics.width, 100, "used width"); |
| 68 | + assert_equals(metrics.left, 0, "offset from the grid container's left edge"); |
| 69 | +}, "justify-self: start on an item with an automatic width uses the fit-content size"); |
| 70 | + |
| 71 | +test(() => { |
| 72 | + const metrics = itemMetrics("center-inline"); |
| 73 | + assert_equals(metrics.width, 100, "used width"); |
| 74 | + assert_equals(metrics.left, 50, "offset from the grid container's left edge"); |
| 75 | +}, "justify-self: center on an item with an automatic width uses the fit-content size"); |
| 76 | + |
| 77 | +test(() => { |
| 78 | + const metrics = itemMetrics("end-inline"); |
| 79 | + assert_equals(metrics.width, 100, "used width"); |
| 80 | + assert_equals(metrics.left, 100, "offset from the grid container's left edge"); |
| 81 | +}, "justify-self: end on an item with an automatic width uses the fit-content size"); |
| 82 | + |
| 83 | +test(() => { |
| 84 | + assert_equals(itemMetrics("center-inline-clamped").width, 60, "used width"); |
| 85 | +}, "justify-self: center on an item with an automatic width clamps the fit-content size by the stretch-fit size"); |
| 86 | + |
| 87 | +test(() => { |
| 88 | + const metrics = itemMetrics("stretch-block"); |
| 89 | + assert_equals(metrics.height, 100, "used height"); |
| 90 | + assert_equals(metrics.top, 0, "offset from the grid container's top edge"); |
| 91 | +}, "align-self: stretch on an item with an automatic height uses the stretch-fit size"); |
| 92 | + |
| 93 | +test(() => { |
| 94 | + const metrics = itemMetrics("start-block"); |
| 95 | + assert_equals(metrics.height, 20, "used height"); |
| 96 | + assert_equals(metrics.top, 0, "offset from the grid container's top edge"); |
| 97 | +}, "align-self: start on an item with an automatic height uses the fit-content size"); |
| 98 | + |
| 99 | +test(() => { |
| 100 | + const metrics = itemMetrics("center-block"); |
| 101 | + assert_equals(metrics.height, 20, "used height"); |
| 102 | + assert_equals(metrics.top, 40, "offset from the grid container's top edge"); |
| 103 | +}, "align-self: center on an item with an automatic height uses the fit-content size"); |
| 104 | + |
| 105 | +test(() => { |
| 106 | + const metrics = itemMetrics("end-block"); |
| 107 | + assert_equals(metrics.height, 20, "used height"); |
| 108 | + assert_equals(metrics.top, 80, "offset from the grid container's top edge"); |
| 109 | +}, "align-self: end on an item with an automatic height uses the fit-content size"); |
| 110 | +</script> |
| 111 | +</body> |
0 commit comments