CSS

CSS Overview Selectors Declarations Rule Sets Comments Reset/Normalize Origin Importance Order Specificity Inheritance Cascade Layers Inline Styles Internal Styles External Styles @import Absolute Units Relative Units Dynamic Viewport Angle Units Time Units Frequency Units: Resolution Units Keywords Color Formats Color Functions & Spaces Background Color Background Image Background Repeat Background Position Background Size Background Attachment Background Shorthand Multiple Backgrounds Linear Gradients Radial Gradients Conic Gradients Font Family Font Size Font Weight Font Style Line Height Text Align Text Decoration Text Transform Letter Spacing Word Spacing Text Shadow Text Wrap Variable Fonts Content Box Padding Box Border Box Margin Box Box Sizing Property Margin Padding Border Outline Box Shadow Block Display Inline Display Inline Block Display Static Position Relative Position Absolute Position Fixed Position Sticky Position Top Element Position Right Element Position Bottom Element Position Left Element Position Z Index Flexbox Box Layout Display Flex Flex Direction Flex Wrap Justify Content Align Items Align Content Gap, Row Gap & Column Gap Flex Grow Flex Shrink Flex Basis Flex Shorthand Order Property Align Self Practical Applications CSS Grid Layout Grid Display Grid Template Columns Grid Template Rows Grid Template Areas Justify Items Grid Auto Columns Justify Self Nested Grids Responsive Web Design Min Width Max Width Orientation Logical Operators Pointer Hover Prefers Color Scheme Fluid Images Flexible Typography Viewport Width Advanced CSS Features Defining variables Using variables Scope & Inheritance of CSS Variables Property Value Fallback Practical Applications :Hover :Active :Focus :Focus Visible :Visited :Link :First Child :Last Child :Nth Child :Nth of Type :Empty :Checked :Disabled :Enabled :Valid :Invalid :Required :Has :Not :Is :Where ::Before Pseudo Element ::After Pseudo Element ::First Letter ::First Line ::Selection ::Marker CSS 2D Transformations CSS 3D Transformations Transform Origin Transform Style Backface Visibility Transition Property Transition Duration Transition Timing Function Transition Delay Transition Shorthand Ease Timing Function Linear Timing Function Ease In Timing Function Ease Out Timing Function Ease In Out Timing Function Cubic Bezier Function Animations (@keyframes) Defining @keyframes Animation Name CSS Animation Duration Animation Timing Function CSS Animation Delay CSS Animation Iteration Count Animations Direction CSS Animation Fill Mode CSS Animation Play State CSS Filter Blur CSS Filter Brightness CSS Filter Contrast() CSS Drop Shadow CSS Grayscale CSS Hue Rotate CSS Invert CSS Opacity CSS Saturate CSS Sepia Mix Blend Mode Background Blend Mode Object Fit & Object Position Scroll Snap Type Scroll Snap Align Scroll Padding Scroll Margin Scroll Triggered Animations JS Variables

Practical Applications


Centering Elements

Centering content is a fundamental layout task in CSS. Modern CSS offers powerful and flexible methods like Flexbox and Grid to achieve precise alignment, moving beyond older, more cumbersome techniques.

Example 1: Flexbox Centering

.flex-container {
  display: flex; /* Establishes a flex-container */
  justify-content: center; /* Centers horizontally */
  align-items: center; /* Centers vertically */
  height: 200px;
  background-color: #f0f0f0;
}

Explanation This code uses display: flex to create a flexbox layout. The justify-content: center property aligns the child element along the main axis (horizontally), and align-items: center aligns it along the cross axis (vertically).


Example 2: Grid Centering

.grid-container {
  display: grid; /* Defines a grid container */
  place-items: center; /* Centers horizontally and vertically */
  height: 200px;
  background-color: #f0f0f0;
}

Explanation The display: grid property turns an element into a grid container. The shorthand property place-items: center is then used to align the grid item in the center, both vertically and horizontally.


Example 3: Position and Transform

.parent {
  position: relative; /* Creates a positioning context */
  height: 200px;
  background-color: #f0f0f0;
}

.child {
  position: absolute; /* Positions relative to the parent */
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%); /* Adjusts position based on element size */
}

Explanation This classic method positions the child element 50% from the top and left of its relative parent. The transform: translate(-50%, -50%) then shifts the element back by half of its own width and height, achieving a perfect center.


Example 4: Margin Auto for Block Elements

.block-element {
  width: 50%; /* A specific width is required */
  margin-left: auto; /* Pushes the element from the left */
  margin-right: auto; /* Pushes the element from the right */
  background-color: #f0f0f0;
}

Explanation For horizontally centering a block-level element that has a defined width, you can set its left and right margins to auto. This tells the browser to automatically distribute the remaining horizontal space equally on both sides.


Navigation

Creating intuitive navigation is crucial for user experience. CSS provides the tools to build everything from simple horizontal link lists to complex, responsive navigation bars that adapt to any device.

Example 1: Simple Horizontal Navbar

.nav-horizontal ul {
  list-style-type: none; /* Removes bullet points */
  margin: 0;
  padding: 0;
  display: flex; /* Arranges items in a row */
  background-color: #333;
}

.nav-horizontal li a {
  display: block;
  color: white;
  padding: 14px 16px;
  text-decoration: none;
}

Explanation We use display: flex on the <ul> to create a horizontal layout for our list items. Basic padding and color styles are applied to the links to create a clean, functional navigation bar.


Example 2: Vertical Navigation Bar

.nav-vertical ul {
  list-style-type: none; /* Removes list bullets */
  padding: 0;
  width: 150px; /* Defines the width of the navigation */
  background-color: #f1f1f1;
}

.nav-vertical li a {
  display: block; /* Makes the entire area clickable */
  color: #000;
  padding: 8px 16px;
  text-decoration: none;
}

Explanation For a vertical navbar, we style the <ul> with a fixed width. By setting the <a> elements to display: block, they fill the available width, making the entire list item clickable.


Responsive Components

Responsive design ensures your website looks and functions well on all screen sizes. Using media queries, you can create flexible components like cards and grids that adapt their layout based on the viewport width.

Example 1: Responsive Card Component

.card {
  width: 100%; /* Full width on small screens */
  border: 1px solid #ccc;
  border-radius: 8px;
}

/* On screens 768px and wider, display cards side-by-side */
@media (min-width: 768px) {
  .card-container {
    display: flex;
    gap: 16px;
  }
  .card {
    width: 33.33%; /* Three cards per row */
  }
}

Explanation The card is full-width by default for mobile devices. A media query checks for a minimum screen width of 768px and then applies a flexbox layout to the container, placing the cards in a row.