导航

v2.1.0
媒体

设备框

用手机外框呈现截图、视频或实时演示。

本页内容

设备框以手机机身为外框,呈现截图、视频或实时演示。整体采用同一种材质,像用黏土塑成的模型:凸起面板构成机身,屏幕四周有窄边框,顶部的摄像头位置有一处小凹槽。外框用轮廓表明这是手机,所有色彩都由屏幕内容决定,就像桌面端的窗口一样。

设备

<div class="device" style="--device-width: 13.5rem">  <img class="device-screen" src="/demo/moonstone.svg" alt="A pale stone over grey hills"></div>
<div className="device" style={{ '--device-width': '13.5rem' } as React.CSSProperties}>  <img className="device-screen" src="/demo/moonstone.svg" alt="A pale stone over grey hills" /></div>
<div class="device" style="--device-width: 13.5rem">  <img class="device-screen" src="/demo/moonstone.svg" alt="A pale stone over grey hills"></div>
<template>  <div class="device" style="--device-width: 13.5rem">    <img class="device-screen" src="/demo/moonstone.svg" alt="A pale stone over grey hills">  </div></template>

#屏幕

将 device-screen 应用于手机显示的内容,可以是 <img>、<video>、<iframe> 或一段标记。屏幕填满机身边框内的区域,图像按屏幕边界裁切。屏幕的圆角半径等于机身的圆角半径减去边框宽度,使两者之间的间距在转角处保持一致。

用标记构成屏幕时,从摄像头凹槽下方开始排布内容。像真实手机的状态栏那样,在顶部留出约为机身宽度六分之一的空间。

用标记构成屏幕

<div class="device" style="--device-width: 13.5rem">  <div class="device-screen bg-surface-base flex flex-col gap-sm px(14) pt(44) pb(14)">    <p class="eyebrow">Wednesday</p>    <p class="heading heading-lg">Today</p>    <ul class="checklist mt(4)">      <li class="checklist-item">        <label class="checklist-label">          <input class="checkbox" type="checkbox" checked><span class="checklist-title">Review the contract</span>        </label>      </li>      <li class="checklist-item">        <label class="checklist-label">          <input class="checkbox" type="checkbox"><span class="checklist-title">Call Grace at three</span>        </label>      </li>      <li class="checklist-item">        <label class="checklist-label">          <input class="checkbox" type="checkbox"><span class="checklist-title">Book the flight</span>        </label>      </li>    </ul>  </div></div>
<div className="device" style={{ '--device-width': '13.5rem' } as React.CSSProperties}>  <div className="device-screen bg-surface-base flex flex-col gap-sm px(14) pt(44) pb(14)">    <p className="eyebrow">Wednesday</p>    <p className="heading heading-lg">Today</p>    <ul className="checklist mt(4)">      <li className="checklist-item">        <label className="checklist-label">          <input className="checkbox" type="checkbox" defaultChecked /><span className="checklist-title">Review the contract</span>        </label>      </li>      <li className="checklist-item">        <label className="checklist-label">          <input className="checkbox" type="checkbox" /><span className="checklist-title">Call Grace at three</span>        </label>      </li>      <li className="checklist-item">        <label className="checklist-label">          <input className="checkbox" type="checkbox" /><span className="checklist-title">Book the flight</span>        </label>      </li>    </ul>  </div></div>
<div class="device" style="--device-width: 13.5rem">  <div class="device-screen bg-surface-base flex flex-col gap-sm px(14) pt(44) pb(14)">    <p class="eyebrow">Wednesday</p>    <p class="heading heading-lg">Today</p>    <ul class="checklist mt(4)">      <li class="checklist-item">        <label class="checklist-label">          <input class="checkbox" type="checkbox" checked><span class="checklist-title">Review the contract</span>        </label>      </li>      <li class="checklist-item">        <label class="checklist-label">          <input class="checkbox" type="checkbox"><span class="checklist-title">Call Grace at three</span>        </label>      </li>      <li class="checklist-item">        <label class="checklist-label">          <input class="checkbox" type="checkbox"><span class="checklist-title">Book the flight</span>        </label>      </li>    </ul>  </div></div>
<template>  <div class="device" style="--device-width: 13.5rem">    <div class="device-screen bg-surface-base flex flex-col gap-sm px(14) pt(44) pb(14)">      <p class="eyebrow">Wednesday</p>      <p class="heading heading-lg">Today</p>      <ul class="checklist mt(4)">        <li class="checklist-item">          <label class="checklist-label">            <input class="checkbox" type="checkbox" checked><span class="checklist-title">Review the contract</span>          </label>        </li>        <li class="checklist-item">          <label class="checklist-label">            <input class="checkbox" type="checkbox"><span class="checklist-title">Call Grace at three</span>          </label>        </li>        <li class="checklist-item">          <label class="checklist-label">            <input class="checkbox" type="checkbox"><span class="checklist-title">Book the flight</span>          </label>        </li>      </ul>    </div>  </div></template>

