CSSのボックスモデルの構造をCSSアニメーションで可視化
Post on:2021年2月1日
CSSのボックスモデルの構造をCSSアニメーションで可視化したインタラクティブなデモを紹介します。
こういうのを実装する機会は限られますが、何かあった時にやってみたいですね。
CSSのボックスモデルをCSSアニメーションで可視化
最近のボックスモデルについての詳しい解説は、以前の記事をご覧ください。
実際の動作は、下記でお楽しみください。
サイズがここだと小さいので、下部の0.5xか上部のEditから見るとよいです。
See the Pen
Explode A Node: 3D Box Model by Adam Argyle (@argyleink)
on CodePen.
実装コードは、下記の通り。
1 2 3 4 5 6 7 8 9 |
<div class="stack"> <div class="content card"> <p><b><span id="desktop-prompt">Hover</span><span id="mobile-prompt">Tap</span> me!</b></p> </div> <div class="padding card"></div> <div class="border card"></div> <div class="background card"></div> <div class="box-shadow card"></div> </div> |
CSSの表現力もかなりパワーアップしていますね。
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 |
@use postcss-preset-env { stage: 0 } .stack { display: grid; grid: [stack] / [stack]; perspective: 5000; &:before { content: ""; position: fixed; inset: 0; background: lch(95 0 0); transition: background 2s ease; pointer-events: none; } &:matches(:hover, :active) { &:before { background: lch(15 0 0); } } &:matches(:hover, :active) .card { --bg-opacity: 5%; transition-duration: 2s; transition-timing-function: var(--easeInOutCirc); transform: rotateX(45deg) rotate(45deg) translateZ(var(--z-offset)) translateY(20vh) translateX(20vh) ; &:nth-child(1) { --z-offset: 75vh; } &:nth-child(2) { --z-offset: 50vh; } &:nth-child(3) { --z-offset: 25vh; } &:nth-child(4) { --z-offset: 0vh; } &:nth-child(5) { --z-offset: -25vh; } &:nth-child(1)::after { transition-delay: 1.5s; } &:nth-child(2)::after { transition-delay: 1.7s; } &:nth-child(3)::after { transition-delay: 1.9s; } &:nth-child(4)::after { transition-delay: 2.1s; } &:nth-child(5)::after { transition-delay: 2.3s; } &::after { opacity: 1; transform: rotate(-45deg) rotateY(45deg) translateX(0px); } &.content { background: transparent; color: white; border-color: transparent; transition: border-color 1s var(--easeInOutCirc) 0s, background-color 1s var(--easeInOutCirc) 0s, color 1s var(--easeInOutCirc) 0s, transform 2s var(--easeInOutCirc) 0s; } &.padding { transition-delay: .1s; } &.border { transition-delay: .2s; } &.background { background: white; transition: background-color 2s var(--easeInOutCirc) .3s, transform 2s var(--easeInOutCirc) .2s; } &.box-shadow { transition-delay: .4s; box-shadow: 0 -1px 25px rgba(0, 0, 0, 0.4), 0 7.6px 6.1px rgba(0, 0, 0, 0.051), 0 14.3px 11.5px rgba(0, 0, 0, 0.059), 0 25.5px 20.5px rgba(0, 0, 0, 0.07), 0 47.6px 38.4px rgba(0, 0, 0, 0.094), 0 114px 92px rgba(0, 0, 0, 0.19) ; } } } .card { grid-area: stack; --z-offset: 0; --easeInOutCirc: cubic-bezier(0.85, 0, 0.15, 1); --bg-opacity: 0%; transition: transform 4s ease; inline-size: 40vmin; block-size: 40vmin; box-sizing: border-box; color: white; box-siing: border-box; background: lch(100 0 0 / 5%); position: relative; @media (orientation: landscape) { inline-size: 40vh; block-size: 40vh; } @media (max-width: 540px) { inline-size: 60vw; block-size: 60vw; } &:nth-child(1) { z-index: 3; } &:nth-child(2) { z-index: 2; } &::after { content: "Box"; position: absolute; left: -45%; top: 110%; font-size: 1.25rem; text-shadow: 0 1px 3px lch(0 0 0 / 75%); white-space: nowrap; padding: 1ch 2ch; border-radius: 4ch; opacity: 0; transition: opacity .5s ease 0s, transform .5s ease; transform: rotate(-45deg) rotateY(45deg) translateX(20px); background: lch(0 0 0 / 70%); @media (max-width: 540px) { left: -20%; top: 70%; } } &.content { z-index: 5; transition: border-color 4s ease, background-color 5s ease 2s, color 4s ease 0s, transform 4s ease 0s; padding: 5vmin; font-size: max(2.5vmin, .9rem); line-height: 1.5; background: lch(100 0 0); border: 5px solid hotpink; color: black; &::after { content: "Content Box"; left: -50%; @media (max-width: 540px) { left: -23%; } } } &.padding { background: transparent; z-index: 4; transition-delay: .1s; &::after { content: "Padding Box"; } &::before { content: ""; position: absolute; inset: 0; clip-path: polygon( 0% 0%, 0% 100%, 5vmin 100%, 5vmin 5vmin, calc(100% - 5vmin) 5vmin, calc(100% - 5vmin) calc(100% - 5vmin), 5vmin calc(100% - 5vmin), 5vmin 100%, 100% 100%, 100% 0% ); background: repeating-linear-gradient( -45deg, cyan, cyan 1px, lch(100 0 0 / 5%) 1px, lch(100 0 0 / 5%) 10px ); } } &.border { z-index: 3; background: transparent; transition-delay: .2s; &::after { content: "Border Box"; } &::before { content: ""; position: absolute; inset: 0; clip-path: polygon( 0% 0%, 0% 100%, 5px 100%, 5px 5px, calc(100% - 5px) 5px, calc(100% - 5px) calc(100% - 5px), 5px calc(100% - 5px), 5px 100%, 100% 100%, 100% 0% ); background: linear-gradient(hotpink, hotpink); } } &.background { z-index: 2; transition: background-color 4s ease .3s, transform 4s ease .3s; &::after { content: "Background Box"; } } &.box-shadow { z-index: 1; background: transparent; transition: box-shadow 4s ease .4s, transform 4s ease .4s; box-shadow: 0 2.8px 2.2px rgba(0, 0, 0, 0.02), 0 6.7px 5.3px rgba(0, 0, 0, 0.028), 0 12.5px 10px rgba(0, 0, 0, 0.035), 0 22.3px 17.9px rgba(0, 0, 0, 0.042), 0 41.8px 33.4px rgba(0, 0, 0, 0.05), 0 100px 80px rgba(0, 0, 0, 0.07) ; &::after { content: "Shadow Box"; } } } body { min-block-size: 100vh; display: flex; align-items: center; justify-content: center; font-family: system-ui; background: lch(30 0 0); } p:first-of-type { margin-block-start: 0; } @media (hover:hover) { #mobile-prompt { display: none; } } @media (hover:none) { #desktop-prompt { display: none; } } |
sponsors