CSS属性不被继承吗? [英] CSS properties are not being inherited?

查看:28
本文介绍了CSS属性不被继承吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

<div style='width:50%;background:yellow;margin:0 auto;padding:20px;font-family:Calibri;'>
    Parent div
    <div style='background:lime;'>
      Child div
    </div>
</div>

这里的样式 font-family:Calibri; 在子级div中是自动继承的,而其他样式仍然是 遗留的 (未继承).

Here the style font-family:Calibri; is automatically inherited in the child-div, whereas the other styles remain bequeathed (un-inherited).

这是一个演示 .

DEMO,为child-div定义了相同的填充 .

如您所见,结果不同相同.

为什么会这样?是否有继承的特定样式?如果是这样,它们是什么?

Why is it so? Are there any specific styles which are inherited? If so, what are they?

推荐答案

某些属性是继承的,而某些则不是.

Some properties are inherited, and some aren't.

如果您希望某些元素从其父项继承不可继承属性的值,则可以使用

If you want some element to inherit the value of a non-inheritable property from its parent, you can use inherit value.

我建议您阅读规范的继承部分:

I suggest reading Inheritance section of the spec:

如上所述,某些值由文档树中元素的子代继承.每个属性都定义是否继承.

Some values are inherited by the children of an element in the document tree, as described above. Each property defines whether it is inherited or not.

发生继承时,元素将继承计算值.来自父元素的计算值将同时成为指定值和子元素上的计算值.

When inheritance occurs, elements inherit computed values. The computed value from the parent element becomes both the specified value and the computed value on the child.

每个属性还可以具有"inherit"的级联值,这意味着对于给定的元素,该属性采用与该元素的父级属性相同的指定值.继承"值可用于强制值的继承,也可用于通常不继承的属性.

Each property may also have a cascaded value of 'inherit', which means that, for a given element, the property takes the same specified value as the property for the element's parent. The 'inherit' value can be used to enforce inheritance of values, and it can also be used on properties that are not normally inherited.

这篇关于CSS属性不被继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