๐Ÿ“˜
NavDoc by Bash School
GithubContact
๐Ÿ“˜
NavDoc by Bash School
  • ๐ŸŽ“Introduction
  • ๐ŸขGetting Started
  • โšกChangelog
  • ๐Ÿ‘จโ€๐Ÿš€Maintainers
  • ๐Ÿ›ฃ๏ธRoadmap
  • Fundamentals
    • The Internet
      • Introduction
      • What is a URL
      • What is a port
      • The DNS protocol
      • The TCP protocol
      • The UDP protocol
      • The Web
      • The HTTP protocol
      • Hyperlinks
      • What is a Web browser
      • What is a Web server
    • HTML
      • Your first HTML page
      • Text tags
      • Attributes
      • Links
      • Images
      • Lists
      • Head Tags
      • Container tags
    • CSS
      • Introduction
      • Colors
      • selectors
      • Cascade
      • Specificity
      • Units
      • Advanced selectors
      • Typography
      • The box model
      • The display property
      • Responsive design
  • JavaScript
    • Basics
      • Introduction
      • Literals , Identifiers, Variables
      • Comments
      • The difference between let, const and var
      • Types
      • Operators and expressions
      • Arithmetic operators
      • The assignment operator
      • Operators precedence
      • Strings
      • Numbers
      • Semicolons, white space and sensitivity
      • Arrays
      • Conditionals
      • Loops
      • Functions
      • Objects
      • Arrays + functions
      • OOPS
      • Asynchronous
      • Scope, hoisting, event loop
      • ES Modules
      • Errors and exceptions
      • Built-in objects
        • The global object
        • Object properties
        • Number
        • String
        • Math
        • JSON
        • Date
        • Intl
        • Set and Map
      • More operators
    • Nodejs
      • Getting Started
      • Installation
      • Hello World in Node
      • Modules
      • Packages
      • File Handling
      • HTTP Request
      • Processing Files
      • HTTP
    • Express.js
      • Getting Started
      • Middleware
      • Serve Static Assets
      • How to Send Files to the Client
      • Sessions
      • Validate Input
      • Sanitizing Data
      • Forms
      • File Uploads
    • React
      • Setting up a React project with Vite
      • React Components
      • Introduction to JSX
      • Using JSX to compose UI
      • The difference between JSX and HTML
      • Embedding JavaScript in JSX
      • Handling user events
      • Managing state
      • Component props
      • Data flow
      • Lifecycle events
      • Managing forms in React
      • Install the React Developer Tools
      • Installing Tailwind CSS in a React app
      • Build a counter in React
    • TypeScript
      • Key Benefits
      • Types of Languages
      • The Need for TypeScript
      • What is TypeScript?
      • The tsc Compiler
      • Basic Types in TypeScript
      • tsconfig
      • Interfaces
      • Types
      • Arrays in TypeScript
      • Enums
      • Exporting and importing
    • MongoDB
      • SQL vs. NoSQL Databases
      • Installing MongoDB
      • MongoDB Databases and Collections
      • Working with Documents
      • MongoDB Operators
      • Sorting, Indexing & Searching
      • Built-in Methods
Powered by GitBook
On this page
  • Named colors
  • RGB and RGBa (rgb() / rgba())
  • Hexadecimal notation (#nnnnnn)

Was this helpful?

Edit on GitHub
  1. Fundamentals
  2. CSS

Colors

PreviousIntroductionNextselectors

Last updated 1 year ago

Was this helpful?

By default, an HTML page is rendered by web browsers quite boring in terms of the colors used.

We have a white background, black color, and blue links. Thatโ€™s it.

In the previous example, we used the CSS rule

p {
  color: red;
}

You intuitively know that this applies the color red to p tags.

Before going into more complex topics of CSS, letโ€™s talk about colors a bit more.

Youโ€™ll work with colors all the time.

The two main properties youโ€™ll use are color and background-color.

Both of them accept a color value, which can be in different forms.

Named colors

First, we have CSS keywords that define colors.

We have a lot of them! Like white, black, red, blue, yellow, but also fancy ones like darkviolet, floralwhite, forestgreen.

On my blog, I have this article with the full list of colors and conversions between names, RGB, and hex notations.

RGB and RGBa (rgb() / rgba())

Named colors are not the only option.

p {
  color: rgb(255, 255, 255); /* white */
  background-color: rgb(0, 0, 0); /* black */
}

rgba() lets you add the alpha channel to enter a transparent part, so the image can be see-through. That can be a number from 0 to 1:

p {
  background-color: rgba(0, 0, 0, 0.5);
}

Hexadecimal notation (#nnnnnn)

Another commonly used option is to express the RGB parts of the colors in hexadecimal notation, which is composed of 3 blocks.

black, which is rgb(0,0,0) in RGB is expressed as #000000 . We can shortcut the 2 numbers in each pair to 1 if they are equal, so it becomes #000 .

white, rgb(255,255,255) can be expressed as #ffffff or #fff.

The hexadecimal notation lets express a number from 0 to 255 in just 2 digits since they can go from 0 to โ€œ15โ€ (f).

We can add the alpha channel to support transparency or opacity by adding 1 or 2 more digits at the end, for example, #00000033. Not all browsers support the shortened notation, so use all 6 digits to express the RGB part.

We also have other notations, but I think those are the most common ones you should know about.

You can use rgb() to calculate a color from its , which sets the color based on its red-green-blue parts ranging from 0 to 255:

.

https://flaviocopes.com/rgb-color-codes/
RGB color code
You can see a calculator here