logo

G

  • Tutorials
  • API
  • Examples
  • Plugins
  • Productsantv logo arrow
  • 6.1.28
  • Canvas
    • Introduction
    • Options
    • Built-in objects
    • Coordinate system
    • Scenegraph & Lifecycle
    • Event
    • OffscreenCanvas & Server-side Rendering
    • CustomElementRegistry
    • Frequently Asked Questions
  • Renderer
    • Introduction
    • Canvas Renderer
    • Canvaskit Renderer
    • SVG Renderer
    • WebGL Renderer
    • WebGPU Renderer
    • Custom Renderer
  • Camera
    • Introduction
    • Camera Parameters
    • Camera action
    • Camera animation
  • Event
    • Introduction
    • Event Object
    • Gesture & Drag'n'Drop
    • Frequently Asked Questions
  • Animation
    • Web Animations API
    • Lottie
  • Basic Shapes
    • Basic Concepts
    • DisplayObject
    • Group
    • Text
    • Circle
    • Ellipse
    • Rect
    • Image
    • Line
    • Polygon
    • Polyline
    • Path
    • HTML
  • Style System
    • Introduction
    • CSS Typed OM
    • Inheritance
    • CSS Properties & Values API
    • CSS Layout API
    • Pattern
    • Gradient
  • 3D
    • 材质
    • 几何
    • Mesh
    • 光源
    • 雾
    • 交互
  • Built-in Objects
    • EventTarget
    • Node
    • Element
    • Document
    • MutationObserver
    • Utils
  • GPGPU
    • Introduction
    • Programming Model
    • Kernel API
    • Principles of classical GPGPU implementation
    • webgpu-graph
  • Declarative programming
    • 使用 Web Components
  • Devtools
    • G 开发者工具
    • 内置的渲染统计信息
    • 第三方开发调试工具

CSS Layout API

Previous
CSS Properties & Values API
Next
Pattern

Resource

Ant Design
Galacea Effects
Umi-React Application Framework
Dumi-Component doc generator
ahooks-React Hooks Library
WeaveFox-AI Coding Assistant

Community

Ant Financial Experience Tech
seeconfSEE Conf-Experience Tech Conference
weavefoxWeaveFox-AI Developer Community

Help

GitHub
StackOverflow

more productsMore Productions

Ant DesignAnt Design-Enterprise UI design language
yuqueYuque-Knowledge creation and Sharing tool
EggEgg-Enterprise-class Node development framework
kitchenKitchen-Sketch Tool set
GalaceanGalacean-Interactive solution
weavefoxWeaveFox-AI Coding Assistant
© Copyright 2025 Ant Group Co., Ltd..备案号:京ICP备15032932号-38

Loading...

基于样式系统,我们可以提供常用的布局算法,让用户避免相同场景的重复计算,主要参考 CSS Layout API 。

https://drafts.css-houdini.org/css-layout-api

https://github.com/w3c/css-houdini-drafts/blob/main/css-layout-api/EXPLAINER.md

布局算法以 @antv/g-layout-xxx 形式命名,用户在运行时:

  1. 选择自己需要的布局算法,通过样式系统提供的 CSS.registerLayout API 完成注册
  2. 容器元素通过 display 属性使用,容器内元素不再需要使用 x/y/z 属性或者 等 API 手动定位。定位工作由容器选择的布局算法承担

使用方式如下:

import { CSS, Group, Circle } from '@antv/g';
import { Layout as BlockFlowLayout } from '@antv/g-layout-blockflow';
import { Layout as FlexLayout } from '@antv/g-layout-flex';
import { Layout as MasonryLayout } from '@antv/g-layout-masonry';
import { Layout as GridLayout } from '@antv/g-layout-grid';
// 注册布局算法
CSS.registerLayout('block', BlockFlowLayout);
CSS.registerLayout('flex', FlexLayout);
CSS.registerLayout('masonry', MasonryLayout);
CSS.registerLayout('grid', GridLayout);
// 使用 BlockFlow 布局
const blockGroup = new Group({
style: {
display: 'block', // 通过 display 属性使用
width: '400px',
height: '400px',
},
});
// 容器内元素无需手动定位,由容器负责
blockGroup.appendChild(
new Circle({
style: {
r: 100,
},
}),
);
canvas.appendChild(blockGroup);