USS common properties
Describes the most common USS properties, their syntax and accepted values, and differences from CSS.
阅读时间10 分钟最后更新于 17 天前
This page introduces the common USS properties, their syntax and accepted values, and differences from CSS. For a complete list of USS properties, refer to USS properties reference.
All
The property resets all properties to their default value. This property doesn't apply to the custom USS properties.
allall: initial
Box model

Box model
Dimensions
width: <length> | autoheight: <length> | automin-width: <length> | automin-height: <length> | automax-width: <length> | nonemax-height: <length> | noneaspect-ratio: <ratio> | auto
The and specify the size of the element. If isn't specified, the width is based on the width of the element's contents. If isn't specified, the height is based on the height of the element's contents.
widthheightwidthheightAspect ratio is the ratio of the element's width to its height. You can specify the aspect ratio as a floating-point number (for example, for ) or set it to to maintain the original aspect ratio.
1.77777816/9autoWhen you set other sizing properties on an element, such as , , , or , the property works together with these properties. For example, if you set on an image and apply an aspect ratio of , the image’s width becomes to maintain the ratio. If you explicitly set both and , those values override the aspect ratio.
widthheightmin-widthmax-heightaspect-ratioheight: 200px3/4150pxwidthheightTo ensure your element maintains the ratio regardless of the content inside, you can set to .
overflowhiddenAspect ratio is equivalent to the CSS property. For more information, refer to MDN documentation.
aspect-ratioMargins
margin-left: <length> | auto;margin-top: <length> | automargin-right: <length> | automargin-bottom: <length> | auto/* Shorthand */margin: [<length> | auto]{1,4}
Borders
border-left-width: <length>border-top-width: <length>border-right-width: <length>border-bottom-width: <length>/* Shorthand */border-width: <length>{1,4}
Padding
padding-left: <length>padding-top: <length>padding-right: <length>padding-bottom: <length>/* Shorthand */padding: <length>{1,4}
Differences from CSS
The alternative box model that USS uses is different from the standard CSS box model. In the standard CSS box model, and define the size of the content box. An element's rendered size is the sum of its , , and / values.
widthheightpaddingborder-widthwidthheightUnity's model is equivalent to setting the CSS property to . Refer to the MDN documentation for details.
box-sizingborder-boxFlex layout
UI Toolkit includes a layout engine that positions visual elements based on layout and styling properties. The layout engine implements a subset of Flexbox, an HTML/CSS layout system.
By default, all items are vertically placed in their container.
/* Items */flex-grow: <number>flex-shrink: <number>flex-basis: <length> | autoflex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]align-self: auto | flex-start | flex-end | center | stretch/* Containers */flex-direction: row | row-reverse | column | column-reverseflex-wrap: nowrap | wrap | wrap-reversealign-content: flex-start | flex-end | center | stretch/* The default value is `stretch`.`auto` sets `align-items` to `flex-end`. */align-items: auto | flex-start | flex-end | center | stretchjustify-content: flex-start | flex-end | center | space-between | space-around
Positioning
/* The default value is `relative` which positions the element based on its parent.If sets to `absolute`, the element leaves its parent layout and values are specified based on the parent bounds.*/position: absolute | relative/* The distance from the parent edge or the original position of the element. */left: <length> | autotop: <length> | autoright: <length> | autobottom: <length> | auto
Background
background-color: <color>background-image: <resource> | <url> | none-unity-background-scale-mode: stretch-to-fill | scale-and-crop | scale-to-fit-unity-background-image-tint-color: <color>
For more information about setting background images, refer to Set background images.
Slicing
When assigning a background image, you draw it with respect to a simplified 9-slice specification:
-unity-slice-left: <integer>-unity-slice-top: <integer>-unity-slice-right: <integer>-unity-slice-bottom: <integer>-unity-slice-scale: <length>-unity-slice-type: sliced | tiled
For more information about 9-slice, refer to 9-Slice images with UI Toolkit.
Border color
border-left-color: <color>border-top-color: <color>border-right-color: <color>border-bottom-color: <color>/* Shorthand */border-color: <color>{1,4}
Border radius
border-top-left-radius: <length>border-top-right-radius: <length>border-bottom-left-radius: <length>border-bottom-right-radius: <length>/* Shorthand */border-radius: <length>{1,4}
Differences from CSS
Border radius properties work almost the same in USS and CSS. For detailed information about , refer to the MDN documentation.
border-radiusHowever, the following are the main differences:
- Unity doesn't support the second-radius shorthand () used to create elliptical corners.
border-radius: (first radius values) / (second radius values); - Unity reduces border radius values to half of the element's size in pixels. For example, for a 100px x 100px element, any value greater than 50px is reduced to 50px. If you use percentage (
border-radius) values for border-radius properties, Unity first resolves the percentage to pixels and then reduces the%value to half of the resolved pixel value. If you have different radius values for two or more corners, Unity reduces any values greater than half of the element's size to half of the element's size.border-radius - If the element's width and height aren't equal, or if clamping occurs on one axis, the border radius might be uneven, resulting in elliptical corner rounding.
Appearance
overflow: hidden | visible-unity-overflow-clip-box: padding-box | content-box-unity-paragraph-spacing: <length>visibility: visible | hiddendisplay: flex | none
The defines the clipping rectangle for the element content. The default is , the rectangle outside the padding area (the green rectangle in the box model image above); represents the value inside the padding area (the blue rectangle in the box model image above).
-unity-overflow-clip-boxpadding-boxcontent-boxThe default value is . Set to to remove the element from the UI. Set the to to hide the element, but the element still occupies space in the layout.
displayflexdisplaynonevisibilityhiddenThe property controls the clipping of an element's content. The default value is , which means the element's content isn't clipped to the element's bounds. If you set to , the element's content is clipped to the element's bounds. You can use to make a masking effect.
overflowvisibleoverflowhiddenoverflowDifferences from CSS
The USS property supports only a small subset of the CSS property's available keyword values. The USS version supports keywords that work with the Yoga layout engine.
displaydisplay- For more information about Yoga, refer to Flexible Layouts with Yoga.
- For more information about the CSS property, refer to the MDN documentation.
display
Text properties
Text properties set the color, font, font size, and Unity-specific properties for font resource, font style, alignment, word wrap, and clipping.
color: <color>-unity-font: <resource> | <url>-unity-font-definition: <resource> | <url>font-size: <number>-unity-font-style: normal | italic | bold | bold-and-italic-unity-text-align: upper-left | middle-left | lower-left | upper-center | middle-center | lower-center | upper-right | middle-right | lower-right-unity-text-overflow-position: start | middle | endwhite-space: normal | nowrap | pre | pre-wrap-unity-text-outline-width: <length>-unity-text-outline-color: <color>/* Shorthand */-unity-text-outline: <length> | <color>-unity-text-generator: standard | advanced-unity-text-auto-size: none | best-fit <min-font-size> <max-font-size>/* The text overflow mode. *//* clip: Trims any text that extends beyond the boudaries of its container. *//* ellipsis: Truncates any text that extends beyong the boudaries of its container with an ellipsis. */text-overflow: clip | ellipsistext-shadow: <x-offset> <y-offset> <blur-radius> <color>;letter-spacing: <length>word-spacing: <length>-unity-paragraph-spacing: <length>
For more information about text shadow and text outline, refer to Text effects.
For more information about auto sizing, refer to Auto sizing text elements.
Cursor
The property specifies the mouse cursor to be displayed when the mouse pointer is over an element.
cursorcursor: [ [ <resource> | <url> ] [ <integer> <integer>]? , ] [ arrow | text | resize-vertical | resize-horizontal | link | slide-arrow | resize-up-right | resize-up-left | move-arrow | rotate-arrow | scale-arrow | arrow-plus | arrow-minus | pan | orbit | zoom | fps | split-resize-up-down | split-resize-left-right ]
Differences from CSS
In CSS, you can specify multiple optional custom cursors and a mandatory keyword in a single style declaration. When you specify multiple cursors, they form a fallback chain. If the browser can't load the first custom cursor, it tries each of the others in sequence until one of them loads or there are no more custom cursors to try. If the browser can't load any custom cursors, it uses the keyword.
cursorIn USS, custom cursors and keywords are mutually exclusive. A style declaration can only have one custom cursor or one keyword.
cursorFor detailed information about the CSS property, refer to the MDN documentation.
cursorOpacity
opacity: <number>
Differences from CSS
USS opacity is similar to CSS opacity. The opacity of parent elements affects the perceived opacity of child elements. The perceivability is different between USS opacity and CSS opacity.
In the following USS example, the blue square is a child of the red square. The red square has an of .
opacity0.5.red { background-color: red; opacity: 0.5;}.blue { background-color: blue; left: 20px; top: 20px;}
Although the blue square doesn't have an opacity value, it has a perceived opacity of from the red square. You can see the red square through the blue square.
0.5
USS opacity example
In CSS, if you apply the same styles, both the red and blue squares are 50% transparent. However, you can't see the red square through the blue square, unless you also set the of blue to be less than .
opacity1
CSS opacity example
Material
-unity-material: <resource> | <url> | none
The property assigns a custom material to the element. For more information, refer to Get started with UI Shader Graph.
-unity-material