Can CSS namespace attribute selectors be used with XHTML/HTML elements?

I am trying to customize elements that have the xlink:href attribute in XHTML, but I am facing some difficulties with it. Here is the code I tested:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xyz="http://xyz.org">
<head>
<meta charset="UTF-8" />
<title>css namespace</title>
<style>
body { font-size: 20px; } /* Oops! A normal rule must not precede @namespace rules! */
@namespace html "http://www.w3.org/1999/xhtml";
@namespace xlink "http://www.w3.org/1999/xlink";
@namespace xyz "http://xyz.org";
html|p {
  color: blue;
}
[xlink|href], [xyz|href] {
  cursor: pointer;
  text-decoration: underline;
  color: red;
}
xyz|p, xyz {
  display: block;
}
</style>
</head>
<body>
<!-- typos: 'xref' should be 'href', thank BoltClock's answer! -->
<p xlink:xref="aaa">&lt;p xlink:xref ...</p>
<p xyz:xref="aaa">&lt;p xyz:xref ...</p>
<!-- correctly styled elements: -->
<p xlink:href="aaa">&lt;p xlink:href ...</p>
<p xyz:href="aaa">&lt;p xyz:href ...</p>
<xyz:p xlink:href="aaa">&lt;xyz:p xlink:href ...</xyz:p>
<xyz:p xyz:href="aaa">&lt;xyz:p xyz:href ...</xyz:p>
<xyz xlink:href="aaa">&lt;xyz xlink:href ...</xyz>
<xyz xyz:href="aaa">&lt;xyz xyz:href ...</xyz>
</body>
</html>

Upon testing in chrome 34 and firefox 30, I observed that the [xlink|href], [xyz|href] style rule does not get applied to XHTML's <p> elements, but works for <xyz:p> and <xyz> elements.

What could be causing this issue? Do namespaced CSS attribute selectors function differently with XHTML?

Update:

Namespaced CSS attribute selectors do work with XHTML, but not with HTML files. In my code, there were two issues that needed addressing:

  1. There were typos in attributes xlink:xref and xyz:xref (thanks to BoltClock's answer). The code has been updated accordingly.

  2. A regular CSS rule should not precede any @namespace rules, otherwise the @namespace rules become invalid (my original post omitted the font-size rule at the beginning). Refer to CSS Namespaces Module Level 3:

    Any @namespace rules must follow all @charset and @import rules and precede all other non-ignored at-rules and style rules in a style sheet.

Update 2: For HTML files, which lack support for XML namespaces, namespaced CSS selectors generally don't work. However, because qualified element/attribute names are treated as simple names in HTML, these selectors will function in HTML:

[xlink\:href]:hover, [xyz\:href]:hover { ... }
xyz\:p, xyz { ... }

Interestingly, namespaced selectors targeting the XHTML namespace still work with HTML files, like so:

@namespace html "http://www.w3.org/1999/xhtml";
html|p { ... }

An exception to this is foreign content such as SVG, as suggested by @Alohci.

Answer №1

It appears that there is a mistake in either your HTML or CSS setup.

The attribute selectors are searching for elements with href attributes, but the <p> tags have xref attributes instead of href, causing them not to match.

However, the <xyz:p> and <xyz> elements do have href attributes, which leads them to be the ones that meet the criteria of your attribute selectors.

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

tips on encrypting css/js file names

Can the filename of the css/js file be encrypted? I have noticed that when I view the source of the website, the filenames of the css and js files look encrypted like this. <link href="/assets/css/builds/73e15c8a3cf6409214bbf8a742e9b5d41403226617.css" ...

The ability to choose only one option in a checkbox within a grid view column

In my grid view, I have a checkbox in the last column. I am trying to ensure that only one row can be selected at a time. <tr *ngFor="let permit of PermitDetails" (click)="GoByClick(permit)"> <td style="text-align: center">{{permit.TVA_BAT ...

Steps for executing the jQuery script are as follows:

A friend shared this code with me and it's working perfectly here http://jsfiddle.net/WVLE2/ I tried replicating it on an HTML page below, but unfortunately, it's not working. I'm still learning jQuery and would appreciate any help on how t ...

Ensuring DIVs are completely contained within their parent DIV using Fullscreen CSS and a height of

I found a nearly-perfect design using the code below: https://i.sstatic.net/56i2E.png This code provides the layout mentioned: <!DOCTYPE html> <head> <meta charset="UTF-8"> <link rel="stylesheet" href="http://maxcdn. ...

Creating glitchy dotted lines in an HTML Canvas with the help of translate and scale techniques

I'm working on creating a canvas where users can draw symmetrical lines with their mouse. However, when I use the transform and scale attributes in the draw function to achieve this effect, it creates small gaps in the line and makes it look less smoo ...

Incorporating a JavaScript variable into an inline HTML onclick event

Having trouble passing a variable into an inline onclick function. I've tried researching and following suggestions from this link, but encountered errors (Uncaught SyntaxError: missing ) after argument list): pass string parameter in an onclick func ...

Is there a more efficient method for replacing all attributes on cloned elements?

While this code does the job, I can't help but feel like it's a bit outdated. I'm not very experienced with JS/JQuery and only use them when necessary. My approach involves cloning an element and then replacing all of its attributes with on ...

What could be the reason behind encountering an NaN error while using these particular functions?

I recently delved into the world of JavaScript, starting my learning journey about two months ago. While going through a few tutorials, I stumbled upon an intriguing project idea. However, I've hit a roadblock that's impeding my progress. Every t ...

Is it possible to alter the style of the <input type="file"> element?

After attempting to modify the HTML form input type file, here is the snippet of code I used: In HTML, within form id = form <input class="upload_file" type="file" id="file" name="file" /> .CSS I experimented with both approaches: .upload_button{ ...

How can you turn a SWFObject into a clickable link?

Is there a way to create a clickable link using a .swf flash file with swfObject? I want the entire video to be clickable, but I'm struggling to figure out how to do it. If you take a look at this example here: , you'll see that they were able ...

Optimize your HTML with Meleze.web's ASP.NET MVC 3 Razor minification and compression features

I came across meleze.web on NuGet, which claims to remove extra white spaces in generated HTML results. However, after adding the necessary configuration to my web.config file and running the application, I have not seen any changes in the result. Are th ...

Looking for assistance with changing the class of a span when a radio button is selected

I'm facing an issue with toggling the class of a <span> when a radio button is clicked. <html> <head></head> <style type="text/css"> .eHide { display:none; } </style> <script type="text/javas ...

Is it possible to divide searches into separate divs?

How can I display the results of my AJAX live search in different result divs for styling purposes? I want to style the results separately and have them appear one at a time. However, the current setup with 'element.style' is causing issues. Is ...

Mobile version experiencing issue with Navbar not collapsing

The dropdown navigation bar on the mobile version of my website is not functioning properly. Despite several attempts, I have been unable to figure out a way to make it work effectively. <!DOCTYPE html> <html lang="en"> <head> & ...

Managing the gaps between words with CSS

Having trouble with the word spacing on your webpage bio? Check out the image below: https://i.sstatic.net/ZyCe6.jpg Looking for some advice on how to adjust your CSS for better word spacing? Here are some tips: .column1 { float: left; margin- ...

CSS clearfix doesn't appear to be functioning properly

I've been experimenting with using clearfix instead of the clear both property, but it's not working as expected. Despite following tutorials, the issue persists. How can I troubleshoot this and make it function properly? * { marg ...

Error: The EJS compiler encountered a SyntaxError due to an unexpected token || in the show component file located at /var/www/html

I'm currently working on a project inspired by Colt Steele's YelpCamp creation during his Udemy Web Dev Bootcamp. Everything was going smoothly until I tried to refactor some code towards the end of the course using YouTube tutorials. Now, whenev ...

Every time I adjust the browser height on my landing page, the elements start to overlap each other

Something strange is happening - as I shorten the height of the browser, elements on the page start to overlap. I've tried using Firebug to troubleshoot, but haven't had any luck so far. Maybe someone here can help me out! EDIT: It seems that e ...

Adjusting the page width to ensure compatibility with all devices

I am facing difficulties with CSS regarding the properties width and margin. I am developing a webpage that should display properly on all devices, including PCs, smartphones, and tablets. I am using HTML <meta name="viewport" content="width=device-widt ...

Implementing the Upload Feature using AngularJS

Currently, I'm facing a challenge in implementing an upload button on my webpage using AngularJS and Bootstrap. Specifically, I am having trouble assigning the (upload) function to that button in AngularJS. The goal is for the button to enable users t ...