UI Toolkit includes a layout engine that positions elements based on layout and styling properties. The layout engine uses the layout principles of Yoga, which implements a subset of Flexbox, a HTML/CSS layout system. UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary Toolkit properties match Yoga layout behavior. UI Toolkit supports most properties in Flexbox.
By default, all elements are part of the layout. The layout has the following default behaviours:
VisualElement
with text uses the text size in its size calculations. This behavior can be limited by other layout properties.UI Toolkit includes built-in controls for standard UI components, such as a button, toggle, or text field. These built-in controls have styles that affect their layout.
The following are the main style properties:
flex-direction
in USS): Defines the layout direction the main-axis. The default is column
and that makes the main-axis run from top to bottom.flex-grow
in USS): Defines how an element should grow in the main-axis. It’s a ratio that’s shared with all other siblings of the same parent. This property is useful when trying make an element stretch to take up the entire size of its parents (minus any siblings). To do this, set the Flex > Grow value to 1
. If you have two siblings with flex-grow
set to 1
, they will each take 50% of the parent’s available size along the main-axis.align-items
in USS): Defines the alignment of elements in the cross-axis, or the perpendicular axis to the main-axis. For example, if you have two Buttons in a VisualElement
that has flex-direction: column
and align-items: flex-end
set, the two Buttons will squish against the container’s right edge. The options for align-items
have names like flex-start
and flex-end
because they depend on the value of flex-direction
.justify-content
in USS): Defines the alignment of elements in the main-axis. For example, if you have two Buttons in a VisualElement
that has flex-direction: column
and justify-content: flex-end
set, the two Buttons squish against the container’s bottom edge. The options for justify-content
are named like flex-start
and flex-end
because they depend on the value of flex-direction
.For more information, refer to Flex layout.
If the selected element has child elements, in UI Builder, you can use toggles in the header to toggle flex-related style properties in the ViewportThe user’s visible area of an app on their screen.
See in Glossary. The image below shows the options available for the #menu
element:
You can use the style properties to create complex, dynamic layouts. Here’s an example of a layout that anchors a Button
on the bottom-right edge of the screen:
The UXML for this layout, showing the inline styles set on each container VisualElement
, is below:
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements"> <ui:VisualElement name="screen-is-blue" style="flex-grow: 1; justify-content: flex-end; background-color: blue;"> <ui:VisualElement name="toolbar-is-orange" style="align-items: flex-end; background-color: orange;"> <ui:Button text="Button" display-tooltip-when-elided="true" /> </ui:VisualElement> </ui:VisualElement> </ui:UXML>
The containers are colored to reveal their shape. You can use nested VisualElement
containers to achieve any dynamic layout without resorting to explicitly setting the position and size of each element. This keeps the layout dynamic and automatically adjusts to the larger container changing size, like the screen changing size.
UI Builder also supports the Position style properties. The Position > Absolute mode makes an element invisible to the default Flexbox-based layout engine. It’s as if it no longer takes any space. Absolute-position elements appear on top of any siblings that are Relative-position.
While using Absolute position, you can use the Position > Left, Top, Right, Bottom style properties to offset and size the element from the respective edges of its parent. This doesn’t set absolute coordinates on the screen but sets offsets relative to the parent element. You can still use Relative mode to position the parent element. If you set both a Left offset and a Right offset, the Width style property of the element is ignored and the computed width will now come from the following formula:
element-computed-width = parent-width - left-offset - right-offset
Left, Top, Right, Bottom can also be interpreted as anchors. For example, you can anchor a Button
on the bottom-left of the screen:
The UXML below displays the inline styles:
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False"> <ui:VisualElement style="flex-grow: 1;"> <ui:VisualElement style="position: absolute; left: 0; bottom: 0;" /> </ui:VisualElement> </ui:UXML>
With positioning, there is a difference between having Left set to 0
and having Left unset:
Left set to 0
means set an offset of 0.Left unset
doesn’t set any offset and lets other style properties define the width or height of the element.You can also modify these offset style properties directly in the Canvas via additional resize handles on each edge and corner of the element’s blue selection border. It’s important to differentiate between what’s set and what’s unset, so the Canvas also includes “anchor” toggles as orange squares off each side of the element. The Canvas handles will adjust which style properties are set when resizing the element visually, depending on which “anchors” are set. For example, say you are resizing the element in the Canvas via its right-border handle:
Avoid to use Absolute position mode because it bypasses the built-in layout engine in UI Toolkit and can lead to high-maintenance UI where changing the overall layout requires manually updating individual elements. However, Absolute positioning is appropriate for specific use cases, such as overlays. It’s useful to overlay some complex UI on top of other complex UI, such as a popup or a dropdown. In these cases, use Absolute position for the overlay container, but continue using Relative mode for elements inside the overlay to maintain flexibility.
The following shows an example of a simple centered popup:
And here’s the UXML for your reference:
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements"> <ui:VisualElement name="complex-ui-screen"> <ui:Toggle label="Toggle" /> <ui:MinMaxSlider picking-mode="Ignore" label="Min/Max Slider" min-value="10" max-value="12" low-limit="-10" high-limit="40" /> <ui:Label text="Label" /> <ui:Button text="Button" /> <ui:Button text="Button" /> </ui:VisualElement> <ui:VisualElement name="overlay" style="position: absolute; left: 0; top: 0; right: 0; bottom: 0; background-color: rgba(0, 0, 0, 0.71); align-items: center; justify-content: center;"> <ui:VisualElement name="popup" style="background-color: rgba(70, 70, 70, 255);"> <ui:Label text="Exit?" name="message" /> <ui:Button text="OK" name="ok-button" style="width: 108.3333px;" /> </ui:VisualElement> </ui:VisualElement> </ui:UXML>
The example above highlights the use of Absolute position. Set all Position > Left, Top, Right, Bottom to 0
, so the position is 0
pixels away from the edges of the screen. This makes the #overlay
element overlap the #complex-ui-screen
container element. You can also set a semi-transparent background to the #overlay
element to make the other UI appear darkened. Use #overlay
to set Relative position to center our #popup
container VisualElement
.
The following list provides tips to help improve the performance of the layout engine:
Set the width
and height
to define the size of an element.
Use the flexGrow
property (USS: flex-grow: <value>;
) to assign a flexible size to an element. The value of the flexGrow
property assigns a base weight to the size of an element when it’s determined by its siblings.
Set the flexDirection
property to row
(USS: flex-direction: row;
) to switch to a horizontal layout.
Use relative positioning to offset an element based on its original layout position.
Set the position
property to absolute
to place an element relative to its parent position rectangle. This doesn’t affect the layout of its siblings or parent.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.