4: Colors and Backgrounds

കഴിഞ്ഞ chapter-ൽ CSS Selectors ഉപയോഗിച്ച് HTML elements തിരഞ്ഞെടുക്കുന്നത് പഠിച്ചു. ഇനി തിരഞ്ഞെടുത്ത elements-ന്റെ നിറവും background-ഉം എങ്ങനെ മാറ്റാം എന്ന് പഠിക്കാം. ഒരു webpage-ന്റെ visual appearance മെച്ചപ്പെടുത്തുന്നതിൽ Colors-നും Backgrounds-നും വളരെ പ്രധാനപ്പെട്ട സ്ഥാനമുണ്ട്.

CSS-ൽ color നൽകാൻ പല രീതികളുണ്ട്. Color name, HEX, RGB, RGBA, HSL തുടങ്ങിയ രീതികൾ സാധാരണയായി ഉപയോഗിക്കുന്നു. Background-നായി background-color, background-image തുടങ്ങിയ properties ഉപയോഗിക്കാം.

Text Color — color Property

ഒരു element-ന്റെ text color മാറ്റാൻ color property ഉപയോഗിക്കുന്നു.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Text Color</title>
  <style>
    h1 {
      color: blue;
    }

    p {
      color: green;
    }
  </style>
</head>
<body>
  <h1>Welcome to CSS</h1>
  <p>ഇത് green നിറത്തിലുള്ള paragraph ആണ്.</p>
</body>
</html>

Color Names

CSS-ൽ പല നിറങ്ങൾക്കും പേരുകൾ നേരിട്ട് ഉപയോഗിക്കാം. ഉദാഹരണത്തിന് red, blue, green, black, white, orange, purple തുടങ്ങിയ color names ഉപയോഗിക്കാം.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Color Names</title>
  <style>
    .red-text {
      color: red;
    }

    .blue-text {
      color: blue;
    }

    .orange-text {
      color: orange;
    }
  </style>
</head>
<body>
  <p class="red-text">Red text</p>
  <p class="blue-text">Blue text</p>
  <p class="orange-text">Orange text</p>
</body>
</html>

Color names പഠിക്കാൻ എളുപ്പമാണ്. എന്നാൽ professional websites-ൽ കൂടുതൽ കൃത്യമായ നിറങ്ങൾ തിരഞ്ഞെടുക്കാൻ HEX, RGB, HSL തുടങ്ങിയ color values ഉപയോഗിക്കുന്നത് സാധാരണമാണ്.

HEX Color

HEX എന്നത് hexadecimal color notation ആണ്. # ചിഹ്നത്തിന് ശേഷം സാധാരണയായി ആറ് characters ഉപയോഗിച്ചാണ് ഒരു നിറം വ്യക്തമാക്കുന്നത്.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>HEX Colors</title>
  <style>
    h1 {
      color: #2563eb;
    }

    p {
      color: #444444;
    }
  </style>
</head>
<body>
  <h1>HEX Color</h1>
  <p>ഇത് HEX color value ഉപയോഗിച്ചുള്ള text ആണ്.</p>
</body>
</html>

ഉദാഹരണത്തിന് #000000 black-നും #ffffff white-നും പ്രതിനിധീകരിക്കുന്നു. HEX values ഉപയോഗിച്ച് വളരെ കൃത്യമായ colors തിരഞ്ഞെടുക്കാൻ കഴിയും.

Color Valueഉദാഹരണംസാധാരണ നിറം
Color nameredRed
HEX#ff0000Red
RGBrgb(255, 0, 0)Red
RGBArgba(255, 0, 0, 0.5)അൽപ്പം transparent ആയ Red
HSLhsl(0, 100%, 50%)Red

RGB Color

RGB എന്നത് Red, Green, Blue എന്ന മൂന്ന് color components-നെ സൂചിപ്പിക്കുന്നു. ഓരോ component-നും സാധാരണയായി 0 മുതൽ 255 വരെയുള്ള value നൽകാം.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>RGB Colors</title>
  <style>
    .red {
      color: rgb(255, 0, 0);
    }

    .green {
      color: rgb(0, 128, 0);
    }

    .blue {
      color: rgb(0, 0, 255);
    }
  </style>
</head>
<body>
  <p class="red">Red</p>
  <p class="green">Green</p>
  <p class="blue">Blue</p>
</body>
</html>

rgb(255, 0, 0) എന്നത് full Red ആണ്. rgb(0, 255, 0) green-നും rgb(0, 0, 255) blue-നും പ്രതിനിധീകരിക്കുന്നു.

RGBA Color

