Back to Blog
January 6, 20269 min readUnder the Hood:

Under the Hood: Analyzing the Accuracy of AI-Generated CSS Selectors

R
Replay Team
Developer Advocates

TL;DR: This article dives deep into how Replay utilizes Gemini to generate accurate CSS selectors from video, highlighting the challenges and solutions in behavior-driven reconstruction.

Under the Hood: Analyzing the Accuracy of AI-Generated CSS Selectors#

The promise of AI-driven code generation is tantalizing: automatically translate designs or user flows into functional code. But the devil is always in the details, especially when it comes to accurately targeting elements with CSS selectors. Many tools offer screenshot-to-code conversion, but these often fall short because they lack an understanding of user intent. Replay takes a different approach, using video as the source of truth, and leverages Gemini to understand user behavior and generate contextually accurate CSS selectors. This is "Behavior-Driven Reconstruction." Let's examine how Replay achieves this and the challenges involved.

The Problem with Traditional Approaches#

Traditional screenshot-to-code tools are limited. They analyze static images, which provides only a snapshot of the UI. They don't understand the dynamic nature of web applications, user interactions, or the underlying logic. This leads to brittle CSS selectors that break easily when the UI changes slightly.

Consider this table comparing different approaches:

FeatureScreenshot-to-CodeManual CSSReplay
Contextual Understanding
Dynamic Behavior Analysis
Maintenance EffortHighMediumLow
AccuracyLowHighHigh
SpeedFastSlowFast

Replay overcomes these limitations by analyzing video of user interactions. This allows the AI to understand the why behind the UI elements, not just the what.

Behavior-Driven Reconstruction: Replay's Approach#

Replay's core innovation is "Behavior-Driven Reconstruction." It uses Gemini to analyze video recordings of user interactions and generate code that reflects the intended behavior. This process involves several key steps:

  1. Video Analysis: Replay analyzes the video frame by frame, identifying UI elements and their properties.
  2. Behavioral Inference: Gemini infers the user's intent by observing their interactions with the UI (e.g., clicking a button, filling out a form).
  3. CSS Selector Generation: Gemini generates CSS selectors that accurately target the intended elements based on their properties and the user's behavior.
  4. Code Generation: Replay generates React code (or other frameworks) using the generated CSS selectors to recreate the UI.
  5. Multi-Page Generation: Replay understands page transitions and generates code for multi-page applications.
  6. Supabase Integration: Connect directly to your Supabase backend for data-driven components.
  7. Style Injection: Seamlessly inject styles to match your existing design system.

The Challenge: Ensuring Accuracy#

Generating accurate CSS selectors is a complex task. The AI must consider various factors, including:

  • Specificity: Selectors should be specific enough to target the intended elements but not so specific that they break easily.
  • Maintainability: Selectors should be easy to understand and maintain.
  • Uniqueness: Selectors should uniquely identify the intended elements.
  • Dynamic Content: Selectors should work even when the content of the UI changes.

To address these challenges, Replay employs several techniques:

  • Heuristic-Based Refinement: After Gemini generates the initial selectors, Replay applies a set of heuristics to refine them. These heuristics consider factors like the element's position in the DOM tree, its attributes, and its relationship to other elements.
  • Behavioral Contextualization: The AI uses the user's behavior to further refine the selectors. For example, if the user clicks a button with a specific text label, Replay can generate a selector that targets that button based on its text label.
  • Machine Learning-Based Optimization: Replay continuously learns from its mistakes and improves its CSS selector generation algorithms using machine learning. This allows it to adapt to new UI patterns and generate more accurate selectors over time.

Implementation Details: Code Examples#

Let's look at some code examples to illustrate how Replay generates CSS selectors.

Example 1: Targeting a Button

Suppose the user clicks a button with the text "Submit". Replay might generate the following CSS selector:

css
button:contains("Submit") { /* Styles for the submit button */ }

This selector targets all

text
button
elements that contain the text "Submit". While simple, this demonstrates how Replay leverages textual content identified from the video to create meaningful selectors.

Example 2: Targeting an Input Field

Suppose the user enters text into an input field with the placeholder "Email". Replay might generate the following CSS selector:

css
input[placeholder="Email"] { /* Styles for the email input field */ }

This selector targets all

text
input
elements with the
text
placeholder
attribute set to "Email".

💡 Pro Tip: Replay prioritizes attribute-based selectors when appropriate, as they are often more robust than purely structural selectors.

Example 3: React Component Reconstruction

Replay doesn't just generate CSS; it reconstructs entire React components, complete with data binding and event handlers.

