Width Units
Introduction
The Width Units variable controls how a unit is horizontally sized, which may be relative to its parent. By default elements uses Absolute width, where each unit represents 1 pixel of width in absolute terms. When using Absolute, an object ignores its parents' effective width.
Absolute
The following shows a child ColoredRectangle with 50 Absolute Width:

Relative to Parent
The following image shows a child ColoredRectangle with -10 Relative to Parent Width, so it sizes itself 10 pixels less wide than its parent.

If an instance does not have a parent, then it uses the canvas size when using a Width Units of Relative to Parent.

Relative to Parent with no direct parentPercentage of Parent
The following shows a child ColoredRectangle with 100 Percentage of Parent Width, which means it has 100% of the effective width of its parent. Note that 100 Percentage of Parent is the same as 0 Relative to Parent:

If an object does not have a parent, then the width of the canvas is used.

Ratio of Parent
Ratio of Parent can be used to fill available space or to share available space with other objects using a ratio. It behaves similar to a Height Units of Ratio of Parent, but operates horizontally rather than vertically. For a more detailed discussion of Ratio of Parent, see the Height Units page.

Ratio of Parent is usually used with a parent that has its Children Layout set to Left to Right Stack or Top to Bottom Stack. For more information, see the Children Layout page.
Relative to Children
The following image shows a child ColoredRectangle with 50 Relative to Children Width, which means that it is sized 50 pixels wider than is necessary to contain its children. Since the rectangle has no children, this is the same as having 50 Absolute Width:

Relative to Children can be used to size an object based on the position and sizes of a parent's children. The following animation shows a container with 0 Relative to Children Width, which means that its effective width is set just large enough to contain its children. Notice that if the children are moved, the parent's effective width adjusts. Both children are considered so the container adjusts its width according to the right-most side of either child:

Width Units of Relative to ChildrenA non-zero Width when using Relative to Children can be used to add additional padding to a parent container. The following animation shows how changing the Width variable can adjust the absolute width relative to children:

Relative to ChildrenRelative to Children results in effective width dynamically adjusting in response to changes on a container's children. The following animation shows a container with Children Layout of Left to Right Stack. Adding additional children expands the container automatically:

Ignored Width Values
A parent container can ignore its children when it determines its absolute width when using a Width Units of Relative to Children if any of the following are true:
The child's
Ignored By Parent Sizeis true.The child's width depends on its parent's width. This circular dependency is resolved by the parent ignoring this child. A child using
Percentage of Heightis ignored only when counting it would create a loop.The child is explicitly positioned outside of the parent's bounds
The child's
X UnitsisPercentage of Parent Width
Child's Ignored By Parent Size is True (1)
If a child has its Ignored By Parent Size set to true, then the parent ignores this child when calculating its own size. For more information, see the Ignored By Parent Size page.
Child Width Depends on its Parent's Width (2)
If a child's width depends on the parent, then the child is ignored by the parent. Once the parent has determined its own width, then the child is sized according to the parent. This type of circular dependency is common when adding background visuals to a container.
For example consider a container with two children - BlueRectangle and YellowRectangle - with the following variables:
BlueRectangle
X=Pixels from LeftBlueRectangle
Width Units=AbsoluteYellowRectangle
Width Units=Relative to Parent
Only YellowRectangle depends on its parent.
Since BlueRectangle's absolute width value does not depend on the parent, the parent can use BlueRectangle's absolute width when calculating its own absolute width. Since YellowRectangle depends on the parent, the parent ignores the YellowRectangle. Instead, YellowRectangle depends on the parent container's absolute width for calculating its own absolute width. This in effect creates a situation where BlueRectangle affects the width of both its parent and also its YellowRectangle sibling.

