CSS (Cascading Style Sheets) is a stylesheet language used to control the visual presentation of HTML documents, allowing styling elements with colors, layouts, fonts, and more.
style
attribute.<style>
tag within the HTML <head>
..css
file linked to the HTML document.The box model describes how an element is rendered with content, padding, border, and margin around it.
Classes (.class-name
) are reusable and can be applied to multiple elements, while IDs (#id-name
) are unique and apply to only one element per page.
Pseudo-classes target elements in specific states, such as :hover
for when an element is hovered over.
Use margin: auto
on a block-level element with a defined width or use Flexbox (justify-content: center
) for flexibility.
The display
property defines how elements are displayed. Common values include:
block
: Takes up the full width.inline
: Takes up only as much space as needed.flex
: Enables Flexbox layout.Adjacent vertical margins between elements can merge into one larger margin, typically the larger of the two.
Padding is the space inside an element between its content and border, while margin is the space outside an element’s border.
A CSS reset removes browser default styling, allowing consistent styling across different browsers.
relative
: Positions relative to itself.absolute
: Positions relative to the nearest positioned ancestor.fixed
: Positions relative to the viewport, staying in place while scrolling.sticky
: Switches between relative and fixed depending on scroll.Media queries apply styles based on device characteristics (e.g., width). They are essential for responsive design.
Example:
@media (max-width: 600px) { body { background-color: lightblue; } }
Flexbox arranges items along a flexible row or column with properties like justify-content
, align-items
, and flex-wrap
.
em
is relative to the font size of its nearest parent, while rem
is relative to the root (html
) font size.
CSS animations are defined with @keyframes
and applied with properties like animation-name
, animation-duration
, and animation-timing-function
.
z-index
controls the stack order of positioned elements. Higher values appear on top.
Specificity determines which CSS rule applies when multiple rules target the same element. IDs have higher specificity than classes, which are higher than element selectors.
:nth-child()
targets the nth child regardless of type, while :nth-of-type()
targets the nth child of a specific type.
CSS Grid divides layouts into rows and columns, creating complex, responsive layouts with properties like grid-template-columns
and grid-gap
.
@import
imports a CSS file into another, allowing modularization. However, it can slow down performance compared to <link>
tags.
The subgrid
value allows child elements to inherit the grid layout of their parent, useful for maintaining alignment across nested grids.
The contain
property improves performance by isolating rendering to specified elements, reducing layout calculations.
will-change
tells the browser to optimize certain properties that will change, such as transform
or opacity
, enhancing animations and transitions.
Houdini APIs let developers extend CSS, allowing access to the browser’s rendering engine for custom styling, animations, and layout calculations.
Variable fonts store multiple font styles in a single file, reducing HTTP requests and enabling smoother transitions between weights and styles.
mix-blend-mode
defines how content blends with background content, used for creative effects like overlays and complex visual styling.
CSS Scoping limits styles to a particular section. The :scope
pseudo-class targets the scoped element, usually within a shadow DOM or component-based structure.
calc()
performs calculations within CSS, allowing for responsive adjustments like width: calc(100% - 50px);
.
::part
styles custom elements’ shadow DOM, while ::slotted
styles elements passed into slots within Web Components.
clamp()
constrains a CSS value between a minimum, preferred, and maximum value, making responsive design easier without media queries.
Custom properties, defined with --property-name
, allow variables in CSS, making code more maintainable and reusable.
CSS Layers allow controlled stacking of CSS rules for better organization and specificity management, prioritizing specific layers.
prefers-reduced-motion
detects if the user prefers less motion and adapts animations for accessibility.
Critical CSS loads above-the-fold styles inline to minimize render-blocking, speeding up initial page load.
backdrop-filter
applies effects like blur and brightness behind an element, creating visually appealing UI effects.
:has()
is a relational selector allowing CSS to apply styles based on the presence of specific child elements.
The aspect-ratio
property ensures elements maintain proportional dimensions, improving responsive designs.
A polyfill is JavaScript that replicates CSS functionality not natively supported by browsers, ensuring compatibility.
The Paint API allows custom CSS graphics directly within stylesheets, enabling unique backgrounds and borders dynamically.