Course එකට ආපසු

CSS Deep Dive — ඇඳුම් සහ විලාසිතා

40 මිනිත්තු📖Lecture

CSS Deep Dive — ඇඳුම් සහ විලාසිතා

CSS (Cascading Style Sheets) එකෙන් control කරන්නේ HTML elements පෙනෙන හැටි.

  • CSS නැතුව: Plain, default-styled content
  • CSS එක්ක: Beautiful, branded, professional designs

CSS Basic Structure

හැම CSS rule එකක්ම follow කරන්නේ මේ pattern එක:

CSS
selector {
property: value;
property: value;
}

Example:

CSS
h1 {
color: blue;
font-size: 32px;
}
  • Selector (h1) — කාටද style කරන්නේ
  • Property (color) — මොකක්ද change කරන්නේ
  • Value (blue) — මොනවටද change කරන්නේ

Selector Types

1. Element Selector

Type එකේ හැම element එකක්ම target කරනවා:

CSS
p {
color: gray;
}

Target: Page එකේ තියෙන ALL <p> elements

2. Class Selector

Specific class එකක් තියෙන elements target කරනවා:

CSS
.highlight {
background-color: yellow;
}

Target: class="highlight" තියෙන හැම එකක්ම

3. ID Selector

ONE specific element එකක් target කරනවා:

CSS
#main-title {
font-size: 48px;
}

Target: id="main-title" තියෙන එක element එක

4. Descendant & Combined Selectors

CSS
/* Descendant - elements ඇතුලේ තියෙන elements */
header nav a {
color: white;
}
/* Combined - conditions කිහිපයක් */
div.card {
border: 1px solid gray;
}
/* Multiple - ගොඩකට same styles */
h1, h2, h3 {
font-family: Arial, sans-serif;
}

Selector Specificity

Same element එකට rules කිහිපයක් target වුනොත්, දිනන්නේ කවුද?

Specificity Hierarchy (strongest → weakest)

  1. Inline styles (style="...") — Highest priority
  2. ID selectors (#myId) — High priority
  3. Class selectors (.myClass) — Medium priority
  4. Element selectors (p) — Low priority
CSS
p { color: black; } /* Specificity: 0,0,1 */
.intro { color: blue; } /* Specificity: 0,1,0 */
#main-paragraph { color: red; } /* Specificity: 1,0,0 */

<p class="intro" id="main-paragraph"> කියලා තිබුනොත්: Text එක red වෙනවා ID ට highest specificity නිසා.


The Box Model

හැම HTML element එකක්ම layers තියෙන box එකක්:

Text
┌─────────────────────────────────────────────────────┐
│ MARGIN │ ← පිටත ඉඩ
│ ┌───────────────────────────────────────────────┐ │
│ │ BORDER │ │ ← දාරය/තාප්පය
│ │ ┌─────────────────────────────────────────┐ │ │
│ │ │ PADDING │ │ │ ← ඇතුළත ඉඩ
│ │ │ ┌───────────────────────────────────┐ │ │ │
│ │ │ │ CONTENT │ │ │ │ ← Actual content
│ │ │ └───────────────────────────────────┘ │ │ │
│ │ └─────────────────────────────────────────┘ │ │
│ └───────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────┘

Box Model Properties

CSS
.box {
/* Content size */
width: 200px;
height: 100px;
/* Padding - border එකේ ඇතුළත ඉඩ */
padding: 20px; /* හැම පැත්තටම */
padding: 10px 20px; /* Top/bottom, Left/right */
/* Border */
border: 1px solid black;
border-radius: 8px; /* Rounded corners */
/* Margin - border එකෙන් පිටත ඉඩ */
margin: 20px; /* හැම පැත්තටම */
margin: 10px auto; /* Horizontally center */
}

මතක තියාගන්න

  • Padding: Content සහ border අතර ඉඩ (inside)
  • Margin: Elements අතර ඉඩ (outside)

Color Properties

CSS
/* Text Color */
p {
color: blue; /* Named color */
color: #3498db; /* Hex code */
color: rgb(52, 152, 219); /* RGB */
}
/* Background */
.card {
background-color: #f5f5f5;
/* Gradient */
background: linear-gradient(to right, #667eea, #764ba2);
/* Image */
background-image: url('background.jpg');
background-size: cover;
}

Typography Properties

CSS
.text {
/* Font family */
font-family: Arial, Helvetica, sans-serif;
/* Size */
font-size: 16px;
/* Weight */
font-weight: bold; /* or 700 */
/* Alignment */
text-align: center;
/* Decoration */
text-decoration: none; /* Underlines අයින් කරන්න */
/* Line spacing */
line-height: 1.6;
}

Layout Properties

Display Property

CSS
.element {
display: block; /* Full width ගන්නවා, new line */
display: inline; /* Content තියෙන size විතරයි */
display: flex; /* Flexbox container */
display: none; /* Completely hide */
}

Flexbox (වැඩිපුරම Important!)

CSS
.container {
display: flex;
/* Horizontal alignment */
justify-content: center; /* Center */
justify-content: space-between;/* Evenly spread */
/* Vertical alignment */
align-items: center; /* Vertically center */
/* Direction */
flex-direction: row; /* Horizontal (default) */
flex-direction: column; /* Vertical */
/* Items අතර gap */
gap: 20px;
}

Position Property

CSS
.element {
position: static; /* Default, normal flow */
position: relative; /* Normal position එකට relative */
position: absolute; /* Positioned ancestor එකට relative */
position: fixed; /* Viewport එකට fixed */
/* Position කරන්න */
top: 10px;
right: 10px;
}

Interactive States

CSS
/* Hover Effect */
button {
background: blue;
transition: background 0.3s; /* Smooth transition */
}
button:hover {
background: darkblue;
}
/* Focus (inputs වලට) */
input:focus {
outline: 2px solid blue;
}
/* Active (click කරද්දී) */
button:active {
transform: scale(0.95);
}

Responsive Design

CSS
/* Default styles (mobile first) */
.container {
padding: 10px;
flex-direction: column;
}
/* Tablet and up */
@media (min-width: 768px) {
.container {
padding: 20px;
flex-direction: row;
}
}
/* Desktop and up */
@media (min-width: 1024px) {
.container {
padding: 40px;
max-width: 1200px;
margin: 0 auto;
}
}
Breakpoint
Size
Mobile
< 768px
Tablet
768px - 1023px
Desktop
1024px+

Complete Example

CSS
/* Default margins reset කරන්න */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
/* Card component */
.card {
background: white;
border-radius: 12px;
padding: 24px;
margin: 20px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
/* Button */
.button {
display: inline-block;
background: #3498db;
color: white;
padding: 12px 24px;
border: none;
border-radius: 6px;
cursor: pointer;
transition: background 0.3s;
}
.button:hover {
background: #2980b9;
}

Lesson Summary

CSS Structure

selector { property: value; }

Selector Priority

  1. Inline styles (highest)
  2. ID selectors (#)
  3. Class selectors (.)
  4. Element selectors (lowest)

Box Model එක

ContentPaddingBorderMargin

Key Properties

Category
Properties
Colors
color, background-color
Typography
font-size, font-family, font-weight
Spacing
margin, padding, gap
Layout
display: flex, justify-content, align-items
Interactive
:hover, :focus, transition

📝 Quiz

1/3

HIGHEST specificity තියෙන selector එක මොකද?


Next Up

Lesson 2.4: JavaScript Deep Dive
Pages interactive කරන programming language එක understand කරමු!