CSS(CASCADING STYLE SHEET)
CSS
CSS is the language we use to style a Web page.
What is CSS?
- CSS stands for Cascading Style Sheets
- CSS describes how HTML elements are to be displayed on screen, paper, or in other media
- CSS saves a lot of work. It can control the layout of multiple web pages all at once
- External stylesheets are stored in CSS files.
CSS Selectors
Basic Selectors
-
Type selector: targets elements by tag name
-
Universal selector: matches any element
-
Class selector: targets elements by
class -
ID selector: targets a unique element by
id
🔗 Combinators (show relationships between selectors)
-
Descendant (
): matches any nested element -
Child (
>): matches direct children -
Adjacent sibling (
+): next sibling only -
General sibling (
~): any sibling after an element
| Selector | Matches | Example |
|---|---|---|
E + F |
Only the immediate sibling following E |
h2 + p ➝ styles only the first <p> after <h2> |
E ~ F |
All siblings after E at the same level |
h2 ~ p ➝ styles every <p> that comes later in the same parent |
###
There are three main ways to add CSS styles to an HTML file:
1. ✅ Inline styles
Apply styles directly in the HTML tag using the style attribute:
<p style="color: blue; font-weight: bold;">
This text is blue and bold.
</p>
-
Best for quick one-off styling.
-
Not recommended for larger projects—you can quickly end up with hard-to-maintain code.
2. 🧩 Internal (or Embedded) CSS
Place your styles inside a <style> tag in the HTML document’s <head> section:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Page</title>
<style>
header {
background-color: #f0f0f0;
padding: 20px;
}
header h1 {
color: #333;
}
.highlight {
font-style: italic;
color: crimson;
}
</style>
</head>
<body>
<header>
<h1>Welcome!</h1>
</header>
<p class="highlight">This is an emphasized paragraph.</p>
</body>
</html>
-
Good for small to medium-sized projects or temporary tweaks.
-
Keeps HTML and CSS in one file, but can get messy with lots of styles.
3. 🌐 External CSS
Best practice for most projects. Create a separate .css file and link it in your HTML:
styles.css:
body {
font-family: Arial, sans-serif;
line-height: 1.6;
}
.container {
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.btn-primary {
display: inline-block;
background: #28a745;
color: white;
padding: 10px 15px;
text-decoration: none;
border-radius: 4px;
}
.btn-primary:hover {
background: #218838;
}
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>My Site</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Hello, World!</h1>
<a href="#" class="btn-primary">Click Here</a>
</div>
</body>
</html>
-
Keeps styling separate from content—clean and easy to maintain.
-
Allows reuse of styles across multiple HTML pages (just include the same CSS file).
CSS COLOR PROPERTY
The CSS color property is used to define the foreground color of an element’s text content, including text decorations and currentColor contexts w3docs.com+13w3.org+13w3.org+13. Here’s a detailed breakdown:
🎨 What color Does
-
Sets the color of text, underlines, list markers, and other inline decorations.
-
Inherited by default: if you set it on a parent (e.g., body), all descendants will use that color unless overridden w3schools.com+4w3docs.com+4reddit.com+4.
Sets the color of text, underlines, list markers, and other inline decorations.
Inherited by default: if you set it on a parent (e.g., body), all descendants will use that color unless overridden w3schools.com+4w3docs.com+4reddit.com+4.
🎨 Accepted Color Formats
The color property accepts all types of CSS color values :
-
Named keywords
-
Hex codes
-
RGB / RGBA
background-color property lets you set the background color of an HTML element — this includes its content area, padding, and border (but not the margin)Here are the key CSS text properties used to style and control the layout of textual content:
🧠 Main Text Properties
1. color
Sets the text color. Accepts named colors, hex, RGB(A), HSL(A), etc., and is inherited by child elements
Example:
p { color: #333; }
a { color: rgb(0, 102, 204); }
p { color: #333; }
a { color: rgb(0, 102, 204); }
2. font-family
Defines one or more font families and generic fallbacks (e.g., serif, sans-serif). Browsers use the first available font in the list
body { font-family: "Open Sans", Arial, sans-serif; }
body { font-family: "Open Sans", Arial, sans-serif; }
3. font-size, font-weight, font-style
font-size
Example:
h1 { font-size: 2em; font-weight: 700; }
em { font-style: italic; }
h1 { font-size: 2em; font-weight: 700; }
em { font-style: italic; }
4. line-height
Controls spacing between text lines. Can be unitless, length, or percentage. Inherited
Example:
p { line-height: 1.5; }
p { line-height: 1.5; }
5. text-align
left
Example:
h2 { text-align: center; }
h2 { text-align: center; }
6. text-decoration
Adds or removes decorations (underline, overline, line-through). Non-inherited
Example:
a { text-decoration: none; }
del { text-decoration: line-through; }
a { text-decoration: none; }
del { text-decoration: line-through; }
7. text-transform
none
Example:
h1 { text-transform: uppercase; }
h1 { text-transform: uppercase; }
8. text-indent
Indents the first line of a block. Accepts length or percentage values. Inherited
Example:
p { text-indent: 2em; }
p { text-indent: 2em; }
9. letter-spacing, word-spacing
letter-spacing
Example:
h1 { letter-spacing: 0.05em; }
p { word-spacing: 0.2em; }
h1 { letter-spacing: 0.05em; }
p { word-spacing: 0.2em; }
10. text-shadow
Adds a shadow behind text. Values: offset‑x, offset‑y, blur radius, and color
Example:
h2 {
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
h2 {
text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}
11. white-space, text-overflow, text-justify
white-space
Example:
p.no-wrap {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
p.no-wrap {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
📋 Summary Table
Property
Purpose
color
Text color
font-* (family, size, weight, style)
Font properties
line-height
Vertical spacing between lines
text-align
Horizontal alignment
text-decoration
Underline/overline/strike-through
text-transform
Change letter casing
text-indent
First-line indentation in blocks
letter-spacing
Character spacing
word-spacing
Word spacing
text-shadow
Add shadows to text
white-space
Manage wrapping, breaks, and spaces
text-overflow
Handle overflowed text with ellipsis
text-justify
Justification method for justified text
colorfont-* (family, size, weight, style)line-heighttext-aligntext-decorationtext-transformtext-indentletter-spacingword-spacingtext-shadowwhite-spacetext-overflowtext-justify| Component | Usage Example |
|---|---|
text-decoration-line |
underline / overline / line-through / combos |
text-decoration-style |
solid, dotted, dashed, double, wavy |
text-decoration-color |
Any CSS color (e.g., red, #f00, rgba(…)) |
text-decoration-thickness |
2px, 0.1em, etc. |
| Shorthand usage | text-decoration: underline wavy red 2px; |
/* Checkbox element, when checked */
input[type="checkbox"]:checked {
box-shadow: 0 0 0 3px hotpink;/* Active paragraphs */
p:active {
background: #eee;
}
::) and target sub-elements like the first letter, line, markers, or generated content
Comments
Post a Comment