Conceal the initial list item belonging to a particular category

Consider the given HTML code snippet:

<ul class="cart not-empty-cart">
    <li class="cart-item" data-cart-key="0">aaaaa</li>
    <li class="cart_item subtotal">bbbb</li>
    <li class="cart_item delivery_fee">cccc</li>
    <li class="cart_item subtotal">dddd</li>
    <li class="cart_item delivery_fee">eeee</li>
    <li class="cart_item total">fff</li>
    <li class="cart_item checkout">gggg</li>
</ul>

In this code, there are instances of .cart_item.subtotal and .cart_item.delivery_fee that appear more than once. The goal is to hide the first occurrence of each. Specifically, bbbb and cccc li's should be hidden.

An attempt was made using the following CSS:

li.cart_item.subtotal:nth-of-type(1),
li.cart_item.delivery_fee:nth-of-type(1) {
    display: none;
}

This solution did not work as expected. What could be done differently?

Answer №1

The CSS pseudo-class :nth-of-type() selects elements based on their position relative to siblings of the same type (tag name). For more information, check out the documentation.

If you want to hide the second li element, using this pseudo-class won't be helpful.

One approach involves two steps:

  • Hide all instances of a specific selector
  • Show those that come after a matching sibling (the ~ denotes "sibling of" - refer to the docs)

/* Hide all li.cart_item.subtotal and li.cart_item.delivery_fee */
li.cart_item.subtotal,
li.cart_item.delivery_fee {
    display: none;
}
/* Show ones with a preceding sibling of the same classes */
li.cart_item.subtotal ~ li.cart_item.subtotal,
li.cart_item.delivery_fee ~ li.cart_item.delivery_fee {
    display: list-item;
}
<ul class="cart not-empty-cart">
    <li class="cart_item" data-cart-key="0">aaaaa</li>
    <li class="cart_item subtotal">bbbb</li>
    <li class="cart_item delivery_fee">cccc</li>
    <li class="cart_item subtotal">dddd</li>
    <li class="cart_item delivery_fee">eeee</li>
    <li class="cart_item total">fff</li>
    <li class="cart_item checkout">gggg</li>
</ul>


Another user previously suggested a more concise method involving combining these two steps:

  • Select elements to hide that do not have a preceding sibling matching the selector

/*
 * Hide all li.cart_item.subtotal and li.cart_item.delivery_fee
 * if they don't come after a matching sibling.
 */
li.cart_item.subtotal:not(li.cart_item.subtotal ~ *),
li.cart_item.delivery_fee:not(li.cart_item.delivery_fee ~ *) {
    display: none;
}
<ul class="cart not-empty-cart">
    <li class="cart_item" data-cart-key="0">aaaaa</li>
    <li class="cart_item subtotal">bbbb</li>
    <li class="cart_item delivery_fee">cccc</li>
    <li class="cart_item subtotal">dddd</li>
    <li class="cart_item delivery_fee">eeee</li>
    <li class="cart_item total">fff</li>
    <li class="cart_item checkout">gggg</li>
</ul>


In CSS Level 4 selectors, there is the :nth-child(x of y) option, however, it currently lacks widespread browser support.

/* Limited browser compatibility, may work in Safari */
:nth-child(1 of .cart_item.subtotal) {
    display: none;
}

Answer №2

The solution by DenverCoder1 is great, but I would like to mention that you can also achieve this using the :has pseudo-class. It's worth noting that Firefox support for this feature is still limited and requires a flag to be enabled, so it may not be suitable for production just yet.

.subtotal:has(~ .subtotal){
  display: none
}

.delivery_fee:has(~ .delivery_fee) {
  display: none
}
<ul class="cart not-empty-cart">
  <li class="cart-item" data-cart-key="0">aaaaa</li>
  <li class="cart_item subtotal">bbbb</li>
  <li class="cart_item delivery_fee">cccc</li>
  <li class="cart_item subtotal">dddd</li>
  <li class="cart_item delivery_fee">eeee</li>
  <li class="cart_item total">fff</li>
  <li class="cart_item checkout">gggg</li>
</ul>

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

Firebase monitors the authentication status of a user

Trying to maintain a global state in my application based on whether the user is logged in or not has me puzzled. Should I only have a single onAuthStateChanged() in my JavaScript file to monitor state changes and manipulate HTML elements like account deta ...

Having difficulty refreshing a page following button click in PHP

I'm encountering an issue where the PHP page isn't reloading automatically after a specific button is clicked. The changes only take effect when I manually refresh the page. Here's the PHP script in question: <?php // Initialize the sess ...

Set boundaries for square dimensions and placement within the parent container

