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

Inline Styles


Inline styles apply CSS rules directly to individual HTML elements using the style attribute. While quick for small adjustments, they are generally discouraged for larger projects due to maintainability issues and poor separation of concerns.

Example 1: Basic Inline Style

<p style="color: blue; font-size: 16px;">This text is styled with an inline style.</p>

Explanation This example demonstrates how to apply a color and font-size directly to a paragraph element using the style attribute. Inline styles override external or internal stylesheets for that specific element.


Example 2: Multiple Properties Inline

<div style="background-color: lightgray; padding: 20px; border: 1px solid black;">
  <h2>Inline Style Box</h2>
  <p>This div has a background, padding, and border applied inline.</p>
</div>

Explanation Here, a div element is styled with a background color, padding, and a border, showcasing how multiple CSS properties can be defined within a single style attribute. This is useful for quick visual adjustments.


Example 3: Overriding External Styles with Inline

<!DOCTYPE html>
<html>
<head>
<style>
  p {
    color: green;
  }
</style>
</head>
<body>
  <p>This text is green from external style.</p>
  <p style="color: red;">This text is red due to inline style.</p>
</body>
</html>

Explanation This example illustrates the cascade order, where an inline style on the second paragraph overrides the color: green; defined in the internal stylesheet, making the text red. Inline styles have high specificity.


Example 4: Inline Style with !important (Avoid if possible)

<p style="color: purple !important;">This text is purple, even if other styles try to override it.</p>

Explanation While generally discouraged, !important can be used with inline styles to forcefully apply a declaration, overriding almost any other CSS rule. Use it sparingly to avoid maintenance headaches.


Example 5: Inline Style for JavaScript Interaction

<button onclick="this.style.backgroundColor='yellow';">Click Me</button>

Explanation Inline styles can be dynamically manipulated by JavaScript, as shown here where a button's background color changes upon a click event. This provides immediate visual feedback.


Example 6: Basic Inline Image Styling

<img src="image.jpg" alt="A sample image" style="width: 150px; border-radius: 8px;">

Explanation This example applies a specific width and border-radius to an image element directly. It's a quick way to control the appearance of individual images without needing a separate stylesheet.


Example 7: Inline Styles for Form Elements

<input type="text" placeholder="Enter your name" style="border: 2px solid #ccc; padding: 10px;">

Explanation Here, an input field's border and padding are customized using inline styles. This method allows for immediate visual adjustments to form elements.