--- title: '继承性' slug: Web/CSS/@property/inherits tags: - CSS - Reference - Web - Property - Houdini --- <div>{{CSSRef}}{{SeeCompatTable}}</div> <p><strong><code>inherits</code></strong> <a href="/en-US/docs/Web/CSS">CSS</a> 描述符在使用{{cssxref("@property")}} {{cssxref("at-rule")}}时是必需的,它控制由 <code>@property</code>声明注册的自定义属性默认情况下是否会被继承。</p> <h2 id="Syntax">Syntax(类型描述符)</h2> <pre class="brush: css no-line-numbers">@property --property-name { syntax: '<color>'; inherits: false; initial-value: #c0ffee; } @property --property-name { syntax: '<color>'; inherits: true; initial-value: #c0ffee; } </pre> <h2 id="Values">取值</h2> <dl> <dt><code>true</code></dt> <dd>属性默认继承</dd> <dt><code>false</code></dt> <dd>属性默认不继承</dd> </dl> <h2 id="Formal_definition">规范定义</h2> <p>{{cssinfo}}</p> <h2 id="Formal_syntax">标准语法</h2> <pre class="brush: css">{{csssyntax}}</pre> <h2 id="Examples">实例</h2> <p>为 <code>--my-color</code> {{cssxref('--*', '自定义属性')}}添加颜色值类型检测、设置默认值并且设置属性值不允许被继承。</p> <p>使用 <a href="/en-US/docs/Web/CSS">CSS</a> {{cssxref('@property')}} <a href="/en-US/docs/Web/CSS/At-rule">@规则</a>:</p> <pre class="brush: css">@property --my-color { syntax: '<color>'; inherits: false; initial-value: #c0ffee; } </pre> <p>使用 <a href="/en-US/docs/Web/JavaScript">JavaScript中的</a> {{domxref('CSS.registerProperty')}}函数:</p> <pre class="brush: js">window.CSS.registerProperty({ name: '--my-color', syntax: '<color>', inherits: false, initialValue: '#c0ffee', }); </pre> <h2 id="Specifications">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">规范</th> <th scope="col">状态</th> <th scope="col">提议</th> </tr> <tr> <td>{{SpecName('CSS Properties and Values API', '#inherits-descriptor', 'inherits')}}</td> <td>{{Spec2('CSS Properties and Values API')}}</td> <td>初始定义.</td> </tr> </tbody> </table> <h2 id="Browser_compatibility">浏览器兼容性</h2> <div class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div> <p>{{Compat("css.at-rules.property.inherits")}}</p> <h2 id="See_also">另见</h2> <ul> <li><a href="/zh-CN/docs/Web/API/CSS_Properties_and_Values_API">CSS属性与值 API</a></li> <li><a href="/zh-CN/docs/Web/API/CSS_Painting_API">CSS 绘图 API</a></li> <li><a href="/zh-CN/docs/Web/API/CSS_Typed_OM_API">CSS 类型对象模型</a></li> <li><a href="/zh-CN/docs/Web/Houdini">CSS Houdini</a></li> </ul>