RGBA, RGB-യുടെ മറ്റൊരു രൂപമാണ്. ഇവിടെ A എന്നത് Alpha channel ആണ്. Alpha ഉപയോഗിച്ച് color-ന്റെ transparency നിയന്ത്രിക്കാം. Alpha value 0 മുതൽ 1 വരെ നൽകാം. 0 പൂർണ്ണമായും transparent-ഉം 1 പൂർണ്ണമായും opaque-ഉം ആണ്.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>RGBA</title>
  <style>
    .box {
      background-color: rgba(0, 0, 255, 0.5);
      padding: 30px;
    }
  </style>
</head>
<body>
  <div class="box">
    Semi-transparent blue background
  </div>
</body>
</html>

HSL Color

HSL എന്നത് Hue, Saturation, Lightness എന്നതിന്റെ ചുരുക്കപ്പേരാണ്. Hue നിറത്തിന്റെ type-നെയും Saturation നിറത്തിന്റെ intensity-യെയും Lightness നിറം എത്ര light അല്ലെങ്കിൽ dark ആണെന്നതിനെയും സൂചിപ്പിക്കുന്നു.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>HSL Colors</title>
  <style>
    h1 {
      color: hsl(220, 80%, 50%);
    }

    p {
      color: hsl(120, 60%, 35%);
    }
  </style>
</head>
<body>
  <h1>HSL Color</h1>
  <p>HSL ഉപയോഗിച്ചുള്ള color values.</p>
</body>
</html>

Color തിരഞ്ഞെടുക്കുമ്പോൾ HEX, RGB, HSL എന്നിവയിൽ ഏത് notation ഉപയോഗിക്കണം എന്നത് project-ന്റെ ആവശ്യകത അനുസരിച്ചിരിക്കും. ഈ course-ൽ പിന്നീട് CSS variables പഠിക്കുമ്പോൾ colors ഒരു സ്ഥലത്ത് define ചെയ്ത് പല സ്ഥലങ്ങളിലും reuse ചെയ്യുന്നതും പഠിക്കും.

Background Color

ഒരു HTML element-ന്റെ background color മാറ്റാൻ background-color property ഉപയോഗിക്കുന്നു.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Background Color</title>
  <style>
    body {
      background-color: #f4f4f4;
    }

    h1 {
      background-color: #2563eb;
      color: white;
      padding: 20px;
    }

    p {
      background-color: white;
      padding: 15px;
    }
  </style>
</head>
<body>
  <h1>My Website</h1>
  <p>ഇത് background-color ഉപയോഗിച്ചുള്ള example ആണ്.</p>
</body>
</html>

ഇവിടെ body-ക്ക് ഒരു light background color നൽകി. h1-നും p-ക്കും വേറിട്ട background colors നൽകി. Padding ഉപയോഗിച്ചതിനാൽ text-നും background-ന്റെ അരികിനും ഇടയിൽ space ലഭിക്കുന്നു.

Background Image

ഒരു element-ന്റെ background ആയി image ഉപയോഗിക്കാൻ background-image property ഉപയോഗിക്കാം. url() എന്ന function-ന്റെ ഉള്ളിൽ image path നൽകണം.

body {
  background-image: url("images/background.jpg");
}

ഇവിടെ images എന്ന folder-ന്റെ ഉള്ളിലുള്ള background.jpg എന്ന image ആണ് background ആയി ഉപയോഗിക്കുന്നത്. File path ശരിയായിരിക്കണം.

Background Repeat

Background image element-ന്റെ മുഴുവൻ ഭാഗവും cover ചെയ്യാൻ കഴിയാത്തപ്പോൾ browser image repeat ചെയ്യാം. ഇത് നിയന്ത്രിക്കാൻ background-repeat property ഉപയോഗിക്കുന്നു.

body {
  background-image: url("images/pattern.png");
  background-repeat: no-repeat;
}

background-repeat: no-repeat ഉപയോഗിച്ചാൽ image ആവർത്തിക്കില്ല. repeat, repeat-x, repeat-y, no-repeat എന്നീ values സാധാരണയായി ഉപയോഗിക്കാം.

Background Position

Background image element-ന്റെ ഏത് ഭാഗത്താണ് കാണേണ്ടത് എന്ന് background-position ഉപയോഗിച്ച് നിയന്ത്രിക്കാം.

body {
  background-image: url("images/background.jpg");
  background-repeat: no-repeat;
  background-position: center;
}

center ഉപയോഗിച്ചാൽ image element-ന്റെ മധ്യഭാഗത്ത് position ചെയ്യപ്പെടും. top, bottom, left, right തുടങ്ങിയ values-ഉം ഉപയോഗിക്കാം.

