Utilize CSS to exclusively filter through items

Is it possible to use CSS alone to filter a list of items based on specific links being clicked? I have a list that should display only certain items when corresponding links are clicked.

HTML:

<!-- Header links -->
<a href="#" class="all" tabindex="-1" title="All" >All</a> 
<a href="#" class="web" tabindex="-1" title="Web">Web</a> 
<a href="#" class="graphic" tabindex="-1" title="Graphic">Graphic</a> 
<a href="#" class="music" tabindex="-1" title="Music">Music</a>

<!-- Content -->
<li class="web"><a href="#">Web</a></li>
<li class="music"><a href="#">Music</a></li>
<li class="graphic"><a href="#">Graphic</a></li>
<li class="web"><a href="#">Web</a></li>
<li class="music"><a href="#">Music</a></li>
<li class="graphic"><a href="#">Graphic</a></li
<li class="music"><a href="#">Music</a></li>
<li class="graphic"><a href="#">Graphic</a></li>

CSS

a[class="web"]:focus ~ li:not([class="web"]) {
    height:0px;
    width:0px;
    border:none;
    margin:0;
    display:none;
}
a[class="graphic"]:focus ~ li:not([class="graphic"]) {
    height:0px;
    width:0px;
    border:none;
    margin:0;
    display:none;
}
a[class="music"]:focus ~ li:not([class="music"]) {
    height:0px;
    width:0px;
    border:none;
    margin:0;
    display:none;
}

I am trying to wrap a div around the header links, but this seems to interfere with the functionality. How can I make this work seamlessly?

Check out the JSFiddle Demo

Answer №1

Having a div surrounding the a tags is necessary, but it's important to also include the li items within the same container. This is due to the lack of parent selectors in CSS at the moment.

What is the reason for wanting a div around the a headers?

If it's for styling purposes, consider specifying the classes for the a heading links and applying CSS like this:

a.all, a.web, a.graphic, a.music {
  // Your heading css
}

For an example, check out: http://codepen.io/anon/pen/xbPYNX

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

Unsuccessful conversion of JSON data to HTML using json2html in Python

I have been experimenting with converting JSON data to HTML using json2html. The JSON data structure I am working with is as follows: json_obj = [{"Agent Status": "status1", "Last backup": "", "hostId": 1234567, "hostfqdn": "test1.example.com", "username" ...

Creating a Shadow Effect on CSS Triangles

I have created a unique design that resembles a large arrow pointing to the right. <div style=" font-size: 0px; line-height: 0%; width: 100px; border-bottom: 80px solid #8cb622; border-right: 62px solid #dadbdf; "></div> <div style=" font ...

Image loading in NextJS is limited to only Firefox browsers

Images are loading only in Firefox and not anywhere else. Here is the import statement: import gradient from '../public/mountains/gradient.svg' And here is the Image component: <div id='gradient' className={`${styles.bgimage} ${sty ...

Styling for the bootstrap dropdown menu is missing

While attempting to incorporate a bootstrap dropdown menu, I noticed that despite applying the appropriate class to the sublist, the formatting and padding that is typically associated with bootstrap are not being retained. My current setup involves using ...

Place an image in the middle of a div

Here are a few images for you to consider: <div class="builder-item-content row"> <div class="twelve columns b0" style="text-align:center;"> <ul class="upcoming-events"> <li> <a href="http://www.padi.com/scub ...

Using both italic and regular text in an HTML (or ASP.NET) textbox can add emphasis and enhance readability for

Is there a way to achieve this using JavaScript? For instance, if the initial text is 'Axxxxxx', I want it to be displayed in a textbox with normal style for the first letter and italic style for the rest like 'A*xxxxxx*' If the initia ...

Mobile Image Gallery by Adobe Edge

My current project involves using Adobe Edge Animate for the majority of my website, but I am looking to create a mobile version as well. In order to achieve this, I need to transition from onClick events to onTouch events. However, I am struggling to find ...

What is the best way to eliminate the header and side navigation in a master page design?

I've set up a master page called wrap.master which is connected to multiple content pages. I'm looking to create a new content page and link it to the master page. However, I want this new content page to be independent from the header and side n ...

Issues with the responsiveness of Bootstrap 4.5.0 grid system

While following a Django tutorial, I encountered an issue with Bootstrap cards and CSS styling. Using the latest version (Bootstrap 4.5.0), I noticed that my responsive grid was causing the cards to overlap as I increased the screen size. Comparing it to t ...

The functionality of activating a button is not functioning as expected in AngularJS framework

Next to the question I have linked here, I am exploring a different approach. As a beginner in AngularJS/Cordova/Ionic, my goal is to achieve different outcomes when the "Eingepasst" button is clicked, each with unique logic compared to "Gewonnen" or "Ver ...

Why does box-shadow only display on the bottom border and not the right and bottom border?

I want to add a shadow effect on the bottom and right sides of a specific div. #navigation-and-slideshow { overflow: hidden; background-color: #fff; padding: 10px 1%; box-shadow: 2px 2px 5px #222; } However, I am only seeing the shadow ef ...

After filling out the form, the user's entered information is instantly shown on the same page

I want to create a form where my friend can input information that will be displayed on the same page. For example, when he enters the title in a text field and clicks submit, it should appear in the title box. Additionally, he should be able to enter va ...

I am currently encountering a glitch with my video VTT file, as the subtitles are not appearing on screen

welcome to the world of coding WEBVTT 00:01.000 --> 00:8.000 coding is my passion join me on this journey ...

XSLT selecting the remaining elements in a tokenized array

In my XSL document, I have a requirement to generate a hyperlink with text that includes a line break, resulting in two separate sentences. To achieve this, I am utilizing the tokenize function to split words based on whitespace character. <xsl:variab ...

Display the elements of a div at a reduced size of 25% from its original dimensions

I'm currently developing a JavaScript-based iOS simulator that allows users to view their created content on an iPhone and iPad. This involves using AJAX to load the content/page into the simulator, but one issue is that the simulator isn't life- ...

Looking for assistance in translating HTML code into an XpathQueryString?

While following a tutorial by raywenderlich on how to parse HTML in Xcode, I encountered an issue. I was able to parse his website successfully, but I'm struggling with the concept of Xpathquery. All I need is to extract the data from a table. Below i ...

I prefer not to receive the parent's opacity settings when styling a child element in CSS

Is there a way to prevent the child element from inheriting the opacity of the parent in CSS? I have a parent div and a child div inside it. I want to set the opacity for the parent div, but not have it affect the child div. Does anyone know how I can ac ...

Combining Bootstrap navbar with React Router for seamless navigation

I am new to using React and I am having trouble linking pages with Bootstrap navigation. I am attempting to use React Router DOM to navigate to different pages on click, but the app is not compiling because BrowserRouter is not being imported properly. I c ...

Issue with the formatting of the disabled button

I am facing an issue with styling a disabled button. Whenever I try to apply custom styling, it reverts back to the default styling. I have already cleared my cache but the problem persists. The button is disabled using JavaScript with document.getElementB ...

Error message: "Unexpected token" encountered while using the three.js GLTFLoader() function

I have a requirement to load a .glb model into three.js by using the GLTFLoader. I was able to successfully create a rotating 3D mesh on a canvas without encountering any errors in the console. However, when I tried to replace it with the .glb model, I enc ...