CSS Notes -

Koustav MoitraKoustav Moitra
21 min read

Table of contents

CSS and its Syntax

CSS stands for Cascading Style Sheets.
It controls the visual presentation (style, layout, color, fonts, spacing, etc.) of HTML elements.

Why Use CSS?

Without CSS:

<h1>Hello</h1>

With CSS:

<h1 style="color: blue; font-family: Arial;">Hello</h1>


3 Ways to Add CSS

MethodHow to UseExample
InlineInside the HTML tag<p style="color:red;">Text</p>
InternalInside a <style> tag in <head><style>p { color: red; }</style>
External โœ…Linked .css file<link rel="stylesheet" href="styles.css">
๐Ÿ’ก
External is the best for real projects.

CSS Syntax

selector {
  property: value;
}

โœ… Example:

h1 {
  color: blue;
  font-size: 24px;
}

This means make all <h1> elements blue and 24px in size.


CSS Selectors

Selectors target which elements to style.

๐Ÿ”น 1. Universal Selector (*)

Targets all elements.

* {
  margin: 0;
  padding: 0;
}

๐Ÿ”น 2. Element Selector

Targets a specific HTML tag.

p {
  color: green;
}

๐Ÿ”น 3. Class Selector (.)

Targets elements with a specific class.

<p class="highlight">Hello</p>
.highlight {
  background-color: yellow;
}

๐Ÿ”น 4. ID Selector (#)

Targets an element with a unique ID.

<h1 id="main-title">Welcome</h1>
#main-title {
  font-size: 30px;
}

๐Ÿ”น 5. Group Selector

Style multiple elements at once.

h1, h2, p {
  font-family: Arial;
}

๐Ÿ”น 6. Descendant Selector

Select elements inside other elements.

article p {
  color: gray;
}

Applies to all <p> inside <article>.


๐Ÿ”น 7. Child Selector (>)

Selects direct children only.

ul > li {
  list-style-type: square;
}

Specificity (Which Style Wins?)

  1. Inline style

  2. Internal style

  3. External style


CSS Comment

CSS comments are lines of text that are ignored by the browser. They're used to explain code, leave notes, or temporarily disable CSS rules during development.

โœ… Syntax:

/* This is a CSS comment */
  • Comments start with /* and end with */.

  • They can span multiple lines.

๐Ÿ“Œ Examples:

/* This sets the background color of the body */
body {
  background-color: #f0f0f0;
}

/*
  This section styles the navigation bar.
  It includes padding and font color.
*/
nav {
  padding: 10px;
  color: white;
}

CSS Colors

โœ… 1. Color Names

CSS supports 147 predefined color names, like:

color: red;
color: blue;
color: lightgreen;
color: darkslategray;

Easy to use, but limited flexibility.


โœ… 2. HEX (Hexadecimal) Color

HEX is a base-16 value that defines red, green, and blue (RGB) using 2 hex digits each.

color: #RRGGBB;
  • #FF0000 โ†’ red

  • #00FF00 โ†’ green

  • #0000FF โ†’ blue

  • #FFFFFF โ†’ white

  • #000000 โ†’ black

Short form also works: #FFF (same as #FFFFFF)


โœ… 3. RGB (Red, Green, Blue)

Each color is defined using integer values (0โ€“255) for red, green, and blue:

color: rgb(255, 0, 0);    /* Red */
color: rgb(0, 255, 0);    /* Green */
color: rgb(0, 0, 255);    /* Blue */

โœ… 4. RGBA (RGB + Alpha Transparency)

Same as rgb(), but adds alpha (opacity) as a value from 0 (transparent) to 1 (opaque):

color: rgba(255, 0, 0, 0.5);   /* Semi-transparent red */

CSS Background

The background property in CSS is used to set the background effects for elements. It can control background color, images, repeat behavior, positioning, and more.

โœ… 1. background-color

Sets the background color of an element.

background-color: lightblue;

โœ… 2. background-image

Sets a background image using a URL.

background-image: url("image.jpg");

๐Ÿ“Œ Can be used with gradients too:

background-image: linear-gradient(to right, red, yellow);

โœ… 3. background-repeat

Controls if/how the image repeats.

background-repeat: repeat;        /* Default */
background-repeat: no-repeat;     /* Show once */
background-repeat: repeat-x;      /* Horizontal only */
background-repeat: repeat-y;      /* Vertical only */

โœ… 4. background-position

Sets the starting position of the background image.

