How are CSS color values set? And how about a random color?

Source: Internet
Author: User

There are several ways to use colors in Web pages

1, color name , such as red black white

2, hexadecimal color , commonly used in Web pages, each two-bit represents the value of red-green-blue, such as #ffffff白色 #000000黑色

3, Rgba color , such as RGBA (255,255,255,0.5) Translucent white, this way IE8 and the following incompatible

RGBA (R,g,b,a)

R: Red value. Positive integers | Percentage

G: Green value. Positive integers | Percentage

B: Blue value. Positive integers | Percentage

A:alpha transparency. Value between 0~1.

4, Hsla color value , such as Hsla (360, 50%, 50%,. 5) Translucent red, this way IE8 and the following incompatible

HSLA (H,s,l,a)

H:hue (hue). 0 (or 360) indicates red, 120 is green, 240 is blue, and other values are preferred to specify the color. Value is: 0-360

S:saturation (saturation). Values are: 0%-100%

L:lightness (brightness). Values are: 0%-100%

A:alpha transparency. Value between 0~1.

So how about a random color value?

In general, we can use the RGBA or Hsla mode to random values

1. Random Rgba Color

1) Random one 0~255 value, 2) stitch these values into an RGBA string to

The code is as follows: (Create a random color object with a transparency of 0.8, get the color property of the object and the Rgba color value you want)

// Color Objects  function Color () {    this. R = Math.floor (Math.random () *255);      this. g = Math.floor (Math.random () *255);     this. B = Math.floor (Math.random () *255);      This  This  This this. B + ', 0.8) ';  }

2. Random Hsla Color

1) Random one 0~360 color value range, 2) assemble Hsla color value string (saturation, brightness, transparency on demand)

// Color Objects  function Color () {    this. Colorangle = Math.floor (Math.random () *360);       This this. Colorangle + ', 100%,50%,1) ';  }

How are CSS color values set? And how about a random color?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.