CSS Tutorial
CSS Tutorial
CSS (Cascading Style Sheets) is used to style and design web pages. It controls the layout,
colors, fonts, spacing, and much more.
Separation of Content & Design: HTML handles structure, CSS handles styling.
Reusability: A single CSS file can style multiple web pages.
Faster Page Loading: Optimized CSS reduces page load time.
Better Accessibility: Improves UI/UX.
Types of CSS
<style>
p { color: blue; font-size: 18px; }
</style>
1. Content – The actual text, image, or other elements inside the box.
2. Padding – Space between the content and the border.
3. Border – The outline of the box.
4. Margin – Space between the box and adjacent elements.
Total Width = width + left padding + right padding + left border + right border + left margin + right margin
Total Height = height + top padding + bottom padding + top border + bottom border + top margin + bottom margin
Display and Visibility Properties in CSS:
Basic Terminology:
1. display
o display: flex; → Enables flexbox layout.
o display: inline-flex; → Enables flexbox but acts like an inline element.
2. flex-direction
Defines the main axis direction.
o row (default) → Left to right.
o row-reverse → Right to left.
o column → Top to bottom.
o column-reverse → Bottom to top.
3. flex-wrap
Determines whether items wrap or stay in a single line.
o nowrap (default) → No wrapping.
o wrap → Wrap onto multiple lines.
o wrap-reverse → Wrap but in reverse order.
4. flex-flow
Shorthand for flex-direction and flex-wrap.
o Example: flex-flow: row wrap;
5. justify-content
Aligns items along the main axis.
o flex-start (default) → Items start at the beginning.
o flex-end → Items align at the end.
o center → Items align in the center.
o space-between → Space between items, no space at edges.
o space-around → Equal space around all items.
o space-evenly → Equal space between and around all items.
6. align-items
Aligns items along the cross axis.
o stretch (default) → Items stretch to fill container height.
o flex-start → Aligns items at the start.
o flex-end → Aligns items at the end.
o center → Aligns items at the center.
o baseline → Aligns items by their text baselines.
7. align-content
Aligns multiple flex lines when flex-wrap is set.
o stretch (default) → Stretches to fill container.
o flex-start → Aligns at the start.
o flex-end → Aligns at the end.
o center → Aligns at the center.
o space-between → Equal space between lines.
o space-around → Equal space around lines.
Flex Item Properties:
These properties are applied to flex items inside the container.
8. order
Specifies item order (default is 0). Lower values appear first.
o Example: order: 2;
9. flex-grow
Defines how much an item should grow relative to others. Default is 0.
o Example: flex-grow: 1; (Item takes available space equally)
10. flex-shrink
Defines how much an item should shrink relative to others. Default is 1.
o Example: flex-shrink: 0; (Prevents shrinking)
11. flex-basis
Specifies the default size before growing/shrinking.
o Example: flex-basis: 200px; (Item starts with 200px width)
12. flex
Shorthand for flex-grow, flex-shrink, and flex-basis.
o Example: flex: 1 1 auto;
13. align-self
Aligns an individual item along the cross axis (overrides align-items).
o auto (default) → Inherits from align-items.
o flex-start → Aligns at start.
o flex-end → Aligns at end.
o center → Aligns at center.
o baseline → Aligns based on text baseline.
o stretch → Stretches to fill height.
Introduction to Media Queries
Media queries in CSS allow you to apply styles based on the device's characteristics such as
screen width, height, resolution, orientation, etc. They are essential for responsive web design.
Basic Syntax
A media query consists of:
2. Transition Syntax