NUS Hackers Wiki
NUS Hackers Wiki
  • NUS Hackers Wiki
  • Hackerschool
    • Virtual Machines and Linux
    • Beginners' Guide to the Terminal
      • Introduction to the Terminal
      • Modern Shell Tools
      • Shell Scripting
      • Real World Scripting
      • Resources
    • Self-Hosting: Three Easy Pieces
      • 1. Setting up your server
      • 2. Running Services
      • 3. Monitoring your server
    • Vim
    • Introduction to Zig
      • Language Basics
      • Error Handling
      • Memory Management
      • Working with C
      • Exploring comptime
    • CI/CD with Github Actions
      • Background
      • Basics of Github Actions
        • Target workflow
        • Running unit tests
        • Linting code
        • Deploying to Github Pages
      • Advanced use cases
        • Pollers
        • Github script
        • Executing third-party scripts
        • Reusable workflows
      • Cookbook
    • Lightning Git
      • Git Concepts
      • Getting Started with Git
      • Making your first commit
      • Branching
      • Merge Conflicts
      • Integrating remote repositories
      • Collaborative Workflows
      • Commit Manipulation and Reflog
      • Interactive rebasing
      • filter-repo
  • Orbital
    • JavaScript
      • Browser Developer Tools
      • Getting Started
      • Datatypes
      • Operators and Operations
      • Loops and Conditions
      • Functions
      • Strings
      • Arrays
      • HTML
        • Getting Started
        • Tag Attributes
        • HTML Forms
        • Browser Inspector
      • CSS
        • Selectors
        • Colors in CSS
        • Measurements in CSS
        • The Box Model
        • Adding Styles - Part 1
        • Adding Styles - Part 2
      • Working with the DOM
        • Querying the DOM - Selectors
        • Querying the DOM - Element Attributes
        • Querying the DOM - Element Styles
        • Events with JS and HTML
        • Exercise: Click Counter
        • Editing the DOM
        • Fetch Requests
        • Exercise: The NUSMods API
    • React
      • Setup
      • State
    • React Native
      • Setup
      • Intro to JSX
      • Basic Syntax
      • Handling UI
      • Props
      • State Management
    • Git
      • Setup
      • Command Glossary
      • Fundamental Concepts
        • Getting Started
        • Integrating Remote Repositories
        • Branching
        • Merge Conflicts
      • Collaborative Workflows
        • Fork and PR Workflow
        • Branch and PR Workflow
      • Advanced Concepts
        • Ignoring Files
        • Commit Message Conventions
        • Github Collaborators
        • CI/CD with Github Actions
        • Advanced Git Commands
      • FAQ
    • Telegram Bot
      • Creating a TeleBot
      • API Calls
      • Telebot Basics
      • Integrating API's
    • Relational Database
      • Database Overview
      • Database Design
      • Entity Relationship Diagram
      • SQL Basics & PostgreSQL
    • TypeScript
      • Types and Interfaces
      • Utility Types
      • Typing Component Props, Events, and Hooks
      • Why You Should Avoid Using any (and What to Do Instead)
      • TypeScript Tricks You’ll Use All the Time in React
Powered by GitBook
On this page
  • Method 1: by name
  • Method 2: by hexadecimal code
  • Representation of colors in browsers
  • Examples:
  • Back to CSS
  • Method 3: the rgb function
  • Method 4: the rgba function
  • Full list of CSS color values
  • The Eyedropper
  • Next steps
Edit on GitHub
Export as PDF
  1. Orbital
  2. JavaScript
  3. CSS

Colors in CSS

PreviousSelectorsNextMeasurements in CSS

Last updated 12 months ago

In CSS, there are many ways to define colors. We'll look at 4 popular ways to do so in this section.

Method 1: by name

There is a long list of colors that are known to all browsers by name, and these colors can be directly assigned as color values for properties like color (color of the text) or background-color. Of course there are the more "common" colors like red, orange, black, white or green, but there are also many variations and shades like coral, cornflowerblue, limegreen and whitesmoke. The full list of these colors can be found , and there are nearly 150 of them.

Note that quotation marks must not be present around the color values when assigning them: color: red; is correct, but color: "red"; is invalid.

