The Magic Wand of Web Design
What is CSS?
Selectors
Typography
Basic Structure
The Box Model
Units
Backgrounds
Variables
# What is HTML?
CSS is a stylesheet language used to style and layout web pages. It controls the appearance of HTML elements by defining colors, fonts, spacing, and positioning. CSS also enables responsive designs, animations, and custom themes, making websites visually appealing and user-friendly. In short, it’s what turns your web pages from plain text to works of art!
# What is HTML?
# What is HTML?
# What is HTML?
# Basic Structure
The CSS document structure defines how CSS is written, organized, and applied to a web page.
# Basic Structure
style
attribute.<p style="color: blue; font-size: 20px;">This is styled text.</p>
2. Internal CSS: Defined within a <style>
tag inside the <head>
of the HTML document.
3. External CSS: Written in a separate .css
file and linked to the HTML document.
<link rel="stylesheet" href="css/style.css" />
CSS file mainly contain a set of elements called rules
The rules consists of selectors and declaration blocks
Selector: Specifies the HTML elements to style
Declaration Block: Contains one or more style declarations in {}
# Basic Structure
selector {
property: value;
}
h1 {
color: red;
}
# VSC Interface Overview
# VSC Interface Overview
* {
margin: 0;
padding: 0;
} /* Useful for resetting default styles. */
1. Universal Selector
p {
font-size: 16px;
color: #333;
} /* usefull To style all instances of an element similarly. */
2. Type Selector
# VSC Interface Overview
.button {
background-color: blue;
color: white;
} /* usefull For styling multiple elements with the same class. */
class
attribute: .classname
#header {
background-color: gray;
} /* usefull For unique elements like a header or footer. */
id: #idname
# VSC Interface Overview
h1, h2, h3 {
font-family: Arial, sans-serif;
}/* usefull To reduce repetitive code */
:
selector1, selector2, selector3
.container p {
color: green;
} /*To style elements within a specific section. */
ancestor descendant
# VSC Interface Overview
.menu > li {
list-style: none;
}
/* To avoid affecting nested elements.*/
parent > child
h1 + p {
margin-top: 10px;
} /* To style the first paragraph after a header.*/
element1 + element2
# VSC Interface Overview
h1 ~ p {
color: blue;
}
/* For styling all siblings of a specific element.*/
element1 ~ element2
# VSC Interface Overview
input[type="text"] {
border: 1px solid black;
}
/* For forms and specific attributes.*/
[attribute]
– Elements with a specific attribute.[attribute=value]
– Elements with an attribute of a specific value.[attribute^=value]
– Starts with a value.[attribute$=value]
– Ends with a value.[attribute*=value]
– Contains a value.# VSC Interface Overview
a:hover {
color: red;
}
selector:pseudo-class
:hover
– When the user hovers over an element.:nth-child(n)
– Targets the nth child of a parent.# VSC Interface Overview
p::first-line {
font-weight: bold;
}
selector::pseudo-element
::before
– Adds content before an element.::after
– Adds content after an element.# VSC Interface Overview
inline > ID > Class > selectors
# VSC Interface Overview
# Essential Extensions
# Attributes
# VSC Interface Overview
body {
font-family: "Arial", sans-serif;
}
font-family: "Font Name", fallback-font;
p {
font-size: 16px;
}
# VSC Interface Overview
h1 {
font-weight: bold; /* Or numeric values: 100 to 900 */
}
normal
(default)bold
lighter
bolder
# VSC Interface Overview
em {
font-style: italic;
}
font-style: style;
p {
line-height: 1.5; /* Relative to the font size */
}
# VSC Interface Overview
h1 {
text-align: center;
}
text-align: alignment;
left
(default)center
right
justify
# VSC Interface Overview
a {
text-decoration: none;
}
text-decoration: value;
none
(removes decoration)underline
overline
line-through
(strikethrough)# VSC Interface Overview
h2 {
text-transform: uppercase;
}
text-transform: value;
capitalize
(first letter of each word)uppercase
lowercase
# VSC Interface Overview
h1 {
letter-spacing: 2px;
}
p {
word-spacing: 5px;
}
# VSC Interface Overview
h1 {
text-shadow: 2px 2px 5px rgba(0, 0, 0, 0.3);
}
text-shadow: x-offset y-offset blur-radius color;
p {
color: #333;
}
# Customize Your VSCode
# Customize Your VSCode
CSS units define the size of elements, spacing, and other dimensions on a web page. Choosing the right unit is crucial for creating responsive, scalable, and visually appealing designs.
# VSC Interface Overview
p {
font-size: 16px;
}
div {
width: 5cm;
height: 2in;
}
p {
font-size: 12pt;
}
Absolute units are fixed and do not scale based on other elements. They are ideal for print designs but less flexible for responsive web designs
px (pixels), cm / mm, in (inches), pt (points)
# VSC Interface Overview
p {
font-size: 1.5em; /* 1.5 times the parent font size */
}
Relative units are flexible and adapt to the size of their context, making them ideal for responsive designs.
em
: Relative to the font size of the parent element.
# VSC Interface Overview
html {
font-size: 16px; /* Default */
}
h1 {
font-size: 2rem; /* 32px */
}
rem
: Relative to the root element's font size.
div {
width: 50%; /* Half of the parent container */
}
%
: Relative to the parent element.
Use Case | Best Unit |
---|---|
Fixed designs | px |
Scalable text | rem, em |
Responsive layouts | % |
Typography (print) | pt |
# Customize Your VSCode
# Customize Your VSCode
The background property in CSS is used to control the visual appearance of the area behind elements. From simple colors to stunning images and patterns, backgrounds can add depth and personality to your designs.
# VSC Interface Overview
body {
background-color: lightblue;
}
red
, blue
, green
, etc.#ff5733
rgb(255, 87, 51)
hsl(10, 100%, 50%)
# VSC Interface Overview
body {
background-image: url('background.jpg');
}
# VSC Interface Overview
div {
background-image: url('pattern.png');
background-repeat: no-repeat;
}
repeat
(default): Repeats both horizontally and vertically.repeat-x
: Repeats horizontally only.repeat-y
: Repeats vertically only.no-repeat
: Stops repetition.# VSC Interface Overview
div {
background-image: url('hero.jpg');
background-size: cover;
}
auto
(default): Keeps the image's original size.cover
: Scales the image to cover the element, maintaining aspect ratio.contain
: Scales the image to fit within the element.100px
, 50%
, etc.# VSC Interface Overview
div {
background-image: url('logo.png');
background-position: center center;
}
top
, center
, bottom
, left
, right
(or combinations like top left
).50px 100px
, 50% 50%
.# VSC Interface Overview
body {
background-image: url('parallax.jpg');
background-attachment: fixed;
}
scroll
(default): Background scrolls with the page.fixed
: Background stays fixed in place.local
: Background scrolls with the element's content# VSC Interface Overview
div {
background-color: yellow;
background-clip: padding-box;
}
border-box
: Extends to the border (default).padding-box
: Clips to the padding.content-box
: Clips to the content area.# VSC Interface Overview
div {
background: linear-gradient(to right, red, blue);
}
div {
background: radial-gradient(circle, red, yellow, green);
}
# VSC Interface Overview
div {
background: #ff5733 url('pattern.png') no-repeat center/contain fixed;
}
# Customize Your VSCode
# Customize Your VSCode
CSS variables (or Custom Properties) are a powerful CSS feature that allow you to store reusable values, such as colors, sizes or spacing. They make styles more modular, maintainable and dynamic.
# VSC Interface Overview
body {
background-color: lightblue;
}
red
, blue
, green
, etc.#ff5733
rgb(255, 87, 51)
hsl(10, 100%, 50%)
# Customize Your VSCode
# VSC Interface Overview
ul {
list-style-type: square;
}
ol {
list-style-type: upper-roman;
}
disc
(default for <ul>
)circle
, square
decimal
(default for <ol>
)upper-roman
, lower-roman
none
# VSC Interface Overview
ul {
list-style-position: inside;
}
outside
(default): Marker outside contentinside
: Marker aligned with textul {
list-style-image: url('custom-bullet.png');
}
# VSC Interface Overview
ul {
list-style: square inside;
}
# VSC Interface Overview
li {
color: #3498db;
font-size: 18px;
margin: 5px 0;
padding-left: 10px;
}
ul, ol {
list-style-type: none;
padding: 0;
margin: 0;
}
# VSC Interface Overview
ul ul {
list-style-type: circle;
padding-left: 20px;
}
ol ol {
list-style-type: lower-alpha;
padding-left: 20px;
}
ul li::before {
content: "✓";
color: green;
margin-right: 10px;
}
# Customize Your VSCode
# VSC Interface Overview
div { display: block; }
span { display: inline; }
button { display: inline-block; }
.hidden { display: none; }
.container { display: flex; }
.grid-container { display: grid; }
block
: Full width, new line (e.g., <div>
, <p>
)inline
: Only takes content width (e.g., <span>
)inline-block
: Inline but supports width/heightnone
: Hides the element completelyflex
: Enables Flexbox layoutgrid
: Enables CSS Grid layoutThe display property controls how an element is rendered in the layout.
# VSC Interface Overview
Allows other elements beside; margin, padding & width don’t work.
Takes up an entire line; margin, padding & width work.
# VSC Interface Overview
Allows other elements beside; margin, padding & width work. Can create columns, but will force a space between boxes.
# VSC Interface Overview
.visible { visibility: visible; }
.hidden-layout { visibility: hidden; }
visible
: Defaulthidden
: Invisible but still occupies spacecollapse
: Used for table rows/columnsControls whether an element is visible without affecting layout.
# VSC Interface Overview
Controls whether an element is visible without affecting layout.
Feature | display: none | visibility: hidden |
---|---|---|
Space occupied | No | Yes |
Interactivity | Not possible | Not possible |
Performance | Slower on frequent change | Faster |
# Customize Your VSCode
# VSC Interface Overview
.relative {
position: relative;
top: 10px;
left: 20px;
}
.absolute {
position: absolute;
top: 50px;
right: 20px;
}
.fixed {
position: fixed;
top: 0;
left: 0;
}
.sticky {
position: sticky;
top: 10px;
}
static
(default): Normal flowrelative
: Relative to its normal positionabsolute
: Positioned relative to nearest positioned ancestorfixed
: Relative to viewport (doesn't scroll)sticky
: Hybrid of relative and fixedControl the placement of elements using the position property.
# VSC Interface Overview
Move an element around based on coordinates.
Added to a parent element to reset absolute child’s coordinates.
# VSC Interface Overview
Forces an element to not move when the page is scrolled.
Control the stacking order of elements, higher number is closer.
# Customize Your VSCode
# VSC Interface Overview
.box {
width: 200px;
height: 100px;
overflow: auto;
border: 1px solid black;
}
visible
(default): Content overflowshidden
: Clipped without scrollbarsscroll
: Always show scrollbarsauto
: Show only if neededclip
: Similar to hidden
, but no access to overflowControls what happens when content overflows its container.
# VSC Interface Overview
div {
overflow-x: scroll;
overflow-y: hidden;
}
# Customize Your VSCode