Struggling to confine a square within specific size constraints while keeping it neatly aligned within the parent container. Balancing these requirements has proven challenging. The layout consists of a main div with two columns on the left and a div occu ...

The slider thumb is not showing up properly on Microsoft Edge

Hey there! I'm experiencing an issue with the range slider's thumb display in Microsoft Edge. It appears to be positioned inside the track rather than outside as intended. Take a look at this snapshot for clarification: https://i.stack.imgur.co ...

I am facing an issue where PayPal buttons are overlapping other elements on my website. Unfortunately, I am unable to control the

Having integrated PayPal buttons on my Magento website, I am facing an issue where the buttons have their CSS classes inline. This causes them to overlap other elements on the page when scrolling. For instance, as I scroll down, the PayPal button overlaps ...

Tips for performing intricate sorting within a jQuery tableSorter

I am facing an issue with my table data structure. Cell1 Cell2 Computers 10 Dell 5 IBM 3 Compaq 2 HDDs 12 Seagate 7 Samsung 3 Test 2 Monitors 18 Seagate 7 Samsung 9 D ...

choose the drop-down option that is consistently set to the first index

I am currently working on a JavaScript code that aims to fetch the selected item's value, but it seems to always point to the 0 index and return the first value. My HTML Code: <html> <head> <title>Simple Extension</title ...

Utilizing lists for selection within a form

Is there a way to customize the appearance of a select box in a form using styled lists or a similar solution that allows for styling with icons and more? I'm having issues with my PHP validation. PHP // Validate priority if(empty(trim($_POST[" ...

Is the scaling of a div with an image affected by odd pixel sizes?

My goal is to create a responsive grid with square images, where the first image is double the size of the others. While I can achieve this using jQuery, I am curious if there's a way to accomplish this purely with CSS. Grid: Here's an example o ...

restructure the HTML based on a jQuery condition

Using jQuery to Refresh HTML Content function(data) { if(data === "importtrue") { $('#rebuildme').//function like .html } else { $('#rebu ...

In Bootstrap, it is important that the number of indicators in the carousel matches the number of images displayed

I have successfully created a Carousel Slider with the help of Bootstrap and Advanced Custom Fields. Everything is functioning well, but I am facing an issue with making the Indicators correspond to the number of images I have. Each indicator needs to be s ...

Adding a filter function to an ngFor loop upon a user's click event in Angular

Just a quick question here. I'm still new to angular so please bear with me. Here is a glimpse of my view page: https://i.sstatic.net/hr1I4.png Initially, the page loads with all the necessary data. The issue arises when a user clicks on any name un ...

Add a style to every div except for the final one

I've attempted to add a unique style to all divs with the same class within a parent div, except for the last one. Strangely, my code doesn't seem to work as expected for this particular case. Can anyone spot what I might be overlooking? Right no ...

Steps to set a default selection from a list of components in Angular 7

Currently, I am in the process of developing a messages page layout where users can select a message from a list on the left side and view its content on the right side, similar to how it is done in Outlook. Additionally, users should be able to reply or c ...

In HTML5, I am trying to figure out the best way to connect my webpage sections to the corresponding navigation links with the help of IDs (such as linking navhome to

I am in the process of redesigning a website to optimize it for mobile devices. Although I have completely restructured the site, I am encountering an issue where the main pages are not displaying properly. The header, navigation menu, sidebars, and a plac ...

The responsiveness of the bootstrap mobile view is experiencing some technical difficulties

https://i.sstatic.net/IuQXa.pngUsing Bootstrap 4 in my project and I have implemented the following bootstrap codes with an external CSS file in the HTML view, <div class="jumbotron rounded-0"> <div class="container"> &l ...

Guide on using Python to save a legitimate HTML page of a YouTube video

When I use requests.get(URL, allow_redirects=True) on websites like Youtube or Reddit, instead of seeing the usual HTML text content that appears when I view the page in a browser, I get a bunch of unexecuted JavaScript. All I really need is the title of ...

Loading content onto a webpage using AJAX

I am currently facing a challenge while attempting to incorporate a file on my server using AJAX. The issue I am encountering is that it disrupts the functionality of my PHP code. The AJAX is implemented on the following page: <div id="content&quo ...

Using HTML <style> in a bash script is a great way to enhance

I am attempting to eliminate the bullet points from HTML code generated by a bash script. I am currently struggling to apply the style across the entire program. Given my limited knowledge of html, I suspect that I may be misinterpreting or incorrectly p ...

Trigger an outer iframe to navigate from a deeply buried child iframe

Seeking assistance with reloading an iframe nested within two additional iframes. I have attempted using parent.document and Response.Redirect, however, neither are achieving the desired result. When utilizing Response.Redirect (URL), it reloads iframe 1 ...