typescript
import React, { useState } from 'react'; const MyForm = () => { const [email, setEmail] = useState(''); const handleSubmit = (event: React.FormEvent) => { event.preventDefault(); console.log('Submitted email:', email); // Add your submission logic here }; return ( <form onSubmit={handleSubmit}> <input type="email" placeholder="Email" value={email} onChange={(e) => setEmail(e.target.value)} /> <button type="submit">Submit</button> </form> ); }; export default MyForm;

This example demonstrates how Replay can generate a functional React component based on the observed user behavior in the video. It includes state management, event handling, and proper data binding.

Addressing Common Challenges#

Here are some common challenges and how Replay addresses them:

  • Dynamic Class Names: Many modern web frameworks use dynamic class names (e.g., CSS Modules, styled-components). Replay can identify patterns in these class names and generate selectors that are less brittle.
  • Shadow DOM: Replay can penetrate the Shadow DOM to target elements within encapsulated components.
  • Accessibility: Replay strives to generate code that is accessible by default, including proper ARIA attributes and semantic HTML.

⚠️ Warning: While Replay automates much of the process, it's crucial to review the generated code and ensure it meets your specific requirements and accessibility standards.

Product Flow Maps#

Beyond individual components, Replay can generate "Product Flow Maps" – visualizations of the user journey through an application. This allows developers to quickly understand the overall structure and navigation of the UI, making it easier to maintain and extend. This is a unique feature that sets Replay apart from simple code generation tools.

Benefits of Behavior-Driven Reconstruction#

  • Faster Development: Replay significantly reduces the time and effort required to recreate UIs.
  • Improved Accuracy: By analyzing user behavior, Replay generates more accurate and maintainable CSS selectors.
  • Reduced Maintenance: The generated code is less brittle and easier to maintain over time.
  • Better Understanding: Product Flow Maps provide a clear understanding of the user journey.
BenefitDescription
Accelerated DevelopmentReplay automates the tedious task of writing CSS selectors and reconstructing UI components, freeing up developers.
Enhanced AccuracyBehavior-driven analysis ensures the generated code accurately reflects the intended user experience.
Reduced MaintenanceRobust selectors and well-structured code minimize the need for future adjustments and bug fixes.
Deeper UnderstandingProduct Flow Maps provide a visual overview of the application's structure, improving collaboration and maintainability.

Step-by-Step Example: Reconstructing a Simple Form#

Let's walk through a simplified example of how Replay reconstructs a simple form from a video:

Step 1: Video Upload and Analysis#

The user uploads a video of themselves interacting with a form containing fields for "Name" and "Email" and a "Submit" button. Replay begins analyzing the video.

Step 2: Element Identification#

Replay identifies the following elements:

  • An
    text
    input
    field with the placeholder "Name"
  • An
    text
    input
    field with the placeholder "Email"
  • A
    text
    button
    with the text "Submit"

Step 3: Behavior Inference#

Replay infers the following behavior:

  • The user enters text into the "Name" field.
  • The user enters text into the "Email" field.
  • The user clicks the "Submit" button.

Step 4: CSS Selector Generation#

Replay generates the following CSS selectors:

css
input[placeholder="Name"] { /* Styles for the name input field */ } input[placeholder="Email"] { /* Styles for the email input field */ } button:contains("Submit") { /* Styles for the submit button */ }

Step 5: React Code Generation#

Replay generates the following React code:

typescript
import React, { useState } from 'react'; const MyForm = () => { const [name, setName] = useState(''); const [email, setEmail] = useState(''); const handleSubmit = (event: React.FormEvent) => { event.preventDefault(); console.log('Submitted name:', name, 'email:', email); // Add your submission logic here }; return ( <form onSubmit={handleSubmit}> <input type="text" placeholder="Name" value={name} onChange={(e) => setName(e.target.value)} /> <input type="email" placeholder="Email" value={email} onChange={(e) => setEmail(e.target.value)} /> <button type="submit">Submit</button> </form> ); }; export default MyForm;

This is a simplified example, but it illustrates the basic principles behind Replay's behavior-driven reconstruction.

📝 Note: Replay also offers features like Supabase integration, allowing you to quickly connect your UI to a backend database.

Frequently Asked Questions#

Is Replay free to use?#

Replay offers both free and paid plans. The free plan has limitations on the number of videos you can analyze per month. The paid plans offer unlimited usage and additional features.

How is Replay different from v0.dev?#

While both Replay and v0.dev aim to generate code, they differ significantly in their approach. v0.dev primarily uses text prompts to generate code, while Replay analyzes video of user interactions. This allows Replay to understand the user's intent and generate more accurate and maintainable code. Furthermore, Replay provides features like Product Flow Maps and Supabase integration, which are not available in v0.dev. Replay is focused on reconstructing existing UIs, while v0.dev is more focused on generating new UIs from scratch.

What frameworks does Replay support?#

Currently, Replay primarily supports React. Support for other frameworks is planned for the future.

How accurate are the generated CSS selectors?#

Replay strives to generate highly accurate CSS selectors. However, the accuracy can vary depending on the complexity of the UI and the quality of the video. It's always recommended to review the generated code and make any necessary adjustments.


Ready to try behavior-driven code generation? Get started with Replay - transform any video into working code in seconds.

Ready to try Replay?

Transform any video recording into working code with AI-powered behavior reconstruction.

Launch Replay Free