The latest version of CSS, as of 2024
-
CSS Container Queries: These allow components to adapt their layout based on the available space of their parent container, enhancing responsive design. This is particularly useful for creating dynamic, reusable components that adjust their layout according to their container size rather than the viewport.
-
CSS Nesting: This feature allows developers to write cleaner, more maintainable code by embedding selectors within one another, similar to pre-processors like SASS. This eliminates the need to repeatedly specify the parent selector, streamlining the process of writing CSS.
-
CSS Layers: Introduced to manage and organize the priority of styles, CSS Layers (
@layer) allow developers to define layers within which styles are applied in a specific order, making it easier to handle complex stylesheets and avoid specificity conflicts. -
New Pseudo-Classes and Properties: The
:has()pseudo-class enables styling of elements based on their descendants, enhancing interactivity and dynamic styling. Additionally, the:focus-visiblepseudo-class improves accessibility by applying styles only when elements are focused via keyboard navigation. -
Enhanced Text Layouts: The
text-wrap: balanceproperty ensures more aesthetically pleasing text layouts by balancing the number of characters per line, preventing awkward breaks and creating a more professional look. -
Advanced Media Queries: Media queries have been extended to support more complex conditions using logical operators like
and,or, andnot. This allows for more nuanced and flexible responsive designs.
These updates reflect the ongoing evolution of CSS, making it more powerful and adaptable to modern web development needs.
For more detailed information, you can visit the CSS Snapshot 2024 and other sources like Academind and Builder.io.
Core CSS Concepts and Properties
-
Selectors and Specificity
- Understand how to select elements using various selectors (
element,.class,#id, attribute selectors, pseudo-classes, and pseudo-elements). - Learn about specificity rules to manage conflicting styles.
- Understand how to select elements using various selectors (
-
Box Model
- Properties:
width,height,padding,margin,border,box-sizing. - Concept: How elements are sized and spaced within their containers.
- Properties:
-
Typography
- Properties:
font-family,font-size,font-weight,line-height,letter-spacing,text-align,text-transform,text-decoration.
- Properties:
-
Color and Backgrounds
- Properties:
color,background-color,background-image,background-size,background-repeat,background-position,opacity.
- Properties:
-
Layout
- Flexbox:
display: flex,flex-direction,justify-content,align-items,flex-wrap,flex-grow,flex-shrink. - Grid:
display: grid,grid-template-columns,grid-template-rows,grid-gap,grid-area,justify-items,align-items.
- Flexbox:
-
Positioning
- Properties:
position(static,relative,absolute,fixed,sticky),top,right,bottom,left,z-index.
- Properties:
-
Responsive Design
- Media Queries:
@media (max-width: 600px) { ... } - Container Queries:
@container (min-width: 400px) { ... }(Experimental)
- Media Queries:
-
Animations and Transitions
- Properties:
transition,transition-property,transition-duration,animation,@keyframes.
- Properties:
-
Transforms
- Properties:
transform,transform-origin. - Functions:
translate(),rotate(),scale(),skew().
- Properties:
-
Flexibility and Responsiveness
- CSS Variables (Custom Properties):
--main-color: #333;,color: var(--main-color); - CSS Units:
%,em,rem,vh,vw,px.
- CSS Variables (Custom Properties):
Latest CSS Features
-
CSS Grid Layout: Advanced grid-based layouts.
- Properties:
grid-template-areas,grid-template-columns,grid-template-rows,grid-auto-flow.
- Properties:
-
CSS Custom Properties: Variables for reusable values.
- Syntax:
--variable-name: value;,var(--variable-name).
- Syntax:
-
CSS Logical Properties: Logical direction-based properties.
- Properties:
margin-inline-start,padding-block-end,inset-block-start.
- Properties:
-
CSS Subgrid: Advanced grid alignment (Experimental).
- Properties:
subgridfor nested grids.
- Properties:
-
CSS Shapes and Exclusions: Content wrapping around shapes.
- Properties:
shape-outside,shape-margin,clip-path.
- Properties:
-
CSS Scroll Snap: Snapping elements during scroll.
- Properties:
scroll-snap-type,scroll-snap-align,scroll-snap-stop.
- Properties:
-
CSS :has() Pseudo-Class: Parent selector (Experimental).
- Syntax:
:has(selector)
- Syntax:
-
CSS :focus-visible: Enhanced focus indication.
- Usage:
:focus-visible { ... }
- Usage:
Essential Resources for Learning CSS
- MDN Web Docs: Comprehensive documentation on all CSS properties and concepts.
- CSS Tricks: Practical guides and examples.
- A Complete Guide to Flexbox: Detailed explanation of Flexbox.
- A Complete Guide to Grid: Detailed explanation of Grid.
Latest CSS properties that are frequently used
-
container-type: Defines the type of container for the container query..wrapper {
container-type: inline-size;
} -
container-name: Assigns a name to a container for use in container queries..wrapper {
container-name: main-container;
} -
@container: Container query rule for applying styles based on container size.@container (max-width: 500px) {
.child {
flex-direction: column;
}
} -
@layer: Defines style layers to manage style priorities.@layer base, theme, components;
@layer base {
body {
margin: 0;
font-family: sans-serif;
}
} -
accent-color: Specifies the color of form controls like checkboxes and radio buttons.input[type="checkbox"] {
accent-color: #007bff;
} -
scroll-padding: Adjusts the scroll position to provide padding around the target element..section {
scroll-padding: 50px;
} -
scroll-snap-type: Controls the snapping behavior when scrolling..container {
scroll-snap-type: y mandatory;
} -
aspect-ratio: Defines the aspect ratio of an element..video {
aspect-ratio: 16 / 9;
} -
backdrop-filter: Applies graphical effects like blur or color shift to the area behind an element..overlay {
backdrop-filter: blur(10px);
} -
place-items: Shorthand foralign-itemsandjustify-items..grid-container {
place-items: center;
} -
scroll-behavior: Specifies the scrolling behavior for a scrolling box.
html {
scroll-behavior: smooth;
}
gap: Defines the gap between grid or flexbox items.
.grid-container {
display: grid;
gap: 20px;
}
grid-template-areas: Specifies areas within a grid layout.
.grid-container {
display: grid;
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
}
min(): Function to return the smallest value among arguments.
.element {
width: min(50vw, 300px);
}
max(): Function to return the largest value among arguments.
.element {
width: max(10vw, 200px);
}
clamp(): Function to set a value within a range defined by a minimum and maximum value.
.element {
font-size: clamp(1rem, 2.5vw, 2rem);
}
subgrid: Enables nested grid items to inherit the grid definitions from the parent grid container.
.subgrid-container {
display: grid;
grid-template-columns: subgrid;
}
font-variant: Controls the use of alternate glyphs in certain fonts.
.text {
font-variant: small-caps;
}
backdrop-filter: Applies graphical effects to the area behind an element.
.modal {
backdrop-filter: grayscale(50%);
}
-
overscroll-behavior: Controls the behavior when reaching the boundary of a scrolling area..scroll-container {
overscroll-behavior: contain;
} -
object-fit: Specifies how the content of a replaced element should be resized to fit its container.
.image {
object-fit: cover;
}
object-position: Specifies the alignment of the content within a replaced element.
.image {
object-position: center top;
}
isolation: Creates a new stacking context, allowing for better control over z-index and blending modes.
.element {
isolation: isolate;
}
mix-blend-mode: Specifies how an element’s content should blend with its background.
.element {
mix-blend-mode: multiply;
}
inset: A shorthand property fortop,right,bottom, andleft.
.box {
position: absolute;
inset: 10px 20px 30px 40px;
}
aspect-ratio: Specifies the preferred aspect ratio of an element.
.video {
aspect-ratio: 16 / 9;
}
text-decoration-thickness: Sets the thickness of text decoration lines.
.underline {
text-decoration: underline;
text-decoration-thickness: 2px;
}
text-underline-offset: Sets the distance between the text underline and its text.
.underline {
text-underline-offset: 4px;
}
line-clamp: Limits the content to a specified number of lines, adding an ellipsis if the content exceeds this limit.
.clamped-text {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}
-
backface-visibility: Defines whether the back face of an element is visible when facing the user..card {
backface-visibility: hidden;
} -
clip-path: Creates a clipping region that determines which parts of an element are visible.
.clip {
clip-path: circle(50%);
}
filter: Applies graphical effects like blurring or color shifting to an element.
.image {
filter: grayscale(100%);
}
-
grid-template-columns: Defines the column structure for a grid container..grid {
display: grid;
grid-template-columns: repeat(3, 1fr);
} -
grid-template-rows: Defines the row structure for a grid container.
.grid {
display: grid;
grid-template-rows: 100px auto 100px;
}
align-self: Allows the alignment of an individual flex or grid item along the cross axis.
.item {
align-self: center;
}
-
justify-self: Aligns an individual grid item along the inline axis..grid-item {
justify-self: end;
} -
place-content: A shorthand foralign-contentandjustify-content.
.grid {
display: grid;
place-content: center space-between;
}
overscroll-behavior: Controls the behavior when the boundary of a scrollable area is reached.
.container {
overscroll-behavior: none;
}
will-change: Hints to the browser about which properties of an element are expected to change, improving performance.
.animated {
will-change: transform, opacity;
}
font-variation-settings: Controls variable font properties, allowing for fine-tuned typography..text {
font-variation-settings: "wght" 700, "wdth" 125;
}
These properties enhance the ability to create flexible, performant, and visually appealing designs. For further reading and more detailed examples, refer to resources like the MDN Web Docs and the CSS Tricks.
What is Stacking Context in CSS
A stacking context is a concept in CSS that controls the layering order (z-order) of elements. It determines the stacking order of elements and their descendants when they overlap, which is crucial for visual rendering in the browser. The stacking context helps determine which elements appear in front of or behind others.
When does a Stacking Context get created?
A stacking context is automatically created in the following situations:
- The root element (
<html>) forms the initial stacking context. - Any positioned element (with
position: relative,absolute,fixed, orsticky) that has az-indexother thanauto. - Elements with
opacityless than 1 (e.g.,opacity: 0.5) create a stacking context. transformproperty (e.g.,transform: translate(),rotate(), etc.) also creates a stacking context.- CSS filters like
filter: blur()orgrayscale()create a stacking context. will-changeproperty: When applied to certain properties liketransform,opacity, etc., it can create a stacking context.mix-blend-mode(other thannormal) also creates a stacking context.
In these cases, the elements within the stacking context are arranged based on their z-index values, and the stacking context itself will be treated as a separate "layer" when the browser renders elements on the page.
How does the Stacking Context Work?
Each stacking context is self-contained. Elements within a stacking context can only be layered relative to other elements within the same stacking context. They can't interact with elements outside their context unless the outer context is higher in the stacking order.
Example:
If we have a page with multiple div elements, and one of them creates a stacking context, it will stack its child elements independently of the other div elements.
<div style="position: relative; z-index: 1;">
<p>This is a stacking context!</p>
</div>
<div style="z-index: 2;">This will be stacked above the first div</div>
- In this case, the second
divwill be stacked above the first one because it has a higherz-indexand is in the same stacking context. - However, the child elements within the first
divare stacked independently of elements outside of this context.
Stacking Order:
- Elements in the same stacking context are ordered based on the
z-indexvalues of their child elements. - Stacking contexts themselves are layered according to their position in the DOM tree and their
z-indexvalues. - Elements without
z-indexandposition: staticare stacked in the order they appear in the DOM.
Visual Example:
<div style="position: relative; z-index: 1; background-color: red;">
<div style="position: absolute; top: 20px; z-index: 10; background-color: yellow;">
Inside inner stacking context
</div>
</div>
In this case:
- The inner
divhas its own stacking context due to theposition: absoluteandz-indexproperties, so it will stack based on its ownz-index. - The outer
divhas its own stacking context as well, and it will be stacked according to thez-indexvalues relative to other contexts.
Key Points to Remember:
- Positioned elements with
z-indexcreate stacking contexts. - Stacking contexts are treated as isolated layers in the rendering process.
z-indexvalues work only within their respective stacking context.
A stacking context is a crucial concept in CSS that governs the z-order (stacking order) of elements when they overlap. Each stacking context is like a separate "layer" of elements, and elements inside one context will be layered according to their z-index relative to other elements within the same context. Here's when a stacking context is created:
1. Root Element (<html>)
-
The root element (
<html>) automatically forms the initial stacking context for the entire document. -
It does not need any special styles to create a stacking context.
Example: The root
<html>element is always a stacking context unless overridden by other conditions.
2. Positioned Elements with z-index
-
Elements with
positionset torelative,absolute,fixed, orstickywill create a stacking context when they also have az-indexvalue other thanauto.Example:
<div style="position: relative; z-index: 10;">Stacking Context</div>This
<div>creates a stacking context because it has aposition(relative) and a specificz-index.
3. Opacity Less Than 1
-
Elements with
opacityless than1create a new stacking context. This includes semi-transparent elements (like those withopacity: 0.5).Example:
<div style="opacity: 0.5;">This will create a stacking context</div>
4. transform Property
-
Elements with the
transformproperty (e.g.,transform: translate(),rotate(),scale(), etc.) automatically create a stacking context, even if they don't have apositionorz-index.Example:
<div style="transform: translateX(50px);">This creates a stacking context</div>
5. will-change Property
-
Elements with the
will-changeproperty create a stacking context when certain properties are likely to change in the future (such asopacity,transform, etc.). This property is a hint to the browser for performance optimization.Example:
<div style="will-change: transform;">This will create a stacking context</div>
6. CSS Filters
-
Elements with CSS filters (e.g.,
filter: blur(),grayscale(),brightness(), etc.) will create a new stacking context.Example:
<div style="filter: blur(5px);">This creates a stacking context</div>
7. mix-blend-mode
-
Elements using
mix-blend-modeother thannormalwill create a stacking context. Themix-blend-modeproperty controls how an element blends with the background or other elements.Example:
<div style="mix-blend-mode: multiply;">This creates a stacking context</div>
8. contain Property
-
The
containproperty (e.g.,contain: layout;) can create a stacking context by isolating the layout of the element. This prevents the child elements from affecting the outside environment and vice versa.Example:
<div style="contain: layout;">This creates a stacking context</div>
9. clip-path
-
Elements with
clip-path(e.g.,clip-path: circle()) will create a stacking context. This property defines the visible area of an element by clipping parts of it.Example:
<div style="clip-path: circle(50%);">This creates a stacking context</div>
10. perspective
-
Elements with the
perspectiveproperty also form a stacking context. Theperspectiveproperty is typically used for 3D transformations and helps in creating a 3D effect on child elements.Example:
<div style="perspective: 500px;">This creates a stacking context</div>
When does a Stacking Context NOT get created?
- Position: static: The default value of
positionisstatic, which does not create a stacking context. - z-index: auto: When an element has
positionbut no specificz-indexor hasz-index: auto, it doesn't create a stacking context.
Summary of Key Cases:
- Positioned elements (
position: relative, absolute, fixed, sticky) with az-indexother thanauto. - Elements with opacity less than
1. transform,will-change,filter,mix-blend-mode,clip-path, andperspective.- Root element (
<html>) automatically creates the initial stacking context.
Why is this Important?
Stacking contexts are vital when managing how elements overlap. Understanding when stacking contexts are created allows developers to control the layer positioning of elements without unintended behavior. Each stacking context operates independently, which ensures more predictable and reliable layout control, especially in complex UIs with overlapping elements.
Sources:
Inner Context, Outer Context, and Hierarchy of Stacking Contexts in CSS
A stacking context is a concept in CSS that controls how elements overlap and are layered in relation to each other. When discussing inner and outer contexts, we are referring to the nesting and hierarchy of these contexts within a page. Here's how the hierarchy and relationship between inner and outer contexts work:
What is the Hierarchy of Stacking Contexts?
-
Root Context:
- The root element (
<html>) forms the initial stacking context for the entire webpage. It serves as the foundation of all other stacking contexts on the page.
- The root element (
-
Outer Context:
- The outer stacking context is any stacking context that exists outside another stacking context. It is typically a parent element that has a specific
position(withz-index) or other properties that create a stacking context. - The outer context is responsible for the stacking order of any nested stacking contexts (i.e., inner contexts) that exist within it.
- The outer stacking context is any stacking context that exists outside another stacking context. It is typically a parent element that has a specific
-
Inner Context:
- The inner stacking context is a stacking context that exists within an outer stacking context. It is created when an element inside the outer context has properties like
position: relative,absolute,fixed, orstickyalong with a specificz-indexor other properties likeopacityortransformthat create new stacking contexts. - The inner context is stacked independently of its outer context but within the limits of its parent stacking context.
- The inner stacking context is a stacking context that exists within an outer stacking context. It is created when an element inside the outer context has properties like
How the Hierarchy Works
In a document, stacking contexts are created whenever certain CSS properties are applied. These stacking contexts are like separate layers in the page's layout, and they have a parent-child relationship:
- Outer Context: A parent stacking context can contain one or more child stacking contexts (inner contexts).
- Inner Context: These are nested stacking contexts, which may have their own stacking order independent of their parent context. However, they cannot interact with elements outside their context unless the parent stacking context is at a higher z-index.
Visualizing the Hierarchy of Stacking Contexts:
Consider the following HTML structure:
<div style="position: relative; z-index: 1;">
<div style="position: absolute; z-index: 2;">
Inner Context (Stacked in Front of Parent)
</div>
<div style="position: absolute; z-index: 1;">
Another Child in Outer Context
</div>
</div>
<div style="position: relative; z-index: 3;">
Outer Context (Higher than Parent Context Above)
</div>
- Outer Context: The second
<div>withposition: relativeandz-index: 3forms an outer stacking context, which will be stacked above the first outer context (withz-index: 1) and any elements inside it. - Inner Context: The first
<div>withposition: relativeandz-index: 1creates a stacking context, and the two child elements inside it form inner stacking contexts. The child withz-index: 2will be stacked in front of the child withz-index: 1within this inner context.
Layering Based on z-index and Positioning:
- Parent Stacking Context: The stacking order for elements in an outer context depends on their
z-index. If the outer context has a higherz-indexthan another stacking context, it will appear in front of that context. - Child Elements within Inner Context: Inside each stacking context, the order of elements is controlled by their
z-indexvalues relative to each other.
Order of Stacking Contexts (DOM Order):
- The outer stacking context will be stacked above any inner contexts unless the
z-indexof the inner context is higher. - The child stacking contexts are ordered based on their DOM hierarchy as well. If two stacking contexts are siblings in the DOM and have the same
z-index, the one that comes later in the DOM will appear in front.
Example Breakdown:
Consider this structure:
<div style="position: relative; z-index: 1;">
Outer Context 1
<div style="position: absolute; z-index: 1;">
Inner Context 1
</div>
</div>
<div style="position: relative; z-index: 2;">
Outer Context 2
<div style="position: absolute; z-index: 1;">
Inner Context 2
</div>
</div>
- Outer Context 1: The first
divwithposition: relativeandz-index: 1creates a stacking context. The inner elements in it will be stacked relative to each other, but they won’t interact with elements outside the context unless theirz-indexis higher. - Inner Context 1: The second
divwithposition: absoluteandz-index: 1creates a new stacking context within Outer Context 1. - Outer Context 2: The second
divwithz-index: 2creates a higher-level stacking context than Outer Context 1. This means that Outer Context 2 and its children will be layered above Outer Context 1 and its inner context.
Key Takeaways:
- Outer contexts are created by parent elements with positioning and specific
z-indexvalues. - Inner contexts are created by child elements within the outer context that have properties such as
positionwithz-indexor other properties liketransform,opacity, etc. - The stacking order of elements inside a stacking context is independent of other contexts.
- Higher
z-indexvalues will appear in front of lower values within the same context. - The order of stacking contexts themselves is determined by their
z-indexrelative to other contexts and their position in the DOM tree.
Sources:
descendants
When descendants of a stacking context overlap, their stacking order is determined by their position in the stacking context, governed by their z-index and how their stacking context is defined. Here's how it works:
How Descendants Overlap in a Stacking Context
-
Positioning and z-index:
- Inside a stacking context, elements can overlap based on their
z-indexvalues. Thez-indexproperty controls the stacking order of positioned elements (those withpositionset torelative,absolute,fixed, orsticky). - Higher
z-indexvalues appear in front of lowerz-indexvalues. - If elements don't have a
z-indexset, they will stack in the order they appear in the DOM (document order), with later elements appearing in front of earlier ones.
- Inside a stacking context, elements can overlap based on their
-
Nested Stacking Contexts:
- When an element creates a stacking context (e.g., a parent with
position: relativeand az-index), its child elements are stacked relative to each other inside that context. - These child elements do not affect the stacking order of elements outside this stacking context. Each stacking context behaves independently.
Example:
<div style="position: relative; z-index: 2;">
<div style="position: absolute; z-index: 1;">This is in front of its sibling within the stacking context</div>
<div style="position: absolute; z-index: 2;">This is behind the first child within the stacking context</div>
</div> - When an element creates a stacking context (e.g., a parent with
-
Overlapping between Different Stacking Contexts:
- When stacking contexts are nested, the outer stacking context will appear above the inner stacking context if it has a higher
z-index. - If two stacking contexts are not nested, they are ordered according to their position in the DOM tree, with later stacking contexts appearing on top.
- When stacking contexts are nested, the outer stacking context will appear above the inner stacking context if it has a higher
Example of Overlapping Descendants:
Consider the following HTML with multiple stacking contexts:
<div style="position: relative; z-index: 1;">
<div style="position: absolute; z-index: 1;">Inner Context 1</div>
<div style="position: absolute; z-index: 2;">Inner Context 2</div>
</div>
<div style="position: relative; z-index: 3;">
<div style="position: absolute; z-index: 1;">Outer Context</div>
</div>
- Inner Context 2 (with
z-index: 2) will be stacked in front of Inner Context 1 (withz-index: 1) becausez-indexwithin the same stacking context determines the layering. - Outer Context will be stacked above the whole first
<div>(withz-index: 1) because it has a higherz-index.
Key Points about Descendants in Stacking Contexts:
-
Descendants within the same stacking context:
- Are stacked based on their z-index values (higher values are in front).
- Without a
z-index, they follow the document order.
-
Stacking contexts themselves:
- Are layered based on the
z-indexof the parent stacking context or the order they appear in the DOM. - The stacking context created by a parent will not affect stacking contexts created by other parent elements unless those contexts overlap.
- Are layered based on the
-
Independence of stacking contexts:
- The stacking order of child elements is contained within their own stacking context. They do not interact with sibling elements outside their context unless the contexts themselves overlap in the document.