- CSS Tutorial
- CSS Introduction
- CSS Syntax
- CSS Comments
- CSS Selectors
- CSS Fonts
- CSS Colors
- CSS Backgrounds
- CSS Box Model
- CSS Borders
- CSS Margins
- CSS Padding
- CSS Text
- CSS Images
- CSS Links
- CSS Lists
- CSS Tables
- CSS Outline
- CSS Icons
- CSS Display
- CSS max-witdh
- CSS Position
- CSS z-index
- CSS Overflow
- CSS Float
- CSS Align
- CSS Opacity
- CSS Navigation Bar
- CSS Dropdowns
- CSS Forms
- CSS Units
- CSS !important
- CSS Specificity
- CSS Combinators
- CSS inline-block
- CSS Hover
- CSS Cursors
- CSS Selectors
- CSS Type Selector
- CSS Class Selector
- CSS ID Selector
- CSS Attribute Selector
- CSS Pseudo-class Selector
- CSS Pseudo-element Selector
- CSS Universal Selector
- CSS Advanced
- CSS Text Formatting
- CSS Gradients
- CSS Shadow
- CSS Rounded Corners
- CSS Text Effects
- CSS 2D Transform
- CSS 3D Transform
- CSS Border Images
- CSS Inherit
- CSS Transitions
- CSS Animations
- CSS Box Sizing
- CSS Tooltip
- CSS Masking
- CSS Pagination
- CSS Styling Images
- CSS object-fit
- CSS object-position
- CSS Buttons
- CSS Multiple Columns
- CSS Variables
- CSS Flexbox
- CSS Grid
- CSS Media Queries
CSS Colors
Colors are the heartbeat of web design. They don't just make a site look "pretty"—they establish brand identity, guide user attention, and improve readability. In CSS, we have several different systems to define colors, ranging from simple names to complex functional notations. Understanding these allows you to create everything from subtle gradients to high-contrast accessible layouts.
- Named Colors: CSS includes a standard set of 140 predefined color names. These are incredibly useful for quick prototyping or when you need a standard color without looking up specific codes. For example:
Commonly used named colors include red, blue, green, black, white, yellow, and more modern additions like rebeccapurple or darkslategrey.
body {
color: darkslategray;
background-color: aliceblue;
}
red or magenta when you are debugging layout issues. It’s much faster to type border: 1px solid red; to see an element's boundaries than it is to look up a Hex code.
- Hexadecimal Colors: Hex colors are the industry standard for web development. They are represented by a six-digit code preceded by a hash symbol (#). The code is broken into three pairs: the first pair for Red, the second for Green, and the third for Blue (RRGGBB). Each pair ranges from 00 (none) to FF (full intensity) in base-16 math.
h1 {
color: #FFA500; /* A vibrant Orange */
background-color: #00FF00; /* Pure Green */
}
#ff6600), you can shorten it to three digits (#f60).
- RGB Colors: The RGB system defines a color by specifying the intensity of Red, Green, and Blue using the rgb() function. Each value is an integer between 0 and 255. This is often more intuitive for developers who are used to how screens display light.
p {
color: rgb(255, 0, 0); /* Pure Red */
background-color: rgb(0, 128, 255); /* A nice Sky Blue */
}
- RGBA Colors: Modern web design often requires elements to be partially see-through. RGBA is identical to RGB but adds a fourth value: Alpha. The alpha channel controls opacity on a scale from 0.0 (fully transparent) to 1.0 (fully opaque).
For example, if you want a semi-transparent overlay over an image:
.overlay {
background-color: rgba(0, 0, 0, 0.7); /* 70% opaque black */
}
opacity on an entire element will fade the element AND its children (like text). Using rgba() as a background color only fades the background, leaving the text inside perfectly readable.
- HSL and HSLA Colors: HSL stands for Hue, Saturation, and Lightness. Many designers prefer this because it’s "human-readable."
- Hue: A degree on the color wheel (0-360). 0 is red, 120 is green, 240 is blue.
- Saturation: 0% is gray, 100% is full color.
- Lightness: 0% is black, 100% is white, and 50% is the "pure" color.
.highlight {
background-color: hsl(120, 100%, 50%); /* Pure bright green */
}
.semi-transparent {
background-color: hsla(240, 100%, 50%, 0.5); /* Semi-transparent blue */
}
- Color Names and Keywords: Beyond specific color values, CSS provides functional keywords that help with inheritance and dynamic styling.
.transparent-bg {
background-color: transparent; /* No color, reveals what's behind it */
}
.inherit-color {
color: inherit; /* Forces the element to take the color of its parent */
}
.dynamic-border {
border: 2px solid currentColor; /* Matches the current text color of the element */
}
currentColor for icons (like SVGs) or borders. If you change the text color of a button on hover, currentColor ensures the border or icon color updates automatically without writing extra lines of CSS.
By mastering these different color methods, you gain full control over the visual atmosphere of your site. Whether you prefer the precision of Hex, the transparency of RGBA, or the logical flexibility of HSL, choosing the right tool for the job will make your stylesheets much easier to maintain.