Method 2: by hexadecimal code

To understand this, we need to understand how colors are represented.

Representation of colors in browsers

A color on the screen is actually a combination of 3 main colors: red, green and blue. Combining different "strengths" or "intensities" of these colors leads to a different color that is rendered on screen. The lower the intensity, the darker the shade of the color and the less prominent it is.

The intensity is a decimal integer that ranges from 0 to 255 (and no higher or lower). This is then converted into (a number system with 16 digits: 0 to 9, then a-f for 10-15 respectively) which reduces it to two digits.

0 as a decimal integer is 00 in hexadecimal, and 255 as a decimal integer represented by ff. There are online conversion calculators to convert decimal to hexadecimal and vice-versa, like

The 3 intensities are then combined into one 6-digit hexadecimal value, which is used to represent the color. The red intensity comes first, followed by green, then lastly blue.

When assigning hexadecimal code colors as color values, a # symbol is prefixed before the 6-digit hex value. Note that again quotation marks need to be omitted.

Format of color: #rrggbb

Examples:

If you want pure red, then you max out the red value and minimize the green and blue. This means that the red value is 255 and the green and blue are both 0. Since 255 in decimal is ff in hex, the color code for pure red becomes #ff0000

The same can be done for pure blue and pure green.

In HTML, if all the values (red, green and blue) are maxed out, then you get the color white: #ffffff

On the other hand, removing all color leaves black: #000000

More generally, if the intensities of red, green and blue are equal to each other you end up with a shade of grey. The lower the intensities, the darker the shade.

Here are some more examples:

  • Yellow is max red, max green, no blue: #ffff00

  • Orange is max red, little less green, no blue: #ffa500

  • Violet is medium red, no green and max blue: #7f00ff

Back to CSS

Coming back to CSS color values, you can assign the 6-digit hexadecimal value to a property the same way to assign a color by name. For example: color: #7f00ff; (violet, as above).

The end of the page will show another way to pick a color from a page using the Eyedropper.

Method 3: the rgb function

While hex codes provide a lot of flexibilty and control over the precise shade of color that you want to use, they are rather unintuitive. CSS provides the rgb function, which takes in 3 arguments and returns a color value. The arguments are integers from 0 to 255 and are the red, green and blue values (left to right). They are slightly easier to use than the direct hex codes, and are supported by all browsers.

To represent the color violet as above, I would write: color: rgb(127, 0, 255);

Again, there are color pickers online that can be used to find the RGB values for particular colors and color palettes.

Method 4: the rgba function

This performs like the rgb function but it takes an additional argument. This argument is a decimal number between 0 and 1 inclusive, and indicates the opacity of the color. 1 means the color is fully opaque and 0 means the color is fully transparent.

Usage: color: rgba(127, 0, 255, 0.6);

Full list of CSS color values

The Eyedropper

Firefox has a tool called the Eyedropper that, when active, can pick out the exact html color code of anything present on a webpage. To use this, open up the Browser Inspector and find the dropper icon at the top right corner of the html window (see below).

Alternatively, you can customize the Firefox toolbar to add the Dev Tools menu with the Eyedropper.

Now click on the icon and you'll have the Eyedropper active. It takes control of your cursor and shows you the exact hexadecimal color code of the pixel your cursor is on. If you click while it is active, it copies the color code to your clipboard and deactivates (returning your cursor to normal). To deactivate it without copying the color code, press the esc key.

Here's how it works (note that I have customized my toolbar to have the Eyedropper and Developer tools accessible from there without using the keyboard shortcut):

Later we'll see use the color picker to choose a suitable background color for our html document.

Next steps

Next, we'll see how measurements in CSS work and what valid measurement values can be assigned to elements.

There are online HTML color pickers, and some IDEs (like Visual Studio Code) provide ways to visualise a color using its hex code while you write CSS, so there is no need to memorize color codes. Additionally, if there is a font color or some such color on a website that you like, you can use the to select the element and read its styling rules to see what its color code is.

Here's a .

here
hexadecimal
this one
Browser Inspector
full list of color values accepted by CSS
The Eyedropper is highlighted by a white circle in this picture