background-position: center;
background-position: top right;
background-position: 50% 50%;

โœ… 5. background-attachment

Defines whether the background scrolls with the page.

background-attachment: scroll;   /* Default */
background-attachment: fixed;    /* Stays in place */

โœ… 6. background (Shorthand)

You can combine all background properties in a single line:

background: url("bg.jpg") no-repeat center/cover fixed;

Format:

background: [color] [image] [repeat] [position]/[size] [attachment];

CSS Units

CSS Units โ€“ Absolute & Relative

โœ… Absolute Units

These units are fixed and do not scale with screen size or parent elements. Best for print or very controlled layouts.

UnitDescriptionExample
pxPixels (screen dots)width: 100px;
ptPoints (1pt = 1/72 inch)font-size: 12pt;
cmCentimetersmargin: 2cm;
mmMillimeterspadding: 10mm;
inInches (1in = 2.54cm)width: 1in;
pcPicas (1pc = 12pt)font-size: 1pc;

Use Case: Print styles, very specific layouts.

โœ… Relative Units

These units scale depending on parent elements or the viewport. Best for responsive web design.

UnitRelative ToExample
%Parent elementwidth: 50%;
emFont size of the parentpadding: 2em;
remFont size of the root (<html>)font-size: 1.5rem;
vw1% of the viewport widthwidth: 80vw;
vh1% of the viewport heightheight: 50vh;
vmin1% of the smaller of vw or vhfont-size: 5vmin;
vmax1% of the larger of vw or vhpadding: 3vmax;
chWidth of the character "0"width: 40ch;
exx-height (height of lowercase "x")line-height: 2ex;

Use Case: Responsive layouts, scaling text, flexible designs.


โœ… Quick Comparison:

ScenarioUse Unit
Responsive layout%, vw, vh, rem
Font size (scalable)em, rem
Fixed-size elementspx
Print layoutcm, pt, in

Example:

html {
  font-size: 16px; /* 1rem = 16px */
}

.container {
  width: 80vw;
  padding: 2em;
  font-size: 1.2rem;
}

CSS Fonts

The font properties in CSS let you control the style, size, weight, family, and more for the text on your web page.

โœ… 1. font-family

Specifies the typeface (the font). Always provide fallback fonts.

๐Ÿ’ก
Fallback fonts are alternative fonts that the browser will use if the primary font is not available.
font-family: 'Roboto', Arial, sans-serif;
  • Custom font: 'Roboto' (loaded via @font-face or Google Fonts)

  • Fallback: Arial

  • Generic family: sans-serif

๐ŸŽฏ Common generic families:

  • serif โ€“ like Times New Roman

  • sans-serif โ€“ like Arial

  • monospace โ€“ like Courier

  • cursive, fantasy, system-ui

โœ… 2. font-size

Defines the size of the text.

font-size: 16px;
font-size: 1.2em;
font-size: 2rem;

Relative units (em, rem, %) make your text responsive.

โœ… 3. font-weight

Controls the boldness of the font.

font-weight: normal;     /* 400 */
font-weight: bold;       /* 700 */
font-weight: 100โ€“900;    /* Numeric scale */

โœ… 4. font-style

Defines the style (italic or normal).

font-style: normal;
font-style: italic;
font-style: oblique;

โœ… 5. font-variant

Used for small caps:

font-variant: small-caps;

CSS Text

These properties help you control the appearance and layout of text, including alignment, spacing, decoration, and transformation.

โœ… 1. color

Sets the text color.

color: red;
color: #333;
color: rgb(255, 0, 0);

โœ… 2. text-align

Aligns the text horizontally.

text-align: left;     /* Default */
text-align: right;
text-align: center;
text-align: justify;

โœ… 3. text-decoration

Adds decorations like underline, line-through, etc.

text-decoration: none;
text-decoration: underline;
text-decoration: line-through;
text-decoration: overline;

Combine with color:

text-decoration: underline red;

โœ… 4. text-transform

Changes the case of the text.

text-transform: none;
text-transform: uppercase;
text-transform: lowercase;
text-transform: capitalize;

โœ… 5. letter-spacing

Adjusts space between characters.

letter-spacing: 2px;

โœ… 6. word-spacing

Adjusts space between words.

word-spacing: 5px;

โœ… 7. line-height

Controls space between lines of text.

line-height: 1.5;

โœ… 8. Text Shadow

Adds shadow to text.

text-shadow: 2px 2px 5px gray;

What is box-sizing in CSS?

