Checking the validity of CSS selectors

I recently came across a CSS Selector Tester tool at the following link:
http://www.w3schools.com/cssref/trysel.asp

Is this tool reliable? It showcases some unique selectors such as:

  • p:first
  • ul li:eq(0)
  • :contains(Duck)

However, I encountered an issue with the following code snippet:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8>
    <title>CSS Selectors</title>
    <style>
        p:first {
            outline: 1px solid red;
        }
    </style>
</head>

<body>
    <div>
        <p>First paragraph</p>
        <p>Second paragraph</p>
        <p>Third paragraph</p>
    </div>
</body>

</html>

DEMO

Answer №1

It appears that the information provided on the linked page is incorrect (cue the sounds of discontent directed towards W3Schools). The selectors listed there are specifically for jQuery, which are considered non-standard and not valid CSS selectors despite their similar syntax.

While the :first pseudo-class is designated for use within the @page rule, it is not meant to be utilized elsewhere. For example, a selector like p:first is not valid in a style rule, @page rule, or even in a document.querySelectorAll() function. Page selectors are unique selectors explained in detail in the CSS2.1 documentation and Paged Media module documentation. Although they are part of the CSS standard, they are separate from the Selectors standard, resembling jQuery selectors only in terms of syntax.

Regarding :first, it is also defined in jQuery but functions differently compared to its usage in a @page rule. The page you referenced demonstrates jQuery's interpretation of :first. It may be more accurate to consider :first as a valid page selector or jQuery selector due to its context-specific nature, incapable of being used in CSS stylesheets or document.querySelectorAll().

Similar to :first, :eq() and related selectors are exclusive to jQuery and differ significantly from CSS equivalents like :first-child and :nth-child(). While CSS can mimic some functionalities using traditional selectors, jQuery introduced these unique selectors for specific purposes. Refer to my response to this query for a detailed explanation.

Furthermore, although :contains() was initially planned for inclusion in CSS specifications, it never materialized. Its functionality has been replicated in jQuery; however, caution must be exercised due to potential performance impact or unexpected behavior.

Answer №2

Those selectors are valid, although they may be considered somewhat uncommon.

:first

This selector is used for styling the first page of a document when it is printed. However, browser support for this selector can be inconsistent. For more information, you can visit: https://developer.mozilla.org/en-US/docs/Web/CSS/:first

In the example markup provided, it seems like you want to style the first item. In that case, you should use :first-child instead. You can learn more about it here: https://developer.mozilla.org/en-US/docs/Web/CSS/:first-child

:eq(0)

This is a jQuery CSS selector. If you were using pure CSS, you would probably use :nth-child instead. More information on this selector can be found at: http://api.jquery.com/eq-selector/

:contains()

Although this selector was proposed, it did not actually make it into the specification. You can read more about it here: Why doesn't the selector h3:nth-child(1):contains('a') work?

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Do not apply tailwindcss styles to Material-UI

I've been struggling to apply styling from tailwindcss to my MUI button. My setup includes babel and webpack, with the npm run dev script as "webpack --mode development --watch". tailwind.css module.exports = { content: ["./src/**/*.{js, jsx, t ...

What is the best way to keep the footer at the bottom and make it fixed in Material UI?

import * as React from "react"; import Container from "@mui/material/Container"; import Image from "next/image"; import Link from "@/src/Link"; import Box from "@mui/material/Box"; import Typography from &q ...

Utilize an iframe input field to initiate a Jquery event

Thanks to this amazing platform, I was able to discover a solution for expanding an iframe upon clicking using HTML and Jquery. However, my issue remains unresolved. I have successfully expanded the iframe with text, but I want to use input fields within ...

Although the Flexbox is configured with 0 gap and margin, there remains a slight space between rows

