简体   繁体   English

来自十六进制 RGB 值的 CSS 中的半透明背景颜色

[英]Semi-transparent background color in CSS from hex RGB value

Background: I have to work with a CMS that provides some integrated SASS functionality: Users can define colors, they are provided as hex values in a variable to the SCSS files I can edit.背景:我必须使用提供一些集成 SASS 功能的 CMS:用户可以定义颜色,它们以十六进制值的形式提供给我可以编辑的 SCSS 文件的变量。

The problem: I have to use a lighter version of one of this colors for a background of a div.问题:我必须使用这种颜色之一的较浅版本作为 div 的背景。 (lighter in the sense of a background-color with some transparency). (在具有一定透明度的背景颜色的意义上更轻)。

Usually I would do this with通常我会这样做

background-color: rgba(r,g,b,a) ; background-color: rgba(r,g,b,a) ;

But what can be done if I don't have the single red/blue/green values, but only the combined hex RGB values?但是,如果我没有单一的红/蓝/绿值,而只有组合的十六进制 RGB 值,该怎么办?

Example of the result that should be achieved:应达到的结果示例:

 /* user color comes as a variable containing a hex value variable: $color example value: 00d; */ h1{ background-color: #00d; /* $color resolves to 00d */ } p{ background-color: #7e7eed; /* <- this value should be calculated from the variable $color, in this case #00d + opacity 50% */ }
 <h1>Title</h1> <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>

Use lighten SASS function 使用减轻 SASS功能

lighten($color, 20%)

If you want to change alfa of your color, use rgba function 如果要更改颜色的Alfa ,请使用rgba函数

rgba($color,0.8)

See the demo 观看演示

Modify the "opacity" css property of your css. 修改CSS的“不透明度” css属性。 A value of 0 is complete transparent, a value of 1 is completely opaque, and a value of .5 is half transparent, etc. Smaller opacity values will make your color "lighter". 值0表示完全透明,值1表示完全不透明,值0.5表示一半透明,依此类推。较小的不透明度值会使您的颜色“更浅”。 Any value between 0 and 1 will work. 0到1之间的任何值都可以。 This is essentially the same as the alpha value in your rgba (a == alpha). 这基本上与您的rgba中的alpha值相同(a == alpha)。

ref: http://www.w3schools.com/css/css_image_transparency.asp 参考: http : //www.w3schools.com/css/css_image_transparency.asp

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

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