#尺寸

机身宽 280px,宽高比为 9 比 19.5。调整尺寸时请设置 --device-width,不要直接设置宽度。圆角半径和边框宽度分别取该属性值的 14% 和 3%,随尺寸一起缩放,让手机始终保持原有形状。用 --device-ratio 调整宽高比,可呈现老款手机或小型平板。侧键向机身外凸出 2px,元素两侧各预留 2px 容纳侧键,因此将设备放在滚动区域边缘时也不会扩大滚动范围。

尺寸

<div class="device" style="--device-width: 7rem">  <img class="device-screen" src="/demo/moonstone.svg" alt></div><div class="device" style="--device-width: 9rem">  <img class="device-screen" src="/demo/moonstone.svg" alt></div><div class="device" style="--device-width: 11rem; --device-ratio: 3 / 4">  <img class="device-screen" src="/demo/moonstone.svg" alt></div>
<>  <div className="device" style={{ '--device-width': '7rem' } as React.CSSProperties}>    <img className="device-screen" src="/demo/moonstone.svg" alt="" />  </div>  <div className="device" style={{ '--device-width': '9rem' } as React.CSSProperties}>    <img className="device-screen" src="/demo/moonstone.svg" alt="" />  </div>  <div className="device" style={{ '--device-width': '11rem', '--device-ratio': '3 / 4' } as React.CSSProperties}>    <img className="device-screen" src="/demo/moonstone.svg" alt="" />  </div></>
<div class="device" style="--device-width: 7rem">  <img class="device-screen" src="/demo/moonstone.svg" alt></div><div class="device" style="--device-width: 9rem">  <img class="device-screen" src="/demo/moonstone.svg" alt></div><div class="device" style="--device-width: 11rem; --device-ratio: 3 / 4">  <img class="device-screen" src="/demo/moonstone.svg" alt></div>
<template>  <div class="device" style="--device-width: 7rem">    <img class="device-screen" src="/demo/moonstone.svg" alt>  </div>  <div class="device" style="--device-width: 9rem">    <img class="device-screen" src="/demo/moonstone.svg" alt>  </div>  <div class="device" style="--device-width: 11rem; --device-ratio: 3 / 4">    <img class="device-screen" src="/demo/moonstone.svg" alt>  </div></template>

#无障碍

  • 外框仅作装饰,辅助技术会跳过它。屏幕承载实际信息:为图像设置说明界面所展示内容的 alt。如果旁边的文字已经说明这些内容,则使用空的 alt。
  • 屏幕内可运行的演示是一个真正的小型页面。确保操作目标的尺寸适合触控,并让演示支持键盘操作,或让键盘焦点跳过演示。
  • 在强制颜色模式下,机身和摄像头凹槽使用系统颜色绘制轮廓。
  • 屏幕中自动播放的录像必须静音。如果录像通过声音或操作步骤传达内容,还需要字幕或文字说明。

#类名

类名说明
device机身是宽 280px、比例为 9 比 19.5 的凸起面板,带窄边框、摄像头凹槽和侧键。侧键从机身凸出 2px,仍位于元素自身的盒内,因此绘制范围不会超出该盒。
device-screen将此类用于图像、视频、嵌入式框架或一段标记。屏幕填满机身边框内的区域,圆角与机身圆角同心。

