101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    Advance CSS

    Receive aemail containing the next unit.
    • CSS Grid Layout
      • 1.1Introduction to grid-based systems
      • 1.2Working with rows and columns
      • 1.3Creating complex, responsive designs
    • CSS Flexbox
      • 2.1Flexbox techniques: alignment and layout
      • 2.2Flexbox controls for UI components
    • Responsive Web Design
      • 3.1Implementing media queries
      • 3.2Employing fluid design principles
    • CSS Preprocessors
      • 4.1Introduction to CSS preprocessors: Sass, Less, and Stylus
      • 4.2Streamlining styling with variables, mixins, and functions
    • CSS Custom Properties (Variables)
      • 5.1Incorporating custom properties in design
      • 5.2Crafting dynamic and themeable styles
    • CSS-in-JS Libraries
      • 6.1Survey of CSS-in-JS solutions
      • 6.2Scoped and encapsulated styles for components
    • Animations and Transitions
      • 7.1Keyframes animations with CSS
      • 7.2Implementing engaging transitions
    • Advanced Selectors and Pseudo-classes
      • 8.1Understanding pseudo-classes
      • 8.2Styling with advanced selectors
    • CSS Architecture and Methodologies
      • 9.1Introduction to BEM and SMACSS
      • 9.2Structuring scalable, organized CSS
    • CSS Transforms and 3D Effects
      • 10.1Introducing CSS transforms
      • 10.2Creating engaging visual effects
    • CSS Filters and Blend Modes
      • 11.1Understanding and applying CSS filters
      • 11.2Creating unique compositions using blend modes
    • Web Fonts and Icon Fonts
      • 12.1Integrating custom web fonts and icon fonts
      • 12.2Enhancing typography and visual elements
    • Accessibility and Inclusive Design
      • 13.1Understanding ARIA roles and attributes
      • 13.2Designing for diverse user needs

    CSS Custom Properties (Variables)

    Using CSS Custom Properties in Your Stylesheets

    convention for representing and interacting with objects in HTML, XHTML and XML documents

    Convention for representing and interacting with objects in HTML, XHTML and XML documents.

    CSS custom properties, often referred to as CSS variables, are entities defined by CSS authors that contain specific values to be reused throughout a document. They are set using custom property notation (e.g., --main-color: black;) and are accessed using the var() function (e.g., color: var(--main-color);).

    How to Use CSS Custom Properties

    To use a CSS custom property, you first need to declare it. This is typically done in the :root pseudo-class, which represents the root of the document.

    :root { --main-bg-color: coral; }

    Once declared, you can use the custom property throughout your stylesheet using the var() function.

    body { background-color: var(--main-bg-color); }

    Scope and Inheritance of CSS Custom Properties

    One of the key features of CSS custom properties is their ability to inherit. If you define a custom property on a parent element, it will be available on all child elements.

    :root { --main-color: black; } body { color: var(--main-color); /* black */ } div { color: var(--main-color); /* black */ }

    The scope of a CSS custom property is determined by where it is declared. If it's declared in the :root, it's available globally. If it's declared inside a selector, it's available within that selector and its descendants.

    :root { --global-color: black; } body { --body-color: white; color: var(--global-color); /* black */ } div { color: var(--body-color); /* white */ }

    Overriding Custom Properties

    CSS custom properties can be easily overridden by simply redeclaring them within a specific scope.

    :root { --main-color: black; } body { --main-color: white; color: var(--main-color); /* white */ } div { color: var(--main-color); /* white */ }

    In the example above, --main-color is redeclared in the body selector, changing the value from black to white. This change affects the body and any child elements, like the div.

    Understanding how to use CSS custom properties can greatly enhance your ability to create dynamic, adaptable stylesheets. They offer a powerful way to control and adjust your design in a consistent and efficient manner.

    Test me
    Practical exercise
    Further reading

    Good morning my good sir, any questions for me?

    Sign in to chat
    Next up: Survey of CSS-in-JS solutions