Are “top” and “to top” the same direction for linear gradients?

两盒软妹~` 提交于 2019-11-27 04:07:36

问题


Browser implementations of linear-gradient() have evolved to support both top and to top as anchor values. Are they the same direction or opposites?


回答1:


They are opposites. to top uses the first color argument at the bottom of the element, while top uses the first color argument at the top.

Here's a fiddle. MDN has the full details and history.




回答2:


They are not the same direction; they are opposites.

to top is an abbreviation for "bottom to top", whereas top means "start from the top".1 This is true not only for the other sides, but also for corners such as top right and bottom left.

Both directional keyword syntaxes were documented in the CSS Images level 3 module, which is why so many implementations made use of the legacy syntax early on. The last public working draft that made use of the legacy syntax is here; you can see a note acknowledging the sheer ambiguity around it, which eventually led to the use of the keyword to in subsequent revisions. The latest revision is here.

Some browsers support both syntaxes for indicating the direction of a linear gradient in their vendor-specific -*-linear-gradient() functions, however it should be noted that the standardized function linear-gradient() only accepts the new syntax with the to keyword. (If a browser accepts both syntaxes in the unprefixed function then it's either outdated or in violation of the spec, and should be reported.)

Note also that the default direction is top for the legacy syntax and to bottom for the current syntax. This means that the default direction is the same, and is unchanged.

Also worth mentioning is that if you use -prefix-free, it already accounts for this change, with the minor caveat that you need to include both syntaxes in separate declarations:

If you include both versions, the cascade does its job and ignores the latter version if it’s not supported:

background: linear-gradient(top, white, black);
background: linear-gradient(to bottom, white, black);

Both statements will be prefixed only in browsers that require the prefix, but regardless of that the browser will use the cascade to figure out which one it can use.


1If there were a from keyword, then top and from top would be equivalent.



来源:https://stackoverflow.com/questions/19486680/are-top-and-to-top-the-same-direction-for-linear-gradients

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!