
Dec 13, 2024
Locating Elements on Your Page: A Guide to CSS Selectors
When creating modifications in Optimal UX, you need to tell it which elements to change. CSS selectors are like search patterns that help you find the right elements. This guide will help you understand how to use selectors effectively.
Basic Element Selection
The simplest way to select elements is by their HTML tag name. For example:
button
finds all button elementsh1
finds all main headingsp
finds all paragraphs*
finds all elements (use with caution!)
Selecting by Class and ID
Classes and IDs are special attributes that developers use to identify elements:
Using Classes
button.primary
finds buttons with the "primary" class.alert
finds any element with the "alert" classp.intro
finds paragraphs with the "intro" class
Using IDs (for unique elements)
button#submit-form
finds the button with ID "submit-form"div#main-content
finds the div with ID "main-content"
Finding Elements by Their Position
Sometimes you need to target elements based on their position in the page:
Parent-Child Relationships
nav > a
finds links that are direct children of navigation elementsarticle p
finds paragraphs anywhere inside articles
Using Pseudo-Selectors
Pseudo-selectors help you find elements based on their position or state. OptimalUX supports these position-based pseudo-selectors:
:first-child
finds elements that are the first within their parent:nth-child(n)
finds elements that are the nth child of their parent:first-of-type
finds the first occurrence of each type of element:nth-of-type(n)
finds elements that are the nth occurrence of their type
Note: :last-child
and :last-of-type
are not supported. This is because Optimal UX uses a high-performance streaming approach that processes elements as they appear, making it impossible to determine the "last" element until the entire page is loaded. For better performance, use specific nth-child values instead.
Examples of pseudo-selector usage:
Using Pseudo-Elements
Optimal UX supports the ::before
and ::after
pseudo-elements, allowing you to insert content before or after an element's main content. These pseudo-elements generate CSS-based visual elements without modifying the HTML structure.
Selecting by Attributes
You can find elements based on their attributes and values:
Basic Attribute Selection
img[alt]
finds images that have an alt attributeinput[required]
finds required form fields
Exact Value Matching
input[type="submit"]
finds submit buttonsa[target="_blank"]
finds links that open in new tabs
Partial Value Matching
[class*="button"]
finds elements with "button" anywhere in their class[href^="https"]
finds secure external links[src$=".pdf"]
finds links to PDF files
Special Attribute Matches
[lang|="en"]
finds elements with English language or dialect (en, en-US, etc.)[data-category~="electronics"]
finds elements where "electronics" is one of the space-separated values
Case Sensitivity Options
[title="Download" i]
matches "DOWNLOAD", "download", etc.[title="Download" s]
matches exactly "Download"
Excluding Elements
The :not()
selector lets you exclude elements that match certain patterns:
button:not(.disabled)
finds all buttons except those with the "disabled" classinput:not([type="hidden"])
finds all visible input fields
Best Practices
Start Specific: Begin with the most specific selector that will work for your needs. It's easier to broaden your selection later than to narrow it down.
Test Your Selectors: Before making modifications, verify that your selector targets exactly the elements you want to change. Remember that the same selector might match different elements across different pages.
Consider Performance: When possible, use more specific selectors instead of broad ones. For example,
div.product
is more efficient thandiv [class*="product"]
.Plan for Position-Based Selection: Since last-child selectors aren't available, plan your selections using first-child or specific nth-child values instead.
Common Examples
For Headers and Navigation
For Content Areas
For Forms
Remember that the key to effective modifications is selecting exactly the right elements. Take time to verify your selectors before making changes, and always test your modifications across different pages where the selector might match.
Read more:
How to Analyze A/B Test Results in LogRocket with Optimal UX Integration
Track A/B tests in LogRocket using secure experiment tracking. Watch session replays, monitor performance, and debug issues across variants with comprehensive analytics.

Feb 5, 2025
📁 A/B testing
📁 Split testing
📁 Integration
📁 Client-side testing
📁 Client-side testing
How to Analyze A/B Test Results in Mixpanel with Optimal UX Integration
Track experiment data in Mixpanel using events and user properties. Learn how to analyze A/B test results using cohorts, funnels, and advanced analytics features.

Jan 31, 2025
📁 A/B testing
📁 Split testing
📁 Integration
📁 Client-side testing
📁 Client-side testing
How to Analyze A/B Test Results in Matomo Analytics with Optimal UX Integration
Track A/B tests in Matomo Analytics using native experiment events. Analyze results while maintaining full control over your testing data.

Feb 3, 2025
📁 A/B testing
📁 Split testing
📁 Integration
📁 Client-side testing
📁 Client-side testing
How to Analyze A/B Test Results in Hotjar with Optimal UX Integration
Track experiment participation in Hotjar using custom events. Get visual insights into user behavior across variants through heatmaps and session recordings.

Feb 7, 2025
📁 A/B testing
📁 Split testing
📁 Integration
📁 Client-side testing
📁 Client-side testing