CSS 十六进制 RGBA? [英] CSS hexadecimal RGBA?

查看:59
本文介绍了CSS 十六进制 RGBA?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道你可以写...

背景色:#ff0000;

...如果你想要红色的东西.

你可以写...

背景色:rgba(255, 0, 0, 0.5);

...如果你想要红色和半透明的东西.

有什么简洁的方法可以用十六进制编写部分透明的颜色吗?我想要类似的东西:

背景色:#ff000088;<--- 88 是 alpha

...或...

background-color: #ff0000 50%;

我以十六进制获取所有颜色,并且必须将它们全部转换为十进制 0-255 比例很烦人.

解决方案

CSS Color Module Level 4 可能支持 4 位和 8 位十六进制 RGBA 表示法!

三周前(2014 年 12 月 18 日)

然而,也就是说,CSS 的工作方式意味着我们今天就可以真正开始使用这些了!如果您真的想立即开始使用它们,只要您添加回退,任何不支持的浏览器都会忽略新属性,直到它们被认为有效:

figure {边距:0;填充:4px;/* 回退(...到不支持 alpha 透明度的浏览器).*/背景:#FEFE7F;颜色:#3F3FFE;/* 当前的现代"浏览器支持.*/背景:rgba(255, 255, 0, 0.5);颜色:RGBA(0, 0, 255, 0.75);/* 跌倒……前进?*/背景:#ffff007F;/* 或者,不太准确,#ff08 */颜色:#0000ffbe;/* 或 #00fc */}

Hello, world!</figure>

只要您在支持 CSS 中的 backgroundcolor 属性的浏览器上查看此答案,

上面代码片段结果中的元素将与此非常相似:

在 Windows 上使用最新版本的 Chrome (v39.0.2171) 检查我们的

元素,我们将看到以下内容:

6 位十六进制回退被 rgba() 值覆盖,我们的 8 位十六进制值被忽略,因为它们目前被 Chrome 的 CSS 解析器视为无效.一旦我们的浏览器支持这些 8 位值,这些值将覆盖 rgba() 的值.

UPDATE 2018-07-04: Firefox、Chrome 和 Safari 现在支持此表示法,Edge 仍然缺失但可能会跟进(https://caniuse.com/#feat=css-rrggbbaa).

I know you can write ...

background-color: #ff0000;

... if you want something that is red.

And you can write ...

background-color: rgba(255, 0, 0, 0.5);

... if you want something red and translucent.

Is there any terse way of writing partially transparent colors in hexadecimal? I want something like:

background-color: #ff000088; <--- the 88 is the alpha

... or ...

background-color: #ff0000 50%;

I am getting all my colors in hexadecimal, and having to convert them all to the decimal 0-255 scale is annoying.

解决方案

The CSS Color Module Level 4 will probably support 4 and 8-digit hexadecimal RGBA notation!

Three weeks ago (18th of December 2014) the CSS Color Module Level 4 editor's draft was submitted to the CSS W3C Working Group. Though in a state which is heavily susceptible to change, the current version of the document implies that in the somewhat near future CSS will support both the 4 and 8-digit hexadecimal RGBA notation.

Note: the following quote has irrelevant chunks cut out and the source may have been heavily modified by the time you read this (as mentioned above, it's an editor's draft and not a finalised document).
If things have heavily changed, please leave a comment letting me know so I can update this answer!

§ 4.2. The RGB hexadecimal notations: #RRGGBB

The syntax of a <hex-color> is a <hash-token> token whose value consists of 3, 4, 6, or 8 hexadecimal digits. In other words, a hex color is written as a hash character, "#", followed by some number of digits 0-9 or letters a-f (the case of the letters doesn’t matter - #00ff00 is identical to #00FF00).

8 digits

The first 6 digits are interpreted identically to the 6-digit notation. The last pair of digits, interpreted as a hexadecimal number, specifies the alpha channel of the color, where 00 represents a fully transparent color and ff represent a fully opaque color.

Example 3
In other words, #0000ffcc represents the same color as rgba(0, 0, 100%, 80%) (a slightly-transparent blue).

4 digits

This is a shorter variant of the 8-digit notation, "expanded" in the same way as the 3-digit notation is. The first digit, interpreted as a hexadecimal number, specifies the red channel of the color, where 0 represents the minimum value and f represents the maximum. The next three digits represent the green, blue, and alpha channels, respectively.

What does this mean for the future of CSS colours?

This means that assuming this isn't completely removed from the Level 4 document, we'll soon be able to define our RGBA colours (or HSLA colours, if you're one of those guys) in hexadecimal format in browsers which support the Color Module Level 4's syntax.

Example

elem {
    background: rgb(0, 0, 0);           /* RGB notation (no alpha). */
    background: #000;                   /* 3-digit hexadecimal notation (no alpha). */
    background: #000000;                /* 6-digit hexadecimal notation (no alpha). */
    background: rgba(0, 0, 0, 1.0);     /* RGBA notation. */

    /* The new 4 and 8-digit hexadecimal notation. */
    background: #0000;                  /* 4-digit hexadecimal notation. */
    background: #00000000;              /* 8-digit hexadecimal notation. */
}

When will I be able to use this in my client-facing products?

All jokes aside: it's currently only the start of 2015, so these will not be supported in any browser for quite some time yet - even if your product is only designed to work on the most up-to-date of browsers you'll probably not be seeing this in action in a production browser any time soon.

View current browser support for #RRGGBBAA color notation

However, that said, the way CSS works means that we can actually start using these today! If you really want to start using them right now, as long as you add a fall back any non-supporting browsers will simply ignore the new properties until they are deemed valid:

figure {
  margin: 0;
  padding: 4px;
  
  /* Fall back (...to browsers which don't support alpha transparency). */
  background: #FEFE7F;
  color: #3F3FFE;
  
  /* Current 'modern' browser support. */
  background: rgba(255, 255, 0, 0.5);
  color: rgba(0, 0, 255, 0.75);
  
  /* Fall... foward? */
  background: #ffff007F; /* Or, less accurately, #ff08 */
  color: #0000ffbe;      /* Or #00fc */
}

<figure>Hello, world!</figure>

As long as you're viewing this answer on a browser which supports the background and color properties in CSS, the <figure> element in result of the above snippet will look very similar to this:

Using the most recent version of Chrome on Windows (v39.0.2171) to inspect our <figure> element, we'll see the following:

The 6-digit hexadecimal fall back is overridden by the rgba() values, and our 8-digit hexadecimal values are ignored as they are currently deemed invalid by Chrome's CSS parser. As soon as our browser supports these 8-digit values, these will override the rgba() ones.

UPDATE 2018-07-04: Firefox, Chrome and Safari are support this notation now, Edge still missing but will probably follow (https://caniuse.com/#feat=css-rrggbbaa).

这篇关于CSS 十六进制 RGBA?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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