USS supports the following simple selectors:
name
attribute.You can combine simple selectors into complex selectors, or append pseudo-classes to them to target elements in specific states.
This page describes each type of simple selector and provides syntax and examples. It uses the following UXML document to demonstrate how simple selectors match elements.
<UXML xmlns="UnityEngine.UIElements">
<VisualElement name="container1">
<VisualElement name="container2" class="yellow">
<Button name="OK" class="yellow" text="OK" />
<Button name="Cancel"class="" text="Cancel" />
</VisualElement>
</VisualElement>
</UXML>
With no styles applied, the UXML document produces the UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary shown below.
Note: |
---|
Example images include margins and thin blue borders to help identify individual elements for the purposes of demonstration. |
C# Type selector match elements based on their C# type.
Note: |
---|
USS Type selectors are analogous to CSS Type selectors that match HTML tags. For example Button {...} in USS matches any elements of C# type Button in the same way that p {...} in CSS matches any paragraph (<p> ) tag. |
Syntax:
A C# type selector is the C# type name, written as is.
TypeName { ... }
When you write C# Type selectors, specify only the concrete object type. Don’t include the namespace in the type name.
For example, this selector is valid:
Button { ... }
This selector is not valid:
UnityEngine.UIElements.Button { ... }
Example:
For the example UXML document above, the following style rule matches the two Button
elements.
Button {
border-radius: 8px;
width: 100px;
}
Name selectors match elements based on the value of assigned name
attributes.
VisualElement.name
.<VisualElement name="my-nameName">
Unity does not enforce any convention for names, but you should make element names unique within a panel. Using non-unique names could lead to unexpected matches.
Note: |
---|
USS Name selectors are analogous to CSS ID selectors that match elements with a specific id attribute. |
Syntax:
A name selector consists of an element’s assigned name prefixed with a number sign (#
).
#name { ... }
Note: |
---|
Only use the number sign # when you write the selector in a USS file. Don’t use it when you assign the name to an element in a UXML or C# file. An element name that includes the number sign is invalid. For example <Button name="OK" /> is valid. <Button name="#OK" /> is not. |
Example:
For the example UXML document above, the following style rule matches the second Button
element.
#Cancel {
border-width: 2px;
border-color: DarkRed;
background-color: pink;
}
Class selectors match elements that have specific USS classes assigned.
Note: |
---|
Class selectors work the same way in USS as they do in CSS. |
Syntax:
A class selector consists of the class name prefixed with a period (.
). Class names cannot begin with a numeral.
.class { ... }
Note: |
---|
The period . is only used when you write the selector in a USS file. Don’t include it when you assign the class to an element in a UXML or C# file. |
For example, use <Button class="yellow" /> and not <Button class=".yellow" /> .Also avoid using periods in class names. Unity’s USS parser interprets a period as the beginning of a new class. For example, if you create a class called yellow.button , and create the following USS rule: .yellow.button{...} the parser interprets the selector as a multiple selector, and tries to find elements that match both a .yellow class and a .button class. |
When an element has more than one class assigned, a selector only has to match one of them to match the element.
You can also specify multiple classes in a selector, in which case an element must have all of those classes assigned in order to match. See Multiple selectors for details.
Example:
For the example UXML document above, the following style rule matches the element named container2 and the button element named OK, and changes their background color to yellow.
.yellow {
background-color: yellow;
}
The universal selector, sometimes called the wildcard selector, matches any element.
Syntax:
* { ... }
Example:
For the example UXML document above, the following style rule matches every element, and changes its background color to yellow. This includes the main area of the window, because the stylesheet is applied to the window’s root element.
* {
background-color: yellow;
}
Note: |
---|
Because it tests every element, the universal selector can impact performance. Use it sparingly. Avoid using the universal selector with the descendant selector. When you use that combination, the system might have to test many elements repeatedly, which can impact performance. |
You can include the universal selector in complex selectors. For example, the following USS rule uses it in a child selector to match any buttons that are children of any elements that are children of an element with the USS class yellow
assigned to it:
.yellow > * > Button{..}
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.