CSS Color CodesWhat is Color Code?The color in the HTML elements is set using the color code property in CSS. We can change an element's background or font color with color properties. CSS uses color codes to specify the colors of elements on a web page. There are different ways we can define the color of an element by using some ways such as:
RGB FormatIn CSS, RGB color format presents the three primary colors: Reb, Blue, and Green. In CSS, color can be defined using the RGB color model or format by indicating the intensity of each color. In CSS, the RGB format is represented by the syntax RGB (red, green, blue), where red, green, and blue are integer values ranging from 0 to 255. Each color channel is broken down as follows:
Example: Here's an example of how you can use RGB color codes in CSS to style an element: In this example, we define the CSs class my-element. In this background, the color property was set to pure red using the RGB color code rbg(255,0,0), and in this, we set the height and width properties to 200 pixels. As you can see, we set pixel 200x200 element with a red background when you open the HTML file in a browser. We can implement properties like color, background color, and border color in CSS using RGB color codes. We can swap out the RGB (255,0,0) value for the desired RGB color code to get the desired color effect. RGBA FormatIn CSS, we have RGB format that represents red, blue, and green colors; in CSS, RGBA format helps us specify a color's opacity or transparency. With the help of Red, Green, blue, and Alpha, we can refer to the RGBA model. With the help of RGBA format, we can denote Alpha for the opacity and transparency of the color, and red, green, and blue can denote their respective color in CSS. The breakdown of each element is as follows:
Example: Here's an example of using the RGBA color code in CSS to style an element with transparency: The example above defines the CSS class my-element. We set the background-color property to the RGBA color code rgba(255, 0, 0, 0.5) and the width and height properties to 200 pixels each. This red has an opacity of 0.5 and is semi-transparent. Hexadecimal NotationIn CSS, hexadecimal notation is a common technique for describing colors because hexadecimal contains three pair digits that represent the intensity of red, green, and blue color that is used for color combination to represent color using a total of six characters. In CSS, we can define the hexadecimal notation in this syntax #RRGGBB, where RR represents the red intensity, GG represents the green intensity, and BB represents the blue intensity. And each pair has a range from 00 to FF in the hexadecimal notation. The breakdown of each element is as follows:
Example: In the example above, the CSS class my-element is defined. When setting the background-color property, we entered the hexadecimal color code #FF0000, which stands for pure red. We also set the width and height properties to 200 pixels. HSLIn CSS, another color model is used to define colors in HSL (Hue, Saturation, and Lightness). We can describe the hue, saturation, and lightness that values color, and it offers a more logical way to define colors. The HSL color model is broken down into the following elements:
Example: Let's take an example to understand HSL in more detail: The CSS class my-element is defined in the example above. We set the background-color property using the HSL color code hsl (0, 100%, 50%), which denotes pure red, and we set the width and height properties to 200 pixels. When you open it in a web browser, this HTML file will display a 200x200 pixel element with a red background color. Using the HSL color code, you can define colors by considering their hue, saturation, and lightness values. Hue (in degrees), saturation (in percentage), and lightness (in percentage) are the three inputs for the hsl() function. HSLAHSLA is a CSS color model extension to the HSL (Hue, Saturation, and Lightness) color model that includes an additional parameter for defining a color's opacity or transparency. The letter "A" in HSLA stands for Alpha, which denotes the degree of transparency. Colors in the HSLA format are specified in CSS using the syntax hsla (hue, saturation, lightness, Alpha), where:
Example: The CSS class my-element is defined in the example above. We used the HSLA color code hsla(0, 100%, 50%, 0.5) to set the width, height, and background color properties to 200 pixels each. This displays a 0.5 opacity red color that is semi-transparent. When you open this HTML file in a web browser, you will see a 200x200 pixel element with a semi-transparent red background. Next TopicDefine CSS Portal |