--- title: CSS.registerProperty() slug: Web/API/CSS/RegisterProperty tags: - CSS - Houdini - Reference translation_of: Web/API/CSS/RegisterProperty ---
{{SeeCompatTable}}

CSS.registerProperty() メソッドは{{cssxref('--*', 'カスタムプロパティ')}}を登録し、プロパティ型のチェック、デフォルト値、およびそれらの値を継承するまたは継承しないプロパティを許可します。

カスタムプロパティを登録すると、カスタムプロパティの振る舞い(許される型は何か、カスタムプロパティがその値を継承するかどうか、カスタムプロパティのデフォルト値は何か)をブラウザーに指示できます。

構文

CSS.registerProperty(PropertyDefinition);

パラメーター

次のメンバーを含むことができる PropertyDefinition ディクショナリオブジェクトです。

name
定義しているプロパティの名前を示す DOMString
syntax {{optional_inline}}
定義されたプロパティの期待される構文を表す DOMString。 デフォルトは "*" です。
inherits
定義されたプロパティを継承する必要があるか(true)、否か(false)を定義するブール値。 デフォルトは false です。
initialValue {{optional_inline}}
定義されたプロパティの初期値を表す DOMString

戻り値

undefined

例外

InvalidModificationError
指定された name はすでに登録されています。
SyntaxError
指定された name は(--foo のように、2つのダッシュで始まる)有効なカスタムプロパティ名ではありません。
TypeError
必要なディクショナリメンバーの name または inherits、あるいはその両方が指定されていません。

次の例では、registerProperty() を使用して{{cssxref('--*', 'カスタムプロパティ')}} --my-color を色として登録し、デフォルト値を指定して、その値を継承しません。

window.CSS.registerProperty({
  name: '--my-color',
  syntax: '<color>',
  inherits: false,
  initialValue: '#c0ffee',
});

この例では、カスタムプロパティ --my-color が構文 <color> を使用して登録されています。 これで、このプロパティを使用して、ホバーまたはフォーカスでグラデーションを遷移(transition)できます。 登録されたプロパティでは遷移が機能しますが、未登録のプロパティでは機能しないことに注意してください!

.registered {
  --my-color: #c0ffee;
  background-image: linear-gradient(to right, #fff, var(--my-color));
  transition: --my-color 1s ease-in-out;
}

.registered:hover,
.registered:focus {
  --my-color: #b4d455;
}

.unregistered {
  --unregistered: #c0ffee;
  background-image: linear-gradient(to right, #fff, var(--unregistered));
  transition: --unregistered 1s ease-in-out;
}

.unregistered:hover,
.unregistered:focus {
  --unregistered: #b4d455;
}
button {
  font-size: 3vw;
}

これらのスタイルをいくつかのボタンに追加できます。

<button class="registered">Background Registered</button>
<button class="unregistered">Background Not Registered</button>

{{EmbedLiveSample("Examples", 320, 320)}}

仕様

仕様 状態 コメント
{{SpecName('CSS Properties and Values API', '#the-registerproperty-function', 'The registerProperty() function')}} {{Spec2('CSS Properties and Values API')}} 初期定義

ブラウザーの互換性

{{Compat("api.CSS.registerProperty", 1)}}

関連情報