擬似クラスは、セレクターのスコープを狭め、要素が特定の状態になったときにのみ一致するようにします。
擬似クラスをシンプルセレクターに付加することで、特定の状態にある特定の要素にマッチさせることができます。例えば、以下の USS 規則では、:hover
擬似クラスを使用して、ユーザーがポインターを重ねたときに Button
要素の色を変えます。
Button:hover {
background-color: palegreen;
}
下の表は、Unity がサポートする疑似クラスの一覧です。疑似クラスを拡張したり、カスタムのクラスを作成することはできません。
疑似クラス | 要素に一致する状態 |
---|---|
:hover |
カーソルを要素の上に置く |
:active |
ユーザーがエレメントと相互作用する |
:inactive |
ユーザーが要素との相互作用を停止する |
:focus |
要素にフォーカスがある |
:selected |
該当なし。Unity はこの疑似状態を使用しません。 |
:disabled |
要素を enabled == false に設定する |
:enabled |
要素を enabled == true に設定する |
:checked |
要素はトグル要素であり、オンに切り替える |
:root |
要素はルート要素 (ビジュアルツリーの最上位要素) |
疑似クラスを連鎖させることで、複数の同時進行の状態に同じスタイルを適用することができます。例えば、次の USS ルールでは、:checked
と :hover
擬似クラスを連鎖させて、ユーザーがポインターを重ねると、チェックされた Toggle
要素の色を変更します。
Toggle:checked:hover {
background-color: yellow;
}
トグルがチェックされていても、ポインターがその上に重なっていないと、セレクターが一致しなくなります。
:root
疑似クラスは、ビジュアルツリーの最上位の要素に一致します。これは、サポートされている他の疑似クラスとは若干異なります。なぜなら、:root
疑似クラス自体を使用して、スタイルシートが影響を与える要素のデフォルトスタイルを定義するためです。
例えば、以下の USS ルールでは、デフォルトのフォントを設定します。より具体的なスタイルルールからフォントを取得していない要素は、そのフォントを使用します。
:root {
-unity-font: url("../Resources/fonts/OpenSans-Regular.ttf");
}
:root
セレクターの一般的な使い方は、“グローバル” 変数 (カスタムプロパティ) を宣言し、他のスタイルルールが特定の値の代わりに使用できるようにすることです。
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.