#自定义属性

属性默认值说明
--device-width17.5rem机身宽度,圆角和边框均以此为基准。
--device-ratio9 / 19.5机身的宽高比
--device-radiuscalc(var(--device-width) * .14)机身的圆角半径
--device-bezelcalc(var(--device-width) * .03)屏幕边框的宽度

#源码

@master/ui/components/device.css
/* * Device: the body of a phone around a screenshot, a video or a running demo. * It is drawn in one material, like a model cast in clay: a lifted sheet for the body, a narrow bezel, and * a small well at the top of the screen where the camera would be. The frame says "this is a phone" and * leaves every color to what it shows, as a window does. * Everything is measured from the body's width: the corner is 14% of it, the bezel 3% and the screen's own * corner is the body's minus the bezel, so the gap keeps one width around the bend. The body is 280px wide * and 9 by 19.5; set --device-width to resize it, not a width, so the corners follow. * The side buttons stand 2px proud of the body, and the element is that much wider than the body on each * side to hold them: nothing is drawn outside its box, so a device at the edge of a scroller adds no scroll. */@layer components.ui {  .device {    --device-width: 17.5rem;    --device-ratio: 9 / 19.5;    --device-radius: calc(var(--device-width) * .14);    --device-bezel: calc(var(--device-width) * .03);    --device-button: 2px;    --surface-color: var(--color-surface-raised);    position: relative;    display: block;    flex: none;    box-sizing: border-box;    /* The body and the camera's well are layers of the device, and no further back or forward than that. */    isolation: isolate;    width: calc(var(--device-width) + var(--device-button) * 2);    height: calc(var(--device-width) / (var(--device-ratio)));    margin: 0;    padding: var(--device-bezel) calc(var(--device-bezel) + var(--device-button));    /* The buttons: three on one side and one on the other, as thin slips the body covers all but 2px of. */    background:      linear-gradient(var(--color-line-strong) 0 0) 0 17% / 50% 4% no-repeat,      linear-gradient(var(--color-line-strong) 0 0) 0 26% / 50% 8% no-repeat,      linear-gradient(var(--color-line-strong) 0 0) 0 37% / 50% 8% no-repeat,      linear-gradient(var(--color-line-strong) 0 0) 100% 31% / 50% 13% no-repeat;    /* The body: a lifted sheet behind the screen. A hairline inside its edge turns the sheet into a frame with a rim. */    &::before {      content: '';      position: absolute;      z-index: -1;      inset: 0 var(--device-button);      border-radius: calc(var(--device-radius) * var(--corner-scale));      corner-shape: var(--corner-shape);      @apply --surface-sheet;      box-shadow: inset 0 0 0 1px var(--color-line-hair), var(--shadow-lifted);      pointer-events: none;    }    /* The camera's well: a pill a third of the screen wide, a little below its top edge. */    &::after {      content: '';      position: absolute;      z-index: 1;      top: calc(var(--device-bezel) + var(--device-width) * .03);      left: 50%;      width: calc(var(--device-width) * .3);      aspect-ratio: 3.4;      border-radius: 999px;      corner-shape: round;      background: var(--color-surface-raised-low);      box-shadow: var(--shadow-pressed);      translate: -50% 0;      pointer-events: none;    }    @media (forced-colors: active) {      background: none;      &::before, &::after {        outline: 1px solid CanvasText;        outline-offset: -1px;      }    }  }  /* The screen: a picture, a video, a frame or a block of markup, finished by a corner concentric with the body's. */  .device-screen {    display: block;    box-sizing: border-box;    width: 100%;    height: 100%;    margin: 0;    border: 0;    border-radius: calc(var(--device-radius) * var(--corner-scale) - var(--device-bezel));    corner-shape: var(--corner-shape);    overflow: clip;    background: var(--color-surface-inset);    outline: 1px solid var(--color-line-image-strong);    outline-offset: -1px;    object-fit: cover;    @media (forced-colors: active) {      outline-color: CanvasText;    }  }}