Child Follows its Other Dimension
A child with a Width Units of Percentage of Height has a width that depends on its own height. If that height depends on the parent, the parent still counts the child's width, as long as the parent's size does not depend on that width in return. The parent ignores the child only when counting it would create a loop.
For example, consider a container with a Width Units and Height Units of Relative to Children, and two children:
BlueRectangle has an
AbsoluteWidthof 10 andHeightof 50.YellowRectangle has a
Height UnitsofPercentage of Parentwith aHeightof 100, and aWidth UnitsofPercentage of Heightwith aWidthof 200.
Gum sizes the container in these steps:
YellowRectangle's height depends on the container's height, so the container ignores it for height. The container is 50 tall, from BlueRectangle.
YellowRectangle is 100% of 50, so it is 50 tall.
YellowRectangle's width is 200% of its height, so it is 100 wide.
Nothing in steps 1 to 3 depends on the container's width, so there is no loop. The container counts YellowRectangle's width and is 100 wide.
Now change the container's Height Units to Percentage of Width with a Height of 200, and leave its Width Units as Relative to Children. The container's height follows its width, YellowRectangle's height follows the container's height, YellowRectangle's width follows its height, and the container's width follows YellowRectangle's width. No size satisfies all four, so the container ignores YellowRectangle's width.
The same rule applies to a Height Units of Percentage of Width. See Height Units.
Child is Explicitly Positioned Outside of Parent's Bounds (3)
A parent does not consider a child if the child is explicitly positioned outside of the parent's bounds. This can happen if the child's X Units and X values result in the child being drawn outside of the parent's bounds
If a child has X Units of Pixels from Left and its X value pushes the child out of the left of the parent, then the portion that is outside of the left of the parent is ignored. The BlueRectangle in the following image has an absolute width of 50. Its X value is -20, so only 30 pixels are used to determine the parent's effective height.

Similarly, if a child uses an X Units of Pixels from Right then the parent does not consider the width of any portion which is outside of its bounds. The following animation shows RedRectangle placed outside of the right of the container's bounds with a X Units of Pixels from Right.

Notice that if RedRectangle is moved so that it is inside the bounds, it can affect the absolute width of the parent. As RedRectangle is moved into the bounds, the parent grows to accommodate the desired RedRectangle X value.

Pixels from Right can make the parent grow to accommodate the child's X valueChild's X Units is Percentage of Parent Width (4)
A parent ignores its child if the child uses an X Units of Percentage of Parent Width because this also creates a circular dependency (parent width depends on child position, child position depends on parent width).

X Units of Percentage of Parent Width result in the child ignoredCentered Children
A child with an X Units of Pixels from Center grows its parent equally to the left and right of the parent's center, so the child stays centered. The parent's width is twice the distance from its center to the child's farthest edge.
For example, a child with an Absolute Width of 100, an X Origin of Center, and an X of 0 makes the parent 100 pixels wide. Changing the child's X to 20 makes the parent 140 pixels wide, because the child's right edge is now 70 pixels from the center.
Relative to Children and Auto Grid Horizontal
If a parent sets its Width Units to Relative to Children, then it resizes itself to contain its children. Normally the width of the entire parent is determined by the child which needs the most space horizontally. If the parent uses an Auto Grid Horizontal layout, then the children control the size of the cells rather than the entire parent. Since all cells must be the same size, the child which needs the most amount of space horizontally determines the width of all cells.
For example, the following image shows a four by four grid, each containing one white rectangle. The first rectangle has an Absolute Width and Height of 100, so each cell is sized to be 100x100. Note that the other rectangles are 50x50.

Relative to Children widthThe largest child determines the cell size for all other children. Therefore, if a child is moved or resized so it outgrows its cell, then the parent width adjusts in response.

The width of a container is determined by the width of the largest cell multiplied by the number of columns. For example, the following container has 3 columns and 4 rows, resulting in 12 cells. The width of the grid is based on 3 columns multiplied by the width of the largest cell.

If children are removed from the container, the container's width does not change - the Auto Grid Horizontal Cells value determines the number of columns when calculating width, whether the cells contain children or are empty.

Since Auto Grid Horizontal Cells acts only as a minimum and not maximum, more children can be added and the container expands to support the newly-added children.

