Avatar
A component for displaying avatar images. When no image is available, it can show the first letter of a name as a placeholder.
Overview
<Avatar size="4rem" src="https://cdn.lism-css.com/img/avatar/EkABPzB34XR7.webp" alt="Avatar of xxxx" /><div class="b--avatar l--frame -w" style="--w: 4rem"> <img src="https://cdn.lism-css.com/img/avatar/EkABPzB34XR7.webp" alt="Avatar of xxxx" width="100%" height="100%" decoding="async" /></div>Styles
The Avatar base styles are defined in the following CSS.
@layer lism-block {
/* src ありの通常時: l--frame との併用が前提 */
.b--avatar {
--w: 2em; /* size prop未指定時のデフォルトサイズ */
width: var(--w);
aspect-ratio: 1/1;
border-radius: var(--bdrs--99);
}
/* src 未指定時: name のイニシャル1文字を表示する。l--center との併用が前提 */
.b--avatar--initial {
--lh: 1;
background-color: var(--base-2);
text-transform: uppercase;
user-select: none;
/* --w が em単位の時を考慮し、子要素側で計算する */
> * {
font-size: calc(var(--w) / 2);
}
}
} The full source code is available on GitHub.
Usage
Provided as Avatar (b--avatar) from the @lism-css/ui package.
Import
import { Avatar } from '@lism-css/ui/react'; import { Avatar } from '@lism-css/ui/astro'; <link href="https://cdn.jsdelivr.net/npm/@lism-css/ui@0.30.0/dist/style.css" rel="stylesheet" /> Props
| Prop | Description |
|---|---|
size |
Specifies the size of the avatar. Default: "2em" |
src |
Path to the avatar image. When omitted, the initial of name is shown instead. |
name |
User name. When src is omitted, its first character is shown as the initial. Also used as the alt text when alt is not specified. |
alt |
Alt text. Takes precedence over name when specified. Use alt="" to treat the avatar as decorative, e.g. when the name is displayed right next to it. |
Examples
Avatar examples<Avatar size="4rem" src="https://cdn.lism-css.com/img/avatar/EkABPzB34XR7.webp" alt="Avatar of xxxx"/><Avatar size="5rem" src="https://cdn.lism-css.com/img/avatar/of5v8rPFMI8F.webp" alt="Avatar of xxxx" bd bdw="2px" bdc="base" bxsh="20"/><div class="b--avatar l--frame -w" style="--w: 4rem"> <img src="https://cdn.lism-css.com/img/avatar/EkABPzB34XR7.webp" alt="Avatar of xxxx" width="100%" height="100%" decoding="async" /></div><div class="b--avatar l--frame -w -bd -bxsh:20" style="--w: 5rem; --bdw: 2px; --bdc: var(--base)"> <img src="https://cdn.lism-css.com/img/avatar/of5v8rPFMI8F.webp" alt="Avatar of xxxx" width="100%" height="100%" decoding="async" /></div>Initial fallback
When src is omitted, the root switches from l--frame to l--center, gets b--avatar--initial, and renders the first character of name in the center. Uppercasing is handled by CSS (text-transform: uppercase), and the font size scales with the avatar size (--w).
The styles are applied to b--avatar--initial on the root element, so the background color can be overridden with props such as bgc.
src<Avatar size="2rem" name="Yamada Taro" /><Avatar size="3rem" name="鈴木" bgc="brand" c="base" /><div class="b--avatar b--avatar--initial l--center -w" style="--w: 2rem"> <span role="img" aria-label="Yamada Taro">Y</span></div><div class="b--avatar b--avatar--initial l--center -w -bgc:brand -c:base" style="--w: 3rem"> <span role="img" aria-label="鈴木">鈴</span></div>There is no automatic switch to the initial when the image fails to load. Since the Astro version renders static HTML, client-side load-state tracking is intentionally not included.
Without @lism-css/ui
This example builds an Avatar with lism-css primitives only, without @lism-css/ui.
<Frame ar="1/1" w="4em" bdrs="99"> <img src="https://cdn.lism-css.com/img/avatar/EkABPzB34XR7.webp" alt="Avatar of xxxx" width="100%" height="100%" decoding="async" /></Frame><div class="l--frame -ar:1/1 -w -bdrs:99" style="--w: 4em"> <img src="https://cdn.lism-css.com/img/avatar/EkABPzB34XR7.webp" alt="Avatar of xxxx" width="100%" height="100%" decoding="async" /></div>