Utilities
Flex
The Flex
utility sets how flex items grow or shrink to fit the available space in their flex container. It's essential for creating flexible and responsive layouts.
Utility Class | USS Properties |
---|---|
flex-1 | flex: 1 1 0%; |
flex-auto | flex: 1 1 auto; |
flex-initial | flex: 0 1 auto; |
flex-none | flex: none; |
Basic Usage in UXML
Flex 1
This makes the element flexible. It will grow to fill any extra space in the container, or shrink if there isn't enough space.
<ui:VisualElement class="flex-1"></ui:VisualElement>
Flex Auto
The item sizes itself based on its content but also grows to fill extra space. It's equivalent to flex: 1 1 auto
.
<ui:VisualElement class="flex-auto"></ui:VisualElement>
Flex Initial
This sets the element's size based on its content, but it won't grow to fill extra space. It can shrink if needed, making sure it doesn't take more space than it should.
<ui:VisualElement class="flex-initial"></ui:VisualElement>
Flex None
This one is the most rigid. The element won't grow or shrink at all. It will maintain its size no matter how much space is available in the container.
<ui:VisualElement class="flex-none"></ui:VisualElement>
Customize
Modifier Variations
You can customize which modifier variations of the Flex
utility are generated, such as pseudo-classes (e.g., hover
, focus
) and custom modifiers.
Flex
utility.By fine-tuning the utility variations, you maintain control over your stylesheet's size and complexity, ensuring that only necessary styles are included. To update these values:
- Open the Theme Config Asset: Find the asset within your Unity project.
- Navigate to the 'Utilities' Section: Locate the
Flex
utility. - Adjust Modifier Variations: Select which modifiers you want to be generated for the utility. Keep in mind that the order of the modifiers will affect the order and specificity of the generated USS classes.
Class Tags and Properties
The Flex
utility generates classes based on predefined tags and their associated USS properties. While we recommend keeping these defaults for consistency and ease of reference in our documentation, you have the option to customize them to suit your project's specific needs.
These values are prepended to the defined values in this format tag-{value}
. If a tag is empty, the generated class will simply be the value by itself.
Here are the default tags and the USS properties they set for the Flex
utility:
- flex : flex
To customize these tags and properties, you can edit the Tag Property Map
field for the Flex
utility in the Theme Config asset. This allows you to define new tags or modify existing ones, thus tailoring the generated classes to your preferences.
Extending Core Fields
The Flex
utility does not extend any fields from the Core
section of the Theme Config asset. Before you try to extend any fields, we recommend checking Unity's documentation to verify which types of values are expected by the relevant USS properties.
To customize or define these extended fields:
- Open the Theme Config Asset: Locate the asset within your Unity project.
- Navigate to the 'Utilities' Section: Find the
Flex
utility. - Customize Extend Fields: Modify or add fields in the
Extend Fields
array to change the core configuration values that are extended. You'll only be able to extend fields of the same type.
Disable Utility
The Flex
utility is enabled by default. You can disable it by unchecking the Enabled
option in the Theme Config asset. This will prevent the generation of the utility's styles.