How to Style an Element Based on Fragment Identifier in Href Attribute Using CSS

I've implemented the tabview feature from the YUI3 library.

This is how the markup code appears:

<div id="demo" class="yui3-tabview-content">
  <ul class="yui3-tabview-list">
    <li class="yui3-tab yui3-widget yui3-tab-selected">
      <a href="#foo" class="yui3-tab-label yui3-tab-content" tabindex="0">foo</a>
    </li>
  </ul>
  <div class="yui3-tabview-panel">
    <div id="foo" class="yui3-tab-panel yui3-tab-panel-selected">
      <p>
        foo content
      </p>
    </div>
  </div>
</div>

You can view a demo of this markup here: http://jsfiddle.net/zb6su/

The challenge I'm facing involves writing JavaScript code to select a tab based on the tabview-panel. For example, if I have the id foo, I want to select the tab with a[href=#foo].

Unfortunately, using a[href=#foo] as a CSS selector doesn't work because # denotes Ids. I also tried using a[href="#foo"] without success. Is there a way to select a link based on the URL fragment in the href attribute using a CSS selector?

I could loop through all nodes and check the href attribute in JavaScript, but I'd prefer a CSS solution if possible.


Update: After further testing, it seems that the selector does work. The issue lies with FireFinder (an extension I am using to test selectors).

Answer №1

Using jQuery, the same selector works perfectly (see fiddle).

$('a[href="#foo"]').text();

The result is "foo". You can achieve the same with CSS as well - check it out:

a[href="#foo"] {
    color:red;
}

It appears that the issue was more related to F21's FireFinder rather than the usage of a[href="#foo"]. Problem solved.

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

Using jQuery to apply the "a" class with the addClass method

The html structure below is giving me trouble: <div class="sub"> <a href="#" id="people">People</a> </div> The CSS for the "a" element is as follows: color:#999999; font-size:31px; I am attempting to use jQuery to change ...

Stylish progress bar using CSS animations

I am trying to make the progress bar expand from 0% width to 50% width in a span of 2 seconds. Here is the code I have written: <style> #progressbar { background-color: #000000; border-radius: 8px; padding: 3px; width: 40 ...

How to use Jquery to dynamically alter cell backgrounds in a table based on their values?

I successfully imported the table from MySQL to HTML, as shown below: My script is designed to highlight the two lowest and two highest values in each column: $(document).ready(function(){ var $table = $("#tbTodos"); $table.find("th").each(functio ...

Ways to retrieve and contrast the border style of an image utilizing javascript

I'm currently attempting to identify images on my webpage that have the style border: 1px #c6c6c6 solid; let images = document.getElementsByTagName('img'); for (let i = 0; i < images.length; i++) { if (images[i].style.border === "1px ...

Tips for positioning the search bar on the opposite side of the navbar in Bootstrap 5

In my navbar, I have items aligned on the left and a search bar aligned on the right. However, when I try to move the items to the right using the ms-auto class, only the items move and the search bar stays in place. How can I adjust the alignment so that ...

Customizing text appearance with innerHTML in JavaScript: A guide to styling

Below is the code I have for a header: <div id="title-text"> The Cuttlefisher Paradise </div> <div id="choices"> <ul> <li id="home"><a href="#">Home</a></li> <li id="contact">&l ...

Tips for incorporating Action Links into your CSS workflow

<li class="rtsLI" id="Summary"><a href="javascript:void(0);" onclick="javascript:rtsXXX.OnClientTabSelected(this‌​, 0);" class="rtsLink"><span class="rtsTxt">Test</span></a></li> I have made a change by replacing th ...

What is the method for defining CSS styles for child elements using the parent element's style attribute?

Here is an example: <div style="SET IMAGE ATTRIBUTES HERE!"> <img src="http://somesite.com/someimg.jpg"><br /> <img src="http://someothersite.com/someotherimg.jpg"><br /> ... </div> With my dynamic ASP.NET ...

The scroll function within the inner div is malfunctioning on the Firefox browser

Scrolling within inner div is not functioning properly in the Mozilla Firefox browser. However, it works seamlessly in Chrome. Is there a workaround for enabling scrolling in Firefox? Here is the fiddle link: http://jsfiddle.net/t6jd25x9/2/ body { f ...

Navigation Bar Search Box Alignment Problem

Hi there, I'm new to programming and I'm trying to center my search bar with the navigation links beside it within a fixed navigation bar. However, I'm having trouble getting it to work properly. Can someone take a look at my HTML and CSS co ...

Customize arrow direction in AntDVue ant-collapse using simple CSS styling

Is there a way to customize the arrow directions in ant-collapse vue? I recently faced an issue where I couldn't change the arrow directions without using !important (which I try to avoid). Fortunately, we found a workaround for this problem at my wo ...

The perplexity of CSS Menus

I have this HTML code, but I'm not sure how to write the CSS so that the drop-down menu appears when I hover over the main li. <li><a href="#">#</a> <ul class="sub_menu"> <li><a hre ...

Aligning a element at the center is not functioning properly when using margin: 0 auto;

I'm having trouble aligning elements to the center when applying margin: 0 auto. You can view the code here: https://jsfiddle.net/helloworld123/vhhx1o7n/ The goal is to align the elements in the center of the page (.entry-wrap should be centered) and ...

Switch between webpage design for different devices (mobile/desktop) using javascript, jquery, and bootstrap

I am currently working on a webpage that contains various html elements with bootstrap classes applied to them. <div class="col-xs-12 col-lg-4"></div> My goal is to implement a button that, when clicked, will toggle the viewport/layout change ...

Building a DIV Element in the Space Between a Header and Footer Using HTML and CSS

I'm attempting to get my DIV element (MainPageImage) to cover the entire screen area, situated between the header and the footer. Here's my HTML code: body { padding: 0; margin: 0; overflow-y: scroll; font-family: Aerial; font-size: ...

Having trouble adjusting the vertical position of jQuery Spinner buttons

I'm attempting to customize the jQuery spinner with styling applied like this: <span class="ui-spinner ui-widget ui-widget-content ui-corner-all" style="height: 14px;"> <input id="testSpinner" class="ui-spinner-input" name="value" aria-value ...

Steps for positioning the navigation bar beneath header 1

In my HTML code, here is a link to an image: I have successfully completed the sections associated with this image: However, I'm facing an issue now. I am unable to position the navigation bar ("Home About Us...") under the "Art Store" section using ...

Homepage experiencing issues with Jquery slider functionality

I have been attempting to incorporate a slider on my magentogo store, but unfortunately, after following all the code from the demo example of the slider along with the necessary CSS, the slider refuses to work. The CSS is configured to display none, as th ...

What is the best way to extend the final column of a table across the entire width?

Here's the code I'm working with: .frm-find-people table td:nth-child(1) { padding: 5px 15px; width: 100px } .frm-find-people table td:nth-child(2) { border: 1px solid red; } <form class="frm-find-people"> <table> ...

What is the best way to transform this into an HTML readable code?

While browsing through a template, I came across this code and I'm unsure of how to get it functioning. I've tried removing the comments, but unfortunately it doesn't seem to be working as expected. li.dropdown.navbar-cart ...