I am currently attempting to utilize a flexbox in order to display several 600x300 images, however, I am encountering an unexpected small gap between the rows despite specifying a 0 gap and margin. Here is a screenshot for reference: .gallery { display: ...

Guide on adjusting the size of an image based on either its width or height dimensions

Currently, I am retrieving product images from an API but unfortunately, they do not have consistent dimensions. My goal is to display these images within a 250x250 div. In some cases, the image is in portrait orientation and should be scaled based on its ...

Is there a way to make the fixed table header scroll along with the table body data?

I am facing an issue with my table where I have multiple columns. I have managed to fix the table header successfully, however, when I scroll horizontally through the table body columns, the header remains fixed and does not move accordingly. How can I res ...

Collapsed on Load Vertical Collapsible Panel - Initially hidden panel on page load

Is there a way to load a <div> already collapsed when the page first loads? You can find my progress so far in this JS Fidle. HTML <button id="aa">Toggle it up</button> <div id="test">TEST</div> CSS div { background ...

CSS Positioning of Fixed/Absolute <div>

Is there a way to keep a div fixed at the bottom of the viewport by using position: fixed; bottom: 0;, then switch it to position: absolute; bottom: 0; when scrolling reaches the footer? I need the div to remain at the bottom of the viewport initially, an ...

Using jQuery and Ajax to fade in content after all images and other assets have finished loading

I've encountered an issue with loading pages via ajax for users with custom URLs. For example, a profile is usually found at http://example.com/users/Dan, but if a user has a custom URL like http://example.com/DansCustomURL, I need to fetch their desi ...

Error message: ngRepeat does not allow duplicate elements in an array

Upon review, I discovered this particular piece of code: <!DOCTYPE html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <body> <script> var app = angular.module("myS ...

Header text refuses to cooperate and center itself

Struggling to find the best way to center the "Header" text while keeping the icon in place. I've tried using text-align: center;, but it's not producing the desired results. Could anyone provide guidance on how to achieve this? Thanks. IMG: ...

Steps to transform an HTML form template into an HTML string

Below is an example of an object I am working with: $scope.item ={ fieldName:'First Name', fieldModel:'user.firstname', placeholder:'enter your name' } I intend to create an html form template as ...

Enhancing the appearance of div content in Angular by utilizing local template variables and material elements

Within this particular implementation, I have utilized an md-sidenav component with a local template variable #sidenavsearchArea. <md-sidenav #sidenavSearchArea align="" mode="push" opened="false"> sidenav content here.. </md-siden ...

HTML5's drag-and-drop feature, including nested targets, allows for intuitive and interactive user

I'm currently implementing HTML5 drag and drop functionality. I have a parent div that is droppable, and inside it, there is another child div that is also droppable. <div id="target-parent"> <div id="target-child"></div> </d ...

Tips for Implementing Color-coded Manchu/Mongolian Script on Your Website

My journey began with a seemingly straightforward task. I had a Manchu word written in the traditional script and I wanted to change the color of all the vowels in the word (ignoring ligatures). <p>ᠠᠮᠪᡠᠯᠠ ᠪᠠᠨᡳᡥᠠ</p> < ...

What is the best method for combining elements from various arrays into a single array?

I have an array of objects in Vue that I need to loop over. Each object contains a key value pair where the value is another array. My goal is to iterate through each object, and then within each object, loop over the arrays to gather all unique items into ...

Utilizing JQuery to ensure that rows have consistent height in two dynamically generated tables

To meet my specific requirements, I am tasked with creating two separate tables that will contain the same number of rows but different data. The first table will display Item Information, while the second table will provide consolidated information about ...

Prevent IonContent from scrolling to the bottom or top when using Ionic framework

In my Ionic app, I have a long text page with 2 buttons that trigger the actions scrollToBottom and scrollToTop. Due to the length of the page, I have set the scroll duration to be 30 seconds. I am facing two issues here: How can I stop the scrolling ...

Synchronize the hiding and displaying of elements with a corresponding element

If I had a pair of <div> elements set up like this: <div class="overlay"></div> <!-- More content here --> <div class="popup"></div> Whenever the div.popup element is displayed with display: block, I want the div.overl ...

Enhance the appearance of specific wordpress usernames by adding a touch of "flair" next to them

I'm looking to add a special "flair" next to specific users on my WordPress website, similar to how YouTube distinguishes verified users. I have the CSS for changing the color on hover, but I need help keeping it positioned correctly. Examples from Y ...