简体   繁体   English

padding:initial与padding:0

[英]padding:initial vs. padding:0

Does padding:initial have any advantage over padding:0 ? padding:initial是否比padding:initial有任何优势padding:0 Example: 例:

<style>
  textarea {
    padding: 0;
  }
</style>
<textarea>Hello, world!</textarea>

They mean the same thing, as the initial value of padding is, in fact, zero on all sides. 它们的意思相同,因为padding的初始值实际上是所有方面都为零。 You can find this in the propdefs for padding and its longhands in this section of the spec. 你可以在propdefs中找到这个,用于padding及其在规范的这一部分中的longhands。

In this case, given the nature of the padding property, and the fact that its initial value is basically common knowledge at this point, there isn't much of an advantage of setting padding: initial over padding: 0 . 在这种情况下,考虑到padding属性的性质,以及此初始值基本上是常识的事实,设置padding: initial没有太大的优势padding: initial padding: 0

In fact, there's one disadvantage , and that is its as yet incomplete browser support owing to the fact that it's a relatively new keyword. 事实上,有一个缺点 ,那就是它的浏览器支持不完整,因为它是一个相对较新的关键字。 Namely, if padding wasn't already zero on this element, browsers that don't support the keyword would ignore the declaration entirely, resulting in the property not being reset to zero. 也就是说,如果此元素上的padding不是零,则不支持该关键字的浏览器将完全忽略该声明,从而导致该属性不会重置为零。


The initial keyword is most useful when used with the new properties defined in the CSS3 Cascading module (none of which are implemented yet AFAIK, and where the keyword itself is defined). 当与CSS3级联模块中定义的新属性一起使用时, initial关键字最有用(其中没有一个实现AFAIK,并且关键字本身已定义)。 It can also be used when you simply want to reset a property to its initial value without caring what that value actually is, which is particularly useful for properties that are either inherited by default, such as color (although the initial value in that case would be currentColor ), or in cases where the "initial" value can otherwise vary depending on the layout and thus cannot be determined as a fixed value. 当您只想将属性重置为其初始值而不关心实际值是什么时,也可以使用它,这对于默认继承的属性(例如color )特别有用(尽管在这种情况下初始值将是是currentColor ),或者在“初始”值可能因布局而变化的情况下,因此不能确定为固定值。 Such cases, however, are admittedly quite rare. 然而,这种情况很少见。

Lastly, note that "initial value" and "browser default value" are not the same; 最后,请注意“初始值”和“浏览器默认值”不一样; the initial value is defined by spec, which is separate from what value the browser assigns to certain properties on certain elements as part of its default stylesheet. 初始值由spec定义,它与浏览器在某些元素上分配给某些属性的值分开,作为其默认样式表的一部分。 There is no reliable way to reset a property to its browser default for the given element without using prior knowledge or even JavaScript to determine this default value. 没有可靠的方法可以将属性重置为给定元素的浏览器默认值,而无需使用先验知识甚至JavaScript来确定此默认值。 See this answer for an illustration. 请参阅此答案以获取说明。

Actualy both are same,But Initial gives the developer more freedom.(@simon's comment)It cannot be always zero,it can be any initial value. Actualy两者都是相同的,但是Initial给开发人员更多的自由。(@ simon的评论)它不能总是为零,它可以是任何初始值。

So there isn't much advantage of using padding: initial over padding: 0 因此使用填充没有太大的优势:初始填充:0

The initial CSS keyword applies the initial value of a property to an element. 初始CSS关键字将属性的初始值应用于元素。 It is allowed on every CSS property and causes the element for which it is specified to use the initial value of the property. 它允许在每个CSS属性上,并导致指定它的元素使用属性的初始值。

Another Example 另一个例子

<p style="color:red"> 
    this text is red 
       <em style="color:initial"> 
          this text is in the initial color (e.g. black)
       </em>
    this is red again
 </p> 

read through https://developer.mozilla.org/en-US/docs/Web/CSS/initial 阅读https://developer.mozilla.org/en-US/docs/Web/CSS/initial

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM