Skip to main content

📐 Yoga / Flexbox Model

PixiJS Layout is powered by Yoga, a cross-platform layout engine that implements a subset of the CSS Flexbox specification. If you're familiar with how Flexbox works in HTML/CSS, you'll find many parallels in how layout rules work here.

For example, key properties such as flexDirection, justifyContent, alignItems, gap, and flexWrap work as expected and follow the same logic as in web development.

As a general rule, PixiJS Layout uses the same property names as CSS, however, the name is converted to camelCase. For example:

PixiJS Layout propertyCSS property
marginLeft, marginRightmargin-left, margin-right
marginTop, marginBottommargin-top, margin-bottom
paddingLeft, paddingRightpadding-left, padding-right
paddingTop, paddingBottompadding-top, padding-bottom
flexGrow, flexShrinkflex-grow, flex-shrink
flexDirection, flexWrapflex-direction, flex-wrap
justifyContent, alignItemsjustify-content, align-items
alignContent, alignSelfalign-content, align-self
backgroundColor, borderRadiusbackground-color, border-radius

🧱 Everything is a Box

In PixiJS Layout, everything is conceptually a box. This reflects the CSS box model:

  • Containers represent layout groups, analogous to HTML <div> elements. They do not render visual content directly, but define layout regions that arrange and position children.

  • Leaf nodes such as Sprite, Graphics, Text, BitmapText, and TilingSprite are analogous to HTML <img> elements. These nodes are visual and content-bearing.

Each node in the tree has a virtual layout box, and the position and size of that box is calculated by the layout engine. Visual elements can additionally scale or crop their internal content using style properties like objectFit and objectPosition.

const container = new Container({
layout: {
width: 500,
height: 300,
flexWrap: 'wrap',
justifyContent: 'center',
alignContent: 'center',
},
});

const sprite = new Sprite({ texture });
sprite.layout = {
width: 100,
height: 100,
objectFit: 'contain',
objectPosition: 'center',
};

container.addChild(sprite);