Search⌘ K

CSS and Styles in Astro

Explore how to apply various CSS techniques within Astro components, including scoped, global, conditional, and inline styles. Understand how to keep styles encapsulated or shared, use conditional classes, and integrate dynamic variables from frontmatter for flexible and maintainable design. This lesson equips you to style Astro projects effectively while maintaining code clarity.

Astro components were created to be capable of handling CSS right inside each component. Styling can be done through the use of the style tags, making it easy to keep styles associated with a component encapsulated in a single file. In this lesson, we’ll take a look at the following types of CSS:

  • Scoped CSS: This is CSS that is scoped to a component, meaning styles are encapsulated and won’t affect other elements on the page. This approach can be effectively used to avoid conflicting CSS styles.

  • Global CSS: This is CSS that is shared globally, which is the opposite of scoped CSS. When using global CSS, we can reuse the same styles across different components. This approach is useful for CSS resets or sharing commonly used styles.

  • Conditional CSS: This is CSS that is conditionally applied to ...