CSS (Cascading Style Sheets) is used to style HTML elements. It defines how HTML elements should be displayed, including layout, colors, fonts, spacing, and more.
You can add CSS in three ways: Inline, Internal, and External.
<!DOCTYPE html>
<html>
<head>
<style>
/* Internal CSS */
h1 {
color: blue;
}
</style>
<link rel="stylesheet" href="styles.css"> <!-- External CSS -->
</head>
<body>
<h1>This is a heading</h1>
<p style="color: green;">This is a paragraph</p> <!-- Inline CSS -->
</body>
</html>
This is a heading (in blue)
This is a paragraph (in green)
Selectors in CSS target HTML elements to apply styles. Common selectors include element, class, id, and universal selectors.
<style>
p {
color: red;
}
.highlight {
background-color: yellow;
}
#special {
font-weight: bold;
}
</style>
<p>Normal paragraph</p>
<p class="highlight">Highlighted paragraph</p>
<p id="special">Special paragraph</p>
Normal paragraph
Highlighted paragraph
Special paragraph
Colors can be specified in different formats like named colors, HEX, RGB, RGBA, HSL, and HSLA.
<style>
.named { color: tomato; }
.hex { color: #00ff00; }
.rgb { color: rgb(0, 0, 255); }
.rgba { color: rgba(255, 0, 0, 0.5); }
</style>
<p class="named">Tomato color</p>
<p class="hex">Hex color</p>
<p class="rgb">RGB color</p>
<p class="rgba">RGBA semi-transparent red</p>
Tomato color
Hex color
RGB color
RGBA semi-transparent red
You can control font family, size, weight, and style using CSS properties like font-family
, font-size
, font-weight
, and font-style
.
<style>
.text-sample {
font-family: Arial, sans-serif;
font-size: 20px;
font-weight: bold;
font-style: italic;
}
</style>
<p class="text-sample">Styled text</p>
Styled text
You can align text left, center, or right. You can also underline, overline, or strike through using text-decoration
.
<style>
.center { text-align: center; }
.underline { text-decoration: underline; }
.strike { text-decoration: line-through; }
</style>
<p class="center">Centered text</p>
<p class="underline">Underlined text</p>
<p class="strike">Strikethrough text</p>
Centered text
Underlined text
Strikethrough text
All HTML elements are treated as boxes. The box model includes content, padding, border, and margin.
<style>
.box {
width: 200px;
padding: 20px;
border: 5px solid black;
margin: 10px;
background-color: lightgray;
}
</style>
<div class="box">This is a box</div>
The display
property defines how elements are rendered on the page. Common values include block
, inline
, inline-block
, none
, and flex
.
<style>
span {
display: block;
background: lightblue;
}
</style>
<span>Block 1</span>
<span>Block 2</span>
The visibility
property controls if an element is visible or hidden. visibility: hidden
hides the element but it still takes up space.
<style>
.hide-me { visibility: hidden; }
</style>
<p>Visible text</p>
<p class="hide-me">You can't see me</p>
Visible text
The position
property determines how an element is positioned on the page. Values include static, relative, absolute, fixed, and sticky.
<style>
.relative-box {
position: relative;
width: 200px;
height: 200px;
background-color: lightblue;
}
.absolute-box {
position: absolute;
top: 20px;
left: 20px;
width: 100px;
height: 100px;
background-color: coral;
}
</style>
<div class="relative-box">
<div class="absolute-box">ABS</div>
</div>
fixed
elements stay in place even when scrolling. sticky
toggles between relative and fixed depending on scroll position.
Flexbox is a layout mode designed to make it easier to align and distribute space among items in a container.
<style>
.flex-container {
display: flex;
justify-content: space-around;
background-color: lightgray;
}
.flex-item {
width: 100px;
height: 100px;
background-color: teal;
color: white;
text-align: center;
line-height: 100px;
}
</style>
<div class="flex-container">
<div class="flex-item">1</div>
<div class="flex-item">2</div>
<div class="flex-item">3</div>
</div>
CSS Grid is a powerful layout system for building two-dimensional grid-based designs. You define rows and columns for layout.
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
gap: 10px;
background-color: #ddd;
padding: 10px;
}
.grid-item {
background-color: steelblue;
color: white;
padding: 20px;
text-align: center;
}
</style>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
<div class="grid-item">4</div>
<div class="grid-item">5</div>
<div class="grid-item">6</div>
</div>
Media queries let you apply CSS styles based on device characteristics like screen width, height, resolution, and orientation. They make websites responsive and adaptive.
<style>
body {
background-color: lightgreen;
}
@media (max-width: 600px) {
body {
background-color: lightcoral;
}
}
</style>
Output: Resize the browser to see the background color change below 600px width.
Using percentage widths allows content to adjust based on screen size, instead of fixed pixels.
<style>
.responsive-box {
width: 80%;
background-color: skyblue;
padding: 20px;
margin: auto;
}
</style>
<div class="responsive-box">
Resize me!
</div>
vw
(viewport width) and vh
(viewport height) adjust based on screen size.
<style>
.viewport-box {
width: 50vw;
height: 20vh;
background-color: navy;
color: white;
text-align: center;
line-height: 20vh;
}
</style>
<div class="viewport-box">
50vw x 20vh box
</div>
CSS transitions let you animate changes in CSS properties smoothly over time.
<style>
.transition-box {
width: 150px;
height: 150px;
background-color: crimson;
transition: background-color 0.5s ease;
}
.transition-box:hover {
background-color: gold;
}
</style>
<div class="transition-box"></div>
CSS animations allow you to animate transitions between CSS states using keyframes.
<style>
@keyframes bounce {
0% { transform: translateY(0); }
50% { transform: translateY(-50px); }
100% { transform: translateY(0); }
}
.animated-box {
width: 100px;
height: 100px;
background-color: tomato;
animation: bounce 1s infinite;
}
</style>
<div class="animated-box"></div>
CSS variables (custom properties) store values that can be reused throughout your CSS. They're defined with --
prefix and accessed with var()
.
<style>
:root {
--main-bg: #222;
--main-color: #fff;
}
.theme-box {
background-color: var(--main-bg);
color: var(--main-color);
padding: 20px;
text-align: center;
}
</style>
<div class="theme-box">
This uses CSS variables!
</div>
CSS variables (custom properties) allow reuse of values and easy theming.
<style>
:root {
--main-color: #4CAF50;
--padding: 15px;
}
.box {
background-color: var(--main-color);
padding: var(--padding);
color: white;
}
</style>
<div class="box">Variable-powered Box</div>
CSS frameworks provide pre-written styles and components to speed up development. Examples include Bootstrap, Tailwind CSS, and Bulma.
<!-- Add Bootstrap CDN -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
<div class="container text-center mt-5">
<h1 class="text-primary">Hello from Bootstrap</h1>
<button class="btn btn-success">Click Me</button>
</div>
Attribute selectors target elements with specific attributes or values.
<style>
input[type="text"] {
border: 2px solid blue;
}
</style>
<input type="text" placeholder="Text input">
<input type="password" placeholder="Password input">
Pseudo-classes define a special state of an element, like :hover
, :first-child
, etc.
<style>
p:first-child {
color: red;
}
p:hover {
background-color: yellow;
}
</style>
<div>
<p>First Paragraph</p>
<p>Second Paragraph</p>
</div>
First Paragraph
Second Paragraph
CSS Grid Layout offers a two-dimensional layout system, ideal for complex designs.
<style>
.grid-container {
display: grid;
grid-template-columns: auto auto auto;
gap: 10px;
background-color: #ccc;
padding: 10px;
}
.grid-item {
background-color: #4CAF50;
padding: 20px;
color: white;
text-align: center;
}
</style>
<div class="grid-container">
<div class="grid-item">1</div>
<div class="grid-item">2</div>
<div class="grid-item">3</div>
</div>
Flexbox is a one-dimensional layout system to align items horizontally or vertically with ease.
<style>
.flex-container {
display: flex;
justify-content: space-around;
background-color: lightgray;
padding: 10px;
}
.flex-item {
background-color: royalblue;
color: white;
padding: 20px;
}
</style>
<div class="flex-container">
<div class="flex-item">Item 1</div>
<div class="flex-item">Item 2</div>
<div class="flex-item">Item 3</div>
</div>
Use high contrast colors, readable font sizes, and focus styles to help all users.
<style>
button:focus {
outline: 3px dashed red;
background-color: yellow;
}
</style>
<button>Accessible Button</button>
em
, rem
, %
over fixed px
.This course covered CSS basics to advanced topics including selectors, box model, layouts with flex/grid, transitions, animations, media queries, accessibility, and frameworks.
To make CSS development easier and more efficient, use these tools:
To take your skills further:
Functions like calc()
, min()
, and max()
provide dynamic styling.
<style>
.dynamic-width {
width: calc(100% - 100px);
background-color: coral;
padding: 10px;
}
</style>
<div class="dynamic-width">Using calc() to adjust width</div>
Clipping allows showing parts of an element in custom shapes.
<style>
.clipped {
clip-path: circle(50% at 50% 50%);
width: 200px;
height: 200px;
background-color: purple;
}
</style>
<div class="clipped"></div>
<style>
.fit-img {
width: 300px;
height: 200px;
object-fit: cover;
}
</style>
<img src="https://via.placeholder.com/400" class="fit-img">
<style>
.blend-box {
background: url('https://via.placeholder.com/300') center/cover;
background-blend-mode: multiply;
background-color: yellowgreen;
width: 300px;
height: 200px;
}
</style>
<div class="blend-box"></div>
<style>
.responsive-text {
font-size: clamp(1rem, 2vw, 3rem);
}
</style>
<p class="responsive-text">This text resizes with the viewport!</p>
This text resizes with the viewport!
<style>
.logical-box {
padding-inline-start: 20px;
margin-block-end: 20px;
background: peachpuff;
}
</style>
<div class="logical-box">Logical padding and margin</div>
<style>
ol.counter {
counter-reset: item;
}
ol.counter li::before {
counter-increment: item;
content: "Step " counter(item) ": ";
font-weight: bold;
}
</style>
<ol class="counter">
<li>First step</li>
<li>Second step</li>
</ol>
<style>
.shape-img {
float: left;
shape-outside: circle(50%);
clip-path: circle(50%);
width: 200px;
height: 200px;
margin: 10px;
}
</style>
<img src="https://via.placeholder.com/200" class="shape-img">
<p>Text wraps around the circle using shape-outside...</p>
Text wraps around the circle using shape-outside. You can create magazine-style layouts with this feature.
<style>
.scroll-area {
height: 100px;
overflow-y: scroll;
}
.scroll-area::-webkit-scrollbar {
width: 10px;
}
.scroll-area::-webkit-scrollbar-thumb {
background: green;
border-radius: 5px;
}
</style>
<div class="scroll-area">
<p>Lots of scrollable content here...</p>
<p>Line 2</p>
<p>Line 3</p>
<p>Line 4</p>
<p>Line 5</p>
<p>Line 6</p>
<p>Line 7</p>
</div>
Lots of scrollable content here...
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7