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

Angle Units


Understanding these units is crucial for creating dynamic and visually appealing web designs, especially with features like transforms, gradients, and animations. Mastering them ensures precise control over rotations and directional properties.

Angle Units: deg, grad, rad, turn

CSS provides several units for specifying angles, each offering a different scale for convenience. These units are essential for properties such as rotate() in transform, conic-gradient(), and animation. Choosing the right unit can make your CSS more readable and easier to manage.


Example 1: Using deg (Degrees)

deg represents degrees, where a full circle is 360 degrees. It's the most commonly understood angle unit, making it intuitive for many developers.

.rotate-element-deg {
  transform: rotate(45deg); /* Rotates the element by 45 degrees clockwise */
  background-color: lightblue;
  width: 100px;
  height: 100px;
}

Explanation This example demonstrates rotating an element by 45 degrees using the deg unit. It's straightforward and widely used for its familiarity.


Example 2: Using grad (Gradians)

grad represents gradians, where a full circle is 400 gradians. This unit is less common in web development but is sometimes used in specific mathematical or engineering contexts.

.rotate-element-grad {
  transform: rotate(100grad); /* Rotates the element by 100 gradians (equivalent to 90 degrees) */
  background-color: lightcoral;
  width: 100px;
  height: 100px;
}

Explanation Here, we rotate an element by 100 gradians, which achieves a 90-degree rotation. While less common, grad offers an alternative scale for angle definition.


Example 3: Using rad (Radians)

rad represents radians, where a full circle is 2π radians (approximately 6.2832 radians). Radians are fundamental in mathematics and physics, often preferred for calculations and when working with trigonometric functions.

.rotate-element-rad {
  transform: rotate(0.7854rad); /* Rotates the element by 0.7854 radians (equivalent to 45 degrees, which is PI/4) */
  background-color: lightgreen;
  width: 100px;
  height: 100px;
}

Explanation This code snippet rotates an element by approximately 0.7854 radians, which is equivalent to 45 degrees. Radians are often used for more precise, mathematical angle definitions.


Example 4: Using turn (Turns)

turn represents turns, where one turn is a full circle. This unit is highly intuitive for rotations, as 0.25turn is a quarter rotation, 0.5turn is a half rotation, and so on.

.rotate-element-turn {
  transform: rotate(0.25turn); /* Rotates the element by a quarter turn (equivalent to 90 degrees) */
  background-color: lightsalmon;
  width: 100px;
  height: 100px;
}

Explanation This example shows a rotation of 0.25 turns, which simplifies expressing quarter or half rotations. The turn unit is very readable for simple rotational values.


Example 5: Combining Angle Units in conic-gradient()

Angle units are also critical for defining color stop positions in conic-gradient(), allowing for complex and vibrant circular gradients.

.conic-gradient-angles {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  background: conic-gradient(red 0deg, red 90deg, blue 90deg, blue 180deg, green 180deg, green 270deg, yellow 270deg, yellow 360deg);
  /* Creates a quartered circle using different angles */
}

Explanation This creates a conic gradient using deg to define four distinct color segments, demonstrating the power of angle units in visual effects. It showcases how different angles define the sweep of the gradient.


Example 6: Animating Rotation with deg

Angle units are fundamental for CSS animations, enabling smooth rotations and transformations over time.

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg); /* Full rotation using degrees */
  }
}

.animated-element {
  width: 80px;
  height: 80px;
  background-color: purple;
  animation: spin 3s linear infinite; /* Animates a continuous spin */
}

Explanation This code defines a simple spin animation that rotates an element 360 degrees using deg for a continuous loop. It highlights the use of angle units in CSS animations.


Example 7: Using rad for Scientific Contexts (Implicit)

While less common directly in everyday CSS, radians are the underlying unit for many mathematical functions that might influence CSS properties in advanced scenarios (e.g., via JavaScript calculations).

/*
  This example is conceptual as direct CSS usage of PI for radians is implicit.
  However, if you were to calculate an angle in JavaScript and apply it:
*/

/* Assume JavaScript calculated angleInRadians = Math.PI / 2; */
/* Then you would apply it to CSS like: */
.js-calculated-rotate {
  transform: rotate(1.5708rad); /* Equivalent to 90 degrees (PI/2 radians) */
  background-color: darkorange;
  width: 100px;
  height: 100px;
}

Explanation This example illustrates a scenario where radians might be used, often after a mathematical calculation in JavaScript, then applied to CSS. It reinforces the importance of understanding radians for more complex, calculated transformations.