Setting a grid's Width value to a non-zero value increases the width of the container on top of what is needed to contain its children. This additional width is distributed evenly to all cells, resulting in additional spacing between each child.
For example, the following 3x3 grid contains children with an absolute width of 50. The grid's Width Units is set to Relative To Children. Notice that as the grid's Width increases, it grows horizontally. This horizontal growth results in a larger cell size, so each child is spaced apart.

Relative to Children and Text
Setting a Text instance's Width Units to Relative to Children results in the Text object adjusting according to its text contents. In other words if a Text's Width Units is set to Relative To Children, then the words in the Text do not wrap.
For example, setting the Width Units to Relative to Children and setting the Width to 0 results in the Text object automatically adjusting its actual width according to the text it contains.

Relative to Max of Children or Parent
Relative to Max of Children or Parent sizes an element to whichever is larger — its parent's width or the width needed to contain its children. The Width value acts as padding on the children side, the same as Relative to Children. A Width of 0 means the element is exactly the larger of its parent's width or its children's bounds.
This unit is most useful when multiple siblings all use Relative to Max of Children or Parent. In this case, the sibling with the widest content drives the parent's width, which in turn sets the width of all narrower siblings. This creates a column of elements that always match each other's width regardless of their individual content.

For example, consider a vertical menu where each menu item contains a Text instance. Each menu item uses Relative to Max of Children or Parent for its Width Units. When one menu item has longer text, that item grows to fit its content. Because the wider item pushes the parent to grow, all other menu items also grow to match — keeping the menu uniform.

Without this unit, you would need to either set a fixed width on all menu items (which cannot adapt to content) or use Relative to Children on each item (which makes each item a different width based on its own text).
A parent with Width Units of Relative to Children measures such a child by its children, limited by the child's Min Width and Max Width. For example, a child with a Min Width of 80 whose children are 30 wide makes its parent 80 wide.
Percentage of Height
Percentage of Height adjusts the object's effective width so it remains proportional to the effective height value multiplied by the Width value (as a percentage). For example, if a Width value of 200 is entered, then the effective width is 200% (2x) of the height.
The following image shows a child ColoredRectangle with a Width of 200 Percentage of Height. In this image, the Height value is 50 units, so the effective width is 100 units:

Percentage of File Width
Sprites can select a Width Unit called Percentage of File Width, which sets the width of the Sprite according to the file that it is displaying. This is the default Width Unit for Sprites.
The following image shows a child Sprite with 200 Percentage of Source File Width, which means it draws two times as wide as its source image:

When using Percentage of Source File Width, the Sprite's absolute width depends on the Sprite's Texture Width property.

Texture Width adjusts its absolute height when using Percentage of File WidthFor more information, see the Sprite Texture Address page.
Maintain File Aspect Ratio Width
Sprites can select a Width Unit called Maintain File Aspect Ratio Width, which sets the effective width of the Sprite so that its aspect ratio matches its source file multiplied by the Width value. Usually Maintain File Aspect Ratio Width is used with a Width value of 100 so that the Sprite shows is source file at the correct aspect ratio.
When this value is used, a Sprite's Height can be changed resulting in its absolute width also changing.

Height when using Maintain File Aspect Ratio Width also adjusts absolute widthWhen using Maintain File Aspect Ratio Width, the Sprite's effective width depends on the Sprite's Texture Width property.

Height or Texture Width affects the Sprite's effective widthAbsolute Multiplied by Font Scale
Absolute Multiplied by Font Scale is a value which multiplies an object's Width value by the global Font Scale value. This can be used to create widths which are responsive to font scales for devices which may have variable text sizes.
The Font Scale value can be adjusted in the Editor tab to preview different global Font Scale values. When a Gum project is loaded at runtime, the runtime may apply a Font Scale value such as using the Text size from Windows.
The global Font Scale value is set to 4, so the rectangle in the following image is drawn at an effective width of 200 pixels.

Last updated
Was this helpful?

