CSS Basics
CSS (Cascading Style Sheets) controls how HTML looks — colors, fonts, spacing, layout, animations. Without CSS, every website would look like a plain text document. CSS is what makes modern websites beautiful and usable.
Three Ways to Add CSS
1. Inline CSS (not recommended)
<p style="color: red; font-size: 20px;">Hello</p>
2. Internal CSS (inside <head>)
<style>
p {
color: red;
font-size: 20px;
}
</style>
3. External CSS (best practice)
<link rel="stylesheet" href="style.css">
External is preferred — keeps HTML clean, allows browser caching, and enables reuse across pages.
CSS Syntax
selector {
property: value;
property: value;
}
/* Example */
h1 {
color: #22d3ee;
font-size: 3rem;
}
CSS Selectors
Basic Selectors
/* Element selector */
p { color: black; }
/* Class selector */
.card { background: white; }
/* ID selector */
#header { padding: 20px; }
/* Universal */
* { margin: 0; }
Combination Selectors
/* Descendant — any p inside .card */
.card p { color: gray; }
/* Direct child — only direct p children of .card */
.card > p { font-weight: bold; }
/* Adjacent sibling — p right after h2 */
h2 + p { margin-top: 0; }
/* Attribute selector */
input[type="email"] { border: 2px solid blue; }
Pseudo-classes
a:hover { color: red; }
a:active { color: orange; }
input:focus { outline: 2px solid cyan; }
li:first-child { font-weight: bold; }
li:last-child { margin-bottom: 0; }
li:nth-child(2) { color: blue; }
Colors in CSS
color: red; /* Named */
color: #22d3ee; /* Hex */
color: rgb(34, 211, 238); /* RGB */
color: rgba(34, 211, 238, 0.8); /* RGBA (with opacity) */
color: hsl(186, 84%, 53%); /* HSL */
Typography
body {
font-family: 'Inter', sans-serif;
font-size: 16px;
font-weight: 400;
line-height: 1.6;
letter-spacing: 0.02em;
text-align: left;
text-transform: uppercase;
text-decoration: underline;
}
Box Model
Every element is a box with 4 layers: content → padding → border → margin.
.card {
width: 300px;
padding: 20px;
border: 2px solid #ccc;
margin: 16px;
box-sizing: border-box; /* includes padding + border in width */
}
Always set box-sizing: border-box globally — it makes sizing much more intuitive. Add this to your reset: *, *::before, *::after { box-sizing: border-box; }
Spacing Shorthand
/* All sides */
padding: 20px;
/* Vertical | Horizontal */
padding: 10px 20px;
/* Top | Horizontal | Bottom */
padding: 10px 20px 15px;
/* Top | Right | Bottom | Left (clockwise) */
padding: 10px 20px 15px 25px;
Display Property
display: block; /* Takes full width */
display: inline; /* Flows with text */
display: inline-block; /* Inline but respects width/height */
display: none; /* Hidden */
display: flex; /* Flexbox (next topic) */
display: grid; /* Grid (next topic) */
Positioning
position: static; /* Default */
position: relative; /* Offset from original position */
position: absolute; /* Positioned relative to nearest positioned ancestor */
position: fixed; /* Fixed to viewport (sticky header) */
position: sticky; /* Hybrid of relative + fixed */
/* Example */
.navbar {
position: fixed;
top: 0;
left: 0;
width: 100%;
z-index: 1000;
}
Backgrounds
.hero {
background-color: #05060a;
background-image: url('bg.jpg');
background-size: cover;
background-position: center;
background-repeat: no-repeat;
/* Gradient */
background: linear-gradient(135deg, #22d3ee, #c084fc);
}
Transitions & Animations
/* Transition */
.btn {
background: blue;
transition: background 0.3s ease, transform 0.3s ease;
}
.btn:hover {
background: red;
transform: translateY(-3px);
}
/* Keyframe animation */
@keyframes pulse {
0%, 100% { transform: scale(1); opacity: 1; }
50% { transform: scale(1.1); opacity: 0.8; }
}
.dot {
animation: pulse 1.5s infinite;
}
Responsive Design — Media Queries
/* Mobile-first approach */
.container {
padding: 16px;
}
@media (min-width: 768px) {
.container { padding: 32px; }
}
@media (min-width: 1024px) {
.container { padding: 60px; }
}
CSS Units
- px — fixed pixels
- % — relative to parent
- rem — relative to root font size (best for scalability)
- em — relative to parent font size
- vw / vh — % of viewport width / height
- clamp() — fluid:
clamp(1rem, 2.5vw, 2rem)
Create an HTML card with a heading, paragraph, and button. Style it with: a gradient background, rounded corners, padding, a border, and a hover effect on the button that changes the background and slightly scales it.
Quick Revision
- CSS controls appearance of HTML.
- 3 ways: inline, internal, external (best).
- Selectors: element, class (
.), ID (#), attribute, pseudo-class. - Box model: content → padding → border → margin.
- Use
box-sizing: border-boxglobally. display: block, inline, inline-block, flex, grid.position: static, relative, absolute, fixed, sticky.- Transitions + animations bring interactivity.
- Media queries for responsiveness.
- Use
rem,%,clamp()over fixedpx.
Interview Questions
Q: What is the CSS box model?
A: Every element is a rectangular box with 4 layers: content, padding (space inside border), border, and margin (space outside border). Understanding this is essential for layout control.
Q: What's the difference between class and id?
A: class can be reused on multiple elements (.card). id must be unique per page (#header). Classes are for styling; IDs are for JavaScript hooks and anchors.
Q: Difference between padding and margin?
A: padding is space inside the border (between content and border). margin is space outside the border (between the element and other elements).
Q: What is specificity in CSS?
A: Specificity determines which rule wins when multiple apply. Order (highest to lowest): inline styles > ID > class/attribute/pseudo-class > element. Higher specificity wins.
Q: What does box-sizing: border-box do?
A: It includes padding and border in the element's total width/height. Without it (default content-box), padding and border add to the size, causing layout surprises.
Want to Master Web Development?
Join our Web Development course in Varanasi — practical classes and career support at our Sarnath centre.