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

Background Color


The background-color property in CSS is used to set a solid color for the background of an HTML element. This color fills the entire content, padding, and border areas of the element, extending to the edge of the border. It's a simple yet powerful way to visually distinguish different sections of a webpage.

Example 1: Setting a Solid Color by Name

/* style.css */
.element-one {
  /* Sets the background color to a predefined color name */
  background-color: steelblue;
  color: white; /* Sets the text color to white for better contrast */
  padding: 20px;
}

Explanation

This code targets an element with the class .element-one and changes its background to a light shade of blue called steelblue. Color names are a straightforward way to apply colors in CSS.


Example 2: Using Hexadecimal Color Values

/* style.css */
.element-two {
  /* Sets the background color using a hex code for a specific shade of green */
  background-color: #2E8B57;
  color: #FFFFFF; /* White text color using its hex code */
  padding: 20px;
}

Explanation

Hexadecimal (hex) color codes offer a much wider range of colors than color names. The code #2E8B57 represents a specific shade of sea green, providing more precise design control.


Example 3: Applying RGB Color Values

/* style.css */
.element-three {
  /* Sets the background color using the RGB color model */
  background-color: rgb(218, 112, 214);
  color: rgb(0, 0, 0); /* Black text color for readability */
  padding: 20px;
}

Explanation

The rgb() function defines a color by its red, green, and blue components, with each value ranging from 0 to 255. This example creates an orchid color by mixing 218 parts red, 112 parts green, and 214 parts blue.


Example 4: Using RGBA for Transparency

/* style.css */
.element-four {
  /* Sets a semi-transparent red background color using RGBA */
  background-color: rgba(255, 0, 0, 0.5);
  color: black;
  padding: 20px;
}

Explanation

The rgba() function adds an alpha channel to rgb(), allowing for transparency. The fourth value (alpha) ranges from 0 (fully transparent) to 1 (fully opaque); here, 0.5 creates a 50% transparent red background.


Example 5: HSL Color Values

/* style.css */
.element-five {
  /* Sets the background using the HSL color model for a vibrant blue */
  background-color: hsl(240, 100%, 50%);
  color: white;
  padding: 20px;
}

Explanation

HSL stands for Hue, Saturation, and Lightness. This is often more intuitive for making color adjustments; hsl(240, 100%, 50%) defines a pure, fully saturated blue.


Example 6: HSLA for Hue, Saturation, Lightness, and Alpha

/* style.css */
.element-six {
  /* Sets a semi-transparent yellow-green background using HSLA */
  background-color: hsla(80, 61%, 50%, 0.7);
  color: black;
  padding: 20px;
}

Explanation

Similar to rgba(), hsla() adds an alpha channel for transparency to the HSL color model. This example creates a yellow-green background that is 70% opaque.


Example 7: The transparent Keyword

/* style.css */
.element-seven {
  /* The parent element has a background image */
  background-image: url('https://via.placeholder.com/150/0000FF/808080');
}
.child-element {
  /* Makes the child's background transparent to show the parent's background */
  background-color: transparent;
  color: white;
  padding: 20px;
}

Explanation

The transparent keyword makes the background of an element completely see-through. This is useful for child elements when you want the parent element's background to be visible.