whatisgithub

What is css-selector?

symfony/css-selector — explained in plain English

Analysis updated 2026-06-24

7,441PHPAudience · developerComplexity · 2/5LicenseSetup · easy

In one sentence

A small PHP library that converts CSS selectors into XPath queries, so you can use the familiar CSS syntax you already know when scraping or parsing HTML with PHP tools that require XPath.

Mindmap

mindmap
  root((css-selector))
    What it does
      CSS to XPath converter
      No framework needed
    Use cases
      Web scraping PHP
      HTML document parsing
      Test selectors
    Origin
      Symfony component
      Ported from Python cssselect
      BSD license
    Audience
      PHP developers
      Scrapers and testers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Query HTML elements by CSS class or tag when web scraping in PHP using a tool that only accepts XPath.

USE CASE 2

Add CSS selector support to a PHP HTML parser or test helper that uses XPath internally.

USE CASE 3

Avoid writing verbose XPath expressions by converting your existing CSS knowledge into XPath automatically.

What is it built with?

PHPSymfony

How does it compare?

symfony/css-selectorsymfony/var-dumpersebastianbergmann/php-text-template
Stars7,4417,4407,437
LanguagePHPPHPPHP
Setup difficultyeasyeasyeasy
Complexity2/51/52/5
Audiencedeveloperdeveloperdeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · easy Time to first run · 5min
Use freely for any purpose, including commercial projects, as long as you keep the BSD copyright notice.

So what is it?

This is a small PHP library that converts CSS selectors into XPath expressions. CSS selectors are the patterns that web developers use to target specific elements on a page, such as "all paragraphs inside a sidebar" or "the first item in a list." XPath is a different query language used to navigate and search through HTML or XML documents. Some PHP tools only understand XPath, so this library acts as a translator: you write the shorter, more familiar CSS syntax and get back the equivalent XPath query. The practical benefit is that CSS selectors are generally easier to read and write than XPath. A CSS selector like "div.article h2" is more compact than writing out the full XPath equivalent, which can become verbose. If you are doing web scraping, testing, or document parsing in PHP and your chosen tool accepts XPath, this library means you do not have to learn XPath syntax from scratch. You write CSS the way a front-end developer would, and the library handles the conversion behind the scenes. The library is part of the Symfony component ecosystem, a collection of standalone PHP packages maintained by the Symfony project. Each component is designed to be used independently from the rest of the framework, so you can pull in just this one piece without adopting the full Symfony stack. It was originally ported from a Python library called cssselect and is distributed under the BSD license. The README is short and does not include installation steps, usage examples, or configuration options. Full documentation is available on the official Symfony documentation site.

Copy-paste prompts

Prompt 1
Use symfony/css-selector to convert 'div.article h2 a' into an XPath expression and then query it with PHP's DOMXPath class, show me the full code.
Prompt 2
I'm scraping a website in PHP using DOMDocument, how do I use the Symfony CssSelector component to find all elements with a specific CSS class?
Prompt 3
Convert these five CSS selectors to XPath using symfony/css-selector and explain any cases where the conversion is lossy or inexact.
Prompt 4
Show me how to install symfony/css-selector standalone via Composer and write a simple PHP script that uses it without the rest of Symfony.

Frequently asked questions

What is css-selector?

A small PHP library that converts CSS selectors into XPath queries, so you can use the familiar CSS syntax you already know when scraping or parsing HTML with PHP tools that require XPath.

What language is css-selector written in?

Mainly PHP. The stack also includes PHP, Symfony.

What license does css-selector use?

Use freely for any purpose, including commercial projects, as long as you keep the BSD copyright notice.

How hard is css-selector to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is css-selector for?

Mainly developer.

Open on GitHub → Ask about another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.