How To Change Colour Of Text In Html

7 min read

Changing the color of text is one of the first styling tasks every web developer learns, yet it remains a fundamental skill used in every project from simple landing pages to complex web applications. Understanding how to change colour of text in HTML involves more than just memorizing a single tag; it requires a grasp of modern CSS practices, color theory formats, and accessibility standards. While legacy HTML attributes like <font color="red"> still exist in browser memory, they have been deprecated for decades. Today, the separation of structure (HTML) and presentation (CSS) is the golden standard, offering developers far greater control, maintainability, and flexibility.

The Modern Standard: Using CSS color Property

The primary mechanism for defining text color is the CSS color property. Despite its name, this property specifically controls the foreground color of an element's text content and its text decorations (like underlines or overlines). It does not affect the background; that requires the background-color property The details matter here..

There are three main ways to apply this CSS rule: inline styles, internal stylesheets, and external stylesheets. Each has a specific use case, though modern development heavily favors the external approach.

1. Inline Styles (Quick Prototyping Only)

Inline styles apply CSS directly to a specific HTML element using the style attribute. This method has the highest specificity, meaning it will override almost any other rule, making it useful for quick testing or highly specific, one-off overrides in email templates where external stylesheets aren't supported Which is the point..

Not obvious, but once you see it — you'll see it everywhere.

This paragraph is a vibrant orange-red.

This heading is green.

Why avoid this in production? It mixes content with presentation, bloats your HTML file size, and makes global design changes a nightmare. If you decide to change your brand's primary text color, you must hunt down every single tag.

2. Internal Stylesheets (Single Page Demos)

Placing a <style> block inside the <head> of your document keeps the CSS separate from the HTML structure but contained within a single file. This is ideal for single-page prototypes, CodePen demos, or landing pages that don't share styles with the rest of a site.


  


  

This uses an internal class selector.

Something went wrong!

3. External Stylesheets (Industry Best Practice)

This is the professional standard. Which means you create a separate . css file (e.g., styles.Worth adding: css) and link it in your HTML <head> using the <link> tag. This enables browser caching, keeps your codebase clean, and allows design systems to scale across hundreds of pages.

HTML:


  

CSS (styles.css):

/* Global body text color */
body {
  color: #333333; /* Dark charcoal for readability */
}

/* Specific component colors */
.btn-primary {
  color: #ffffff; /* White text on colored buttons */
}

blockquote {
  color: #6c757d; /* Muted gray for quotes */
}

Deep Dive: CSS Color Value Formats

Knowing where to put the code is only half the battle. So you must also understand the syntax of color values. CSS supports several formats, each with distinct advantages.

Named Colors

CSS defines 140+ standard color names (e.g., red, blue, rebeccapurple, transparent, currentColor). While readable, they offer limited precision.

color: tomato;
color: mediumseagreen;

Hexadecimal Notation (Hex)

The most ubiquitous format in web design. It represents Red, Green, and Blue channels using base-16 numbers That alone is useful..

  • 6-digit: #RRGGBB (e.g., #ff0000 for red).
  • 3-digit shorthand: #RGB (e.g., #f00 for red). Only works when pairs are identical.
  • 8-digit (with Alpha): #RRGGBBAA (e.g., #ff000080 for 50% transparent red).
  • 4-digit shorthand: #RGBA.
color: #3498db;      /* Standard blue */
color: #349;         /* Shorthand */
color: #3498dbcc;    /* Blue with 80% opacity */

RGB and RGBA Functions

Functional notation using decimal values (0–255) or percentages. The modern syntax (CSS Color Level 4) allows space-separated values and an optional slash for alpha, removing the need for the separate rgba() function.

/* Modern Syntax (Preferred) */
color: rgb(255 99 71);           /* Tomato */
color: rgb(100% 0% 0%);          /* Red using percentages */
color: rgb(255 99 71 / 0.5);     /* 50% Transparency */

/* Legacy Syntax (Still supported) */
color: rgb(255, 99, 71);
color: rgba(255, 99, 71, 0.5);

HSL and HSLA (Hue, Saturation, Lightness)

Many designers prefer HSL because it maps intuitively to how humans perceive color. Hue is the color wheel degree (0–360), Saturation is intensity (0–100%), and Lightness is brightness (0–100%). Like RGB, the modern syntax uses spaces and a slash for alpha That's the part that actually makes a difference. Took long enough..

/* Modern Syntax */
color: hsl(120 100% 25%);        /* Dark Green */
color: hsl(120 100% 25% / 0.8);  /* 80% Opacity */

/* Creating color palettes is easy with HSL */
/* Keep Hue constant, vary Lightness */
--brand-light: hsl(210 80% 60%);
--brand-main:  hsl(210 80% 45%);
--brand-dark:  hsl(210 80% 30%);

Modern Color Spaces: LCH, LAB, OKLCH, and color()

For high-fidelity design systems requiring perceptual uniformity (where a "step" in lightness looks the same across all hues), modern browsers support advanced color spaces The details matter here..

  • OKLCH / LCH: Lightness, Chroma, Hue. Excellent for generating accessible palettes programmatically.
  • color(display-p3 ...): Accesses the wider P3 gamut (used by modern Apple/Retina screens) for colors that standard sRGB cannot display.
/* Vibrant P3 color impossible in standard sRGB */
color: color(display-p3 1 0 0); 

/* OKLCH - Perceptually uniform */
color: oklch(0.6 0.2 260); /* Vivid Blue */

The Power of CSS Custom Properties (Variables)

If you are building anything beyond a throwaway prototype, CSS Custom Properties are

CSS Custom Properties (often called “variables”) are declared on an element using the -- prefix and accessed with the var() function. Because they inherit, they can be scoped to containers, pseudo‑classes, or media queries, allowing a single definition to cascade down a component hierarchy without the need for repetitive selectors.

People argue about this. Here's where I land on it.

:root {
  /* Global tokens for the design system */
  --color-primary: #3498db;
  --color-primary-dark: #2980b9;
  --spacing-md: 1rem;
  --radius-lg: 0.5rem;
}

/* Component‑level overrides */
.card {
  --color-bg: #fff;
  --color-text: #222;
  --spacing-inner: var(--spacing-md);
}

/* State‑based changes */
.button:hover {
  --color-bg: var(--color-primary-dark);
  --color-text: #fff;
}

/* Media‑query theming */
@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #121212;
    --color-text: #e0e0e0;
    --color-primary: #66b2ff;
  }
}

Inheritance and the Cascade

When a custom property is defined on an ancestor, every descendant can read it. This makes it possible to create “design tokens” that are automatically available throughout a component tree. Take this: a navigation bar can set --nav-height and the list items inside it can use that same value without any extra code Less friction, more output..

.nav {
  --nav-height: 4rem;
}
.nav__item {
  padding-top: var(--nav-height);
  padding-bottom: var(--nav-height);
}

If a more specific selector redefines the same variable, the nearer declaration wins, giving you fine‑grained control over the cascade without resorting to !important or complex selector wars.

Fallbacks and Safe Defaults

The var() function accepts an optional fallback value, which is used when the variable is undefined. This is especially handy for older browsers that do not support CSS variables, or when a token has not yet been set during runtime.

.button {
  background: var(--color-primary, #0066ff); /* fallback for legacy browsers */
  color: var(--color-text, #222);
}

Dynamic Updates with JavaScript

Because custom properties are part of the DOM’s CSSOM, they can be read or written programmatically. This enables runtime theming, real‑time adjustments based on user interaction, or even animations that modify numeric values.

const doc = document.documentElement;
function setTheme(isDark) {
  doc.style.setProperty('--color-bg', isDark ? '#111' : '#fff');
  doc.style.setProperty('--color-text', isDark ? '#eee' : '#111');
}

// Example: toggle dark mode on button click
document.Think about it: getPropertyValue('--color-bg'). getElementById('theme-toggle').style.addEventListener('click', () => {
  const dark = doc.trim() === '#111';
  setTheme(!

The same principle applies to animating variables:

```css
@keyframes pulse {
  0%   { --scale: 1; }
  50%  { --scale: 1.05; }
  100% { --scale: 1; }
}

.card {
  transform: scale(var(--scale));
  animation: pulse 2s infinite ease-in-out;
}

Performance Considerations

Custom properties are cheap to evaluate because they are resolved once per element, not per property. On the flip side, writing to a variable that is used many times on the same element can cause layout thrashing if the change triggers a repaint. Consider this: best practice is to batch updates—group related changes in a single style. setProperty() call or use CSS transitions on the variable itself when possible That's the whole idea..

Real talk — this step gets skipped all the time The details matter here..

Integration with Modern Color Spaces

When you combine custom properties with the newer color functions (color(), oklch(), etc.), you access a powerful workflow: define a token in a perceptually uniform space, then let the browser render it using the most capable gamut available Which is the point..

:root {
  --brand-primary: oklch(0.6 0.2 260);
}

/* Use the variable directly */
.button {
  background: color(var(--brand-primary));
}

If a browser does not understand oklch, the fallback can be provided by converting the token to a compatible format, ensuring graceful degradation without sacrificing the benefits of modern color science That's the whole idea..

Theming Strategies

  1. **
Fresh from the Desk

Just In

You Might Like

More Good Stuff

Thank you for reading about How To Change Colour Of Text In Html. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home