Background Size

Background image എത്ര വലുപ്പത്തിൽ കാണിക്കണം എന്ന് background-size ഉപയോഗിച്ച് നിയന്ത്രിക്കാം. cover, contain തുടങ്ങിയ values വളരെ പ്രധാനപ്പെട്ടതാണ്.

.hero {
  background-image: url("images/hero.jpg");
  background-size: cover;
  background-position: center;
}

cover ഉപയോഗിക്കുമ്പോൾ element-ന്റെ മുഴുവൻ area cover ചെയ്യാൻ image വലുതാക്കുകയോ ചുരുക്കുകയോ ചെയ്യും. Image-ന്റെ ചില ഭാഗങ്ങൾ crop ആകാം. contain ഉപയോഗിച്ചാൽ image മുഴുവനായും കാണിക്കാൻ ശ്രമിക്കും; അതിനാൽ ചില ഭാഗങ്ങളിൽ empty space ഉണ്ടാകാം.

Background Gradient

ഒരു നിറത്തിൽ നിന്ന് മറ്റൊരു നിറത്തിലേക്ക് smooth ആയി മാറുന്ന background സൃഷ്ടിക്കാൻ CSS gradients ഉപയോഗിക്കാം. Linear gradient-നായി linear-gradient() function ഉപയോഗിക്കുന്നു.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>CSS Gradient</title>
  <style>
    .hero {
      background: linear-gradient(to right, #2563eb, #7c3aed);
      color: white;
      padding: 60px 20px;
      text-align: center;
    }
  </style>
</head>
<body>
  <section class="hero">
    <h1>Welcome</h1>
    <p>CSS gradients ഉപയോഗിച്ച് മനോഹരമായ backgrounds ഉണ്ടാക്കാം.</p>
  </section>
</body>
</html>

Gradient-ന്റെ direction മാറ്റാനും കൂടുതൽ colors ചേർക്കാനും കഴിയും. CSS gradients images ഇല്ലാതെയും visual backgrounds സൃഷ്ടിക്കാൻ സഹായിക്കുന്നു.

Background Properties ഒരുമിച്ച് ഉപയോഗിക്കാം

Background-നായി പല properties പ്രത്യേകം എഴുതാം. പിന്നീട് ഇവയെ shorthand property ഉപയോഗിച്ച് ചുരുക്കിയും എഴുതാം. ഇപ്പോൾ properties ഓരോന്നായി മനസ്സിലാക്കുന്നത് കൂടുതൽ പ്രധാനമാണ്.

.hero {
  background-color: #222;
  background-image: url("images/hero.jpg");
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

Color Contrast

Text-നും background-നും ഇടയിൽ മതിയായ contrast ഉണ്ടായിരിക്കണം. ഉദാഹരണത്തിന് വളരെ light ആയ background-ൽ വളരെ light text ഉപയോഗിച്ചാൽ വായിക്കാൻ ബുദ്ധിമുട്ടാകും. നല്ല contrast readability-ക്കും accessibility-ക്കും പ്രധാനമാണ്.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Color Contrast</title>
  <style>
    .good-example {
      background-color: #1e3a8a;
      color: white;
      padding: 20px;
    }

    .another-example {
      background-color: #f3f4f6;
      color: #222;
      padding: 20px;
    }
  </style>
</head>
<body>
  <div class="good-example">Dark background with light text</div>
  <div class="another-example">Light background with dark text</div>
</body>
</html>

ഒരു Practical Example — Profile Card

ഇപ്പോൾ colors, background-color, border-radius എന്നിവ ഒരുമിച്ച് ഉപയോഗിച്ച് ഒരു ചെറിയ profile card ഉണ്ടാക്കാം. Border-radius-നെക്കുറിച്ച് വിശദമായി പിന്നീട് പഠിക്കും; ഇപ്പോൾ card-ന്റെ രൂപം മെച്ചപ്പെടുത്താൻ അത് ചെറിയ രീതിയിൽ ഉപയോഗിക്കുകയാണ്.

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Profile Card</title>
  <style>
    body {
      background-color: #f1f5f9;
    }

    .card {
      background-color: white;
      color: #222;
      padding: 25px;
      border-radius: 12px;
    }

    .card h1 {
      color: #2563eb;
    }

    .card p {
      color: #555;
    }
  </style>
</head>
<body>
  <div class="card">
    <h1>Arun Kumar</h1>
    <p>Web Development Student</p>
    <p>HTMLയും CSSയും പഠിച്ചുകൊണ്ടിരിക്കുന്നു.</p>
  </div>
</body>
</html>

ഈ example-ൽ page-ന് light background നൽകി. Card-ന് white background നൽകി. Heading-നും paragraphs-നും വ്യത്യസ്ത text colors നൽകി. ഇത്തരത്തിലുള്ള color combinations ഉപയോഗിച്ചാണ് websites-ൽ visual hierarchy സൃഷ്ടിക്കുന്നത്.

Common Beginner Mistakes

  • Text color മാറ്റാൻ background-color ഉപയോഗിക്കുക.
  • background-color-നും color-നും തമ്മിലുള്ള വ്യത്യാസം മനസ്സിലാക്കാതിരിക്കുക.
  • HEX color value എഴുതുമ്പോൾ # മറക്കുക.
  • HEX value-ൽ തെറ്റായ characters ഉപയോഗിക്കുക.
  • Background image-ന്റെ file path തെറ്റായി നൽകുക.
  • Dark background-ൽ dark text ഉപയോഗിച്ച് readability കുറയ്ക്കുക.
  • Background image ഉപയോഗിക്കുമ്പോൾ text വായിക്കാൻ കഴിയുന്നുണ്ടോ എന്ന് പരിശോധിക്കാതിരിക്കുക.
  • ഒരു webpage-ൽ വളരെ അധികം colors ഉപയോഗിച്ച് design അസംഘടിതമാക്കുക.

Color ഉപയോഗിക്കുമ്പോൾ നല്ല Practices

  • ഒരു website-ൽ പരിമിതമായ ഒരു color palette ഉപയോഗിക്കുക.
  • Text-നും background-നും നല്ല contrast നൽകുക.
  • പ്രധാനപ്പെട്ട headings, buttons തുടങ്ങിയവയ്ക്ക് consistent colors ഉപയോഗിക്കുക.
  • Color മാത്രം ഉപയോഗിച്ച് information നൽകുന്നത് ഒഴിവാക്കുക.
  • Background image ഉപയോഗിക്കുമ്പോൾ text readability പരിശോധിക്കുക.
  • Colors പിന്നീട് CSS variables ഉപയോഗിച്ച് എളുപ്പത്തിൽ മാറ്റാൻ കഴിയുന്ന രീതിയിൽ code organize ചെയ്യുക.

Practice Exercises

  • ഒരു heading-ന്റെ text color blue ആക്കുക.
  • ഒരു paragraph-ന് HEX color ഉപയോഗിക്കുക.
  • ഒരു section-ന് light background color നൽകുക.
  • RGB ഉപയോഗിച്ച് ഒരു heading-ന് color നൽകുക.
  • RGBA ഉപയോഗിച്ച് semi-transparent background ഉണ്ടാക്കുക.
  • ഒരു section-ന് background image നൽകുകയും അത് repeat ചെയ്യാതിരിക്കാൻ CSS എഴുതുകയും ചെയ്യുക.
  • background-size: cover ഉപയോഗിച്ച് ഒരു hero section നിർമ്മിക്കുക.
  • രണ്ട് colors ഉപയോഗിച്ച് ഒരു linear gradient background നിർമ്മിക്കുക.

ചുരുക്കത്തിൽ

  • color property ഉപയോഗിച്ച് text color മാറ്റാം.
  • Color names, HEX, RGB, RGBA, HSL തുടങ്ങിയ രീതികളിൽ colors നൽകാം.
  • background-color ഉപയോഗിച്ച് element-ന്റെ background color മാറ്റാം.
  • background-image ഉപയോഗിച്ച് image background ആയി ഉപയോഗിക്കാം.
  • background-repeat image ആവർത്തിക്കണമോ എന്ന് നിയന്ത്രിക്കുന്നു.
  • background-position image-ന്റെ സ്ഥാനം നിയന്ത്രിക്കുന്നു.
  • background-size image-ന്റെ വലുപ്പം നിയന്ത്രിക്കുന്നു.
  • linear-gradient() ഉപയോഗിച്ച് gradient backgrounds ഉണ്ടാക്കാം.
  • Text-നും background-നും നല്ല color contrast ഉണ്ടായിരിക്കണം.

Quiz & Self Assessment

ചോദ്യം 1 / 4
ഒരു element-ന്റെ text color മാറ്റാൻ ഏത് CSS property ആണ് ഉപയോഗിക്കുന്നത്?

അടുത്ത chapter-ൽ webpage-യിലെ text-ന്റെ appearance കൂടുതൽ നിയന്ത്രിക്കാൻ Fonts and Text Styling — font-family, font-size, font-weight, text-align, line-height തുടങ്ങിയ CSS properties — പഠിക്കാം.