The box-sizing property defines how the total width and height of an element is calculated โ€” whether or not it includes padding and border.

By Default (content-box):

box-sizing: content-box;
  • Only the content is counted in the width and height.

  • Padding and border are added outside the defined width/height.

Example:

width: 300px;
padding: 20px;
border: 10px solid black;

Total width = 300 + 20*2 + 10*2 = 360px
Total height = same calculation


โœ… box-sizing: border-box;

box-sizing: border-box;
  • The width and height include content + padding + border.

  • No need to manually calculate extra space.

Example:

width: 300px;
padding: 20px;
border: 10px solid black;

Here, total width stays 300px.
Content area is automatically shrunk to fit the padding and border.


๐Ÿ“Œ Why Use border-box?

  • Keeps layout simple and predictable โœ…

  • Prevents overflow and alignment issues โœ…

  • Widely used in modern CSS frameworks โœ…


Best Practice (Set Globally):

* {
  box-sizing: border-box;
}

This sets all elements (including pseudo-elements) to calculate width and height in a consistent, easier-to-manage way.


Box Model

The CSS box model is essentially a box that wraps around every HTML element. It consists of: content, padding, borders and margins.

In CSS, the term "box model" is used when talking about design and layout.

box model

  • Content - The content of the box, where text and images appear

  • Padding - Clears an area around the content. The padding is transparent

  • Border - A border that goes around the padding and content

Margin - Clears an area outside the border. The margin is transparent

๐Ÿ”น 1. Content Area

  • Controls the actual content's dimensions.
PropertyDescriptionExample
widthSets the width of the contentwidth: 300px;
heightSets the height of the contentheight: 150px;
min-width, max-widthSet limits on widthmax-width: 500px;
min-height, max-heightSet limits on heightmin-height: 100px;
overflowControls content overflowoverflow: hidden;
displayAffects box behaviordisplay: block;

๐Ÿ”น 2. Padding Area

  • Space between content and border.
PropertyDescriptionExample
paddingShorthand for all sidespadding: 20px;
padding-topTop paddingpadding-top: 10px;
padding-rightRight paddingpadding-right: 15px;
padding-bottomBottom paddingpadding-bottom: 10px;
padding-leftLeft paddingpadding-left: 15px;

๐Ÿ”น 3. Border Area

  • The visible edge surrounding padding and content.
PropertyDescriptionExample
borderShorthand for width, style, colorborder: 1px solid black;
border-widthControls thicknessborder-width: 2px;
border-styleControls style (solid, dashed, etc.)border-style: dashed;
border-colorSets the border colorborder-color: red;
border-top, border-right, border-bottom, border-leftIndividual bordersborder-top: 2px dotted blue;
border-radiusRounds cornersborder-radius: 10px;

๐Ÿ”น 4. Margin Area

  • Space outside the border, between this element and others.
PropertyDescriptionExample
marginShorthand for all sidesmargin: 10px;
margin-topTop marginmargin-top: 5px;
margin-rightRight marginmargin-right: 10px;
margin-bottomBottom marginmargin-bottom: 5px;
margin-leftLeft marginmargin-left: 10px;
margin: autoCenters an element (used with width)margin: auto;

๐Ÿ”น 5. Box Behavior / Control

  • Properties that influence how the box behaves.
PropertyDescriptionExample
box-sizingDefines how width/height are calculatedbox-sizing: border-box;
displayBlock, inline, flex, grid, etc.display: flex;
visibilityShow/hide the boxvisibility: hidden;
overflowWhat happens if content overflowsoverflow: scroll;
outlineA line outside the border (doesnโ€™t affect box size)outline: 2px dotted red;

โœ… Box Model Shorthand Example

.box {
  width: 200px;
  height: 150px;
  padding: 20px;
  margin: 10px;
  border: 2px solid black;
  box-sizing: border-box;
}

Display in CSS

The display property specifies the display behavior (the type of rendering box) of an element.

โœ… block

  • Takes up 100% width of the parent (whole line).

  • Always starts on a new line.

  • You can set width and height.

Example elements: <div>, <p>, <h1>

p {
  display: block;
}

Makes the paragraph take full width and start on a new line.

โœ… inline

  • Takes up only as much width as needed.

  • Sits side by side with other inline elements.

  • You cannot set width or height.

Example elements: <span>, <a>, <strong>

a {
  display: inline;
}

Makes the anchor behave like text โ€” no line break, no width/height control.

โœ… inline-block

  • Behaves like inline (sits next to others).

  • But you can set width and height.

Best of both inline and block.

button {
  display: inline-block;
  width: 150px;
  height: 40px;
}

Button stays inline with others but can have a set width and height.

Common Use Cases

TaskUse this display
Stack items verticallyblock
Place items side-by-sideinline or inline-block
Custom buttons or links with padding & widthinline-block

CSS Gradients

Gradients let you transition between two or more colors smoothly โ€” and they're generated by the browser, so no image files needed!

โœ… 1. Linear Gradient

Color changes in a straight line (top to bottom by default).

background: linear-gradient(direction, color1, color2, ...);

Examples:

background: linear-gradient(to right, red, blue);
/* From left to right */
background: linear-gradient(to bottom right, #ff7e5f, #feb47b);
/* Diagonal gradient */
background: linear-gradient(45deg, #00c6ff, #0072ff);
/* Using degree */

โœ… 2. Radial Gradient

Color radiates outward from a central point (like a circle or ellipse).

background: radial-gradient(shape size at position, color1, color2, ...);

Examples:

background: radial-gradient(circle, red, yellow, green);
background: radial-gradient(ellipse at center, #ff9a9e, #fad0c4);

Box Shadow & Drop Shadow

  1. box-shadow (for elements)

Adds shadow around the border of an element โ€” often used to create depth or elevation.

โœ… Syntax:

box-shadow: offset-x offset-y blur-radius spread-radius color;
  • offset-x โ†’ Horizontal position (right if positive, left if negative)

  • offset-y โ†’ Vertical position (down if positive, up if negative)

  • blur-radius (optional) โ†’ Soften the edges

  • spread-radius (optional) โ†’ Expand or shrink the shadow

  • color โ†’ Shadow color

Example:

box-shadow: 5px 5px 15px rgba(0, 0, 0, 0.3);

Multiple Shadows:

You can stack shadows with commas:

box-shadow: 2px 2px 4px red, -2px -2px 4px blue;

Inset Shadow:

To apply shadow inside the element:

box-shadow: inset 2px 2px 5px rgba(0,0,0,0.5);
  1. filter: drop-shadow() (for images, SVGs, elements)

drop-shadow() is part of the filter property and applies a shadow to the visual content (not the box), useful especially for transparent images, text, or SVGs.

โœ… Syntax:

filter: drop-shadow(offset-x offset-y blur-radius color);

No spread-radius, but it respects transparent areas of PNGs or SVGs!

Example:

filter: drop-shadow(4px 4px 6px rgba(0,0,0,0.5));

Key Differences

Featurebox-shadowdrop-shadow()
Used onAny HTML elementElements, images, SVGs
Applies toThe box itself (border area)The rendered image or content
Spread controlYesNo
Inset optionYes (inset)No
Transparent imagesNo proper shadowโœ… Perfect for transparent PNG/SVG
Multiple shadowsโœ… With commasโœ… Only with multiple filters

List in CSS

Used to style HTML lists (<ul>, <ol>, <li>).

โœ… 1. list-style-type

Defines the type of marker (bullet or number) for list items.

ul {
  list-style-type: circle;
}

Common values:

  • disc (default bullet)

  • circle

  • square

  • decimal (numbers for <ol>)

  • none (no bullet)

โœ… 2. list-style-position

Sets where the marker appears โ€” inside or outside the list item's content.

ul {
  list-style-position: inside;
}
  • outside (default) โ€” bullet is outside the content box

  • inside โ€” bullet is inside the content box

โœ… 3. list-style-image

Replaces the default marker with a custom image.

ul {
  list-style-image: url('bullet.png');
}

Anchor Link States (Pseudo-classes)

Anchor tags (<a>) have 4 main states, often remembered as LVHA:

  • Unvisited link (default state)
a:link {
  color: blue;
}

โœ… 2. :visited

  • Link that the user has already clicked
a:visited {
  color: purple;
}

โœ… 3. :hover

  • When the user hovers over the link
a:hover {
  text-decoration: underline;
  color: red;
}

โœ… 4. :active

  • When the link is being clicked (mouse down)
a:active {
  color: orange;
}

Order Matters!

Always use this order:

a:link
a:visited
a:hover
a:active

(LVHA โ€“ Link, Visited, Hover, Active)

Otherwise, some states might not work as expected.


Pseudo Elements in CSS

1. ::before

  • Inserts content before the elementโ€™s actual content.
h1::before {
  content: "๐Ÿ”ฅ ";
}

2. ::after

  • Inserts content after the elementโ€™s content.
h1::after {
  content: " โœจ";
}

You must use content: with ::before and ::after.

3. ::first-letter

  • Styles the first letter of a block of text.
p::first-letter {
  font-size: 2rem;
  color: red;
}

4. ::first-line

  • Styles the first line of text in a block (depends on screen width).
p::first-line {
  font-weight: bold;
}

Position in CSS

The position property defines how an element is positioned in the document flow.

1. static (default)

  • Normal document flow

  • You canโ€™t use top, right, bottom, left

  • Used when no special positioning is needed.

  • Most HTML elements are static by default.

๐Ÿ“Œ Use when: You donโ€™t need to move the element manually.

div {
  position: static;
}

2. relative

  • Element stays in its normal position

  • You can move it with top, left, etc. relative to itself

  • Use when you want to move an element slightly from its normal position.

  • Can also be used as a reference for absolute children.

๐Ÿ“Œ Use when:

  • You want to nudge an element without breaking layout.

  • You want to position a tooltip or badge over a parent element.

Tip: Combine with z-index to bring things forward or behind.

div {
  position: relative;
  top: 10px;
  left: 20px;
}

3. absolute

  • Removed from normal flow

  • Positioned relative to the nearest positioned ancestor

  • If no positioned ancestor โ†’ relative to <html> or body

  • Use when you want to place something at a precise position.

  • It removes the element from the normal layout.

๐Ÿ“Œ Use when:

  • Creating dropdown menus.

  • Positioning tooltips or popups.

  • Designing modal dialogs or floating boxes.

๐Ÿ’ก Needs a parent with position: relative or it will stick to the <body>.

div {
  position: absolute;
  top: 0;
  left: 0;
}

4. fixed

  • Stays in one place even when scrolling

  • Positioned relative to the viewport

  • Use to pin an element to a specific spot on the screen โ€” even when scrolling.

๐Ÿ“Œ Use when:

  • You want a floating button (like a chat icon).

  • Creating sticky footers/headers that never move.

  • Displaying persistent alerts or banners.

div {
  position: fixed;
  bottom: 10px;
  right: 10px;
}

5. sticky

  • Switches between relative and fixed based on scroll

  • Sticks to a position when a threshold is reached

  • Use to make elements stick temporarily while scrolling, then release.

๐Ÿ“Œ Use when:

  • Creating sticky headers or section titles.

  • Sidebar menus that scroll with the content up to a point.

๐Ÿ’ก Needs a top, left, or bottom value and scrollable parent.

div {
  position: sticky;
  top: 0;
}

CSS z-index Property

  • Controls the stacking order of elements

  • Higher z-index = placed on top

  • Works only with positioned elements (relative, absolute, fixed, sticky)

  • Use when elements overlap and you want to control which one appears on top.

๐Ÿ“Œ Use when:

  • Creating modals, popups, or tooltips.

  • Fixing dropdown menus behind other elements.

  • Overlapping images or buttons.

๐Ÿ”ข Higher z-index means it sits on top.

div {
  position: absolute;
  z-index: 10;
}

Example:

.box1 {
  z-index: 1;
}
.box2 {
  z-index: 2;
}

box2 will appear above box1.

Summary Table

Position TypeIn Normal Flow?Can Use Top/Left?Relative To
staticโœ…โŒN/A
relativeโœ…โœ…Itself
absoluteโŒโœ…Nearest ancestor with position
fixedโŒโœ…Viewport
stickyโœ… (until sticky)โœ…Scroll position

Flex Box -

Flexbox (Flexible Box) is a one-dimensional layout system that helps you align and distribute space among items in a single directionโ€”either row or column.

Basic Terminology

  • Flex Container: The parent element with display: flex.

  • Flex Items: The child elements inside the flex container.

Enabling Flex

<div class="flex-container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
</div>
.flex-container {
  display: flex;
  background-color: #ddd;
}

.item {
  background-color: #4CAF50;
  color: white;
  padding: 20px;
  margin: 5px;
}

Main Axis vs Cross Axis

  • Main Axis = direction of flex (default: row โ†’ horizontal)

  • Cross Axis = perpendicular to main axis

Common Properties on Container

1. flex-direction

Controls main axis direction.

.flex-container {
  display: flex;
  flex-direction: row; /* default */
}

Other options:

  • row-reverse

  • column

  • column-reverse


2. justify-content

Aligns items on the main axis.

.flex-container {
  justify-content: center;
}

Other options:

  • flex-start

  • flex-end

  • space-between

  • space-around

  • space-evenly


3. align-items

Aligns items on the cross axis.

cssCopyEdit.flex-container {
  align-items: center;
}

Options:

  • flex-start

  • flex-end

  • stretch

  • baseline


4. flex-wrap

Makes items wrap to next line.

.flex-container {
  flex-wrap: wrap;
}

Properties on Flex Items

1. flex

Shorthand for flex-grow, flex-shrink, flex-basis.

.item {
  flex: 1;
}

โœ… Makes all items share available space equally.


2. order

Controls display order.

.item {
  order: 2;
}

Key Flexbox Use Cases

โœ… Navigation bars
โœ… Centering elements
โœ… Equal height columns


GRID

CSS Grid is a two-dimensional layout system for building complex layouts with rows and columns.

Basic Terminology

  • Grid Container: Parent with display: grid

  • Grid Items: Children of grid container

Enabling Grid

<div class="grid-container">
  <div class="item">1</div>
  <div class="item">2</div>
  <div class="item">3</div>
  <div class="item">4</div>
</div>
.grid-container {
  display: grid;
  grid-template-columns: 100px 100px;
  grid-template-rows: 100px 100px;
  gap: 10px;
}

.item {
  background-color: #2196F3;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
}

โœ… Result: 2x2 grid with 10px gaps.


โœ… Define Columns with grid-template-columns

grid-template-columns: 1fr 2fr;
  • fr = fraction of available space

โœ… Define Rows

grid-template-rows: 100px 200px;

โœ… gap

Space between grid cells.

gap: 20px;

โœ… Placing Items

.item1 {
  grid-column: 1 / 3;
}

โœ… Makes item span 2 columns.

Key Grid Use Cases

โœ… Page layouts
โœ… Photo galleries
โœ… Complex, two-dimensional designs


CSS Animation

Animation in CSS allows elements to move, change color, grow/shrink, rotate, and more โ€” without JavaScript.

Two Ways to Animate in CSS

  1. Transitions โ€“ Simple changes between two states (hover, focus, etc.)

  2. Animations โ€“ More control, keyframes, multiple steps

CSS TRANSITIONS

A transition is used to animate properties smoothly when they change (e.g., width, background, transform, etc.)

Example: Button Hover

<!DOCTYPE html>
<html>
<head>
  <style>
  div {
  width: 100px;
  height: 100px;
  background: red;
  transition: width 2s;
}

div:hover {
  width: 300px;
}
  </style>
</head>
<body>
       <div></div>
</body>
</html>

Common timing functions: ease, linear, ease-in, ease-out

linear - Same speed throughout

ease - Slow start โ†’ Fast middle โ†’ Slow end

ease-in - Slow start โ†’ Fast end

ease-out - Slow end


CSS ANIMATIONS

CSS @keyframes define how an element will animate step-by-step.

Example: Bouncing Ball

<!DOCTYPE html>
<html>
<head>
  <style>
    .ball {
      width: 50px;
      height: 50px;
      background-color: red;
      border-radius: 50%;
      position: relative;
      animation: bounce 1s infinite;
    }

    @keyframes bounce {
      0%   { top: 0; }
      50%  { top: 100px; }
      100% { top: 0; }
    }
  </style>
</head>
<body>
  <div class="ball"></div>
</body>
</html>
  • animation-name, animation-duration, animation-iteration-count, animation-timing-function

  • infinite = keeps repeating

Animation Properties

PropertyDescription
animation-nameName of the @keyframes
animation-durationTime it takes for one cycle
animation-timing-functionSpeed curve (ease, linear, etc.)
animation-delayWait time before starting
animation-iteration-countHow many times it runs
animation-directionNormal, reverse, alternate
animation-fill-modeforwards, backwards, etc.

Example: Fading Text In and Out

<!DOCTYPE html>
<html>
<head>
  <style>
    .fade {
      font-size: 24px;
      animation: fadeinout 2s infinite;
    }

    @keyframes fadeinout {
      0%   { opacity: 0; }
      50%  { opacity: 1; }
      100% { opacity: 0; }
    }
  </style>
</head>
<body>
  <p class="fade">I fade in and out!</p>
</body>
</html>
0
Subscribe to my newsletter

Read articles from Koustav Moitra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Koustav Moitra
Koustav Moitra