What is the best way to style the first child element that does not contain a specific class?

If I have the following HTML code snippet:

<ul id="some_list">
    <li class="hide">Do Not Target This</li>
    <li class="hide">Do Not Target This</li>
    <li>Target This</li>
    <li>Do Not Target This</li>
</ul>

Is there a way to select the first child element within a list that does not have the "hide" class? For example:

#some_list>li:not(.hide):first-child {
    color: #777;
}

Unfortunately, this selector does not produce the desired result.

Fiddle

Answer №1

To target the initial li element that does not contain the .hide class, you can utilize CSS adjacent sibling selector:

VIEW EXAMPLE.

li:first-child:not(.hide),
li.hide + li:not(.hide) {
    background-color: gold;
}

Additionally, make sure to verify if the first list item (first child) has the .hide class present.

Answer №2

Here is the solution you need

#some_list>li.hide + li:not(.hide){

    color: #CCC;

}

Answer №3

To accomplish this task in JavaScript, you could follow these steps:

View the code snippet on JSFiddle

<script>
var parentElement = document.getElementById('some_list'),
    childElement = parentElement.getElementsByTagName('li');

childElement[2].style.color = 'blue';
</script>

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

Properly spaced website images

I'm trying to display my images side by side with a slight margin in between them. However, when I apply a `margin-right: 10px;` to each div, the last image doesn't line up properly with my title bar. Is there a way to create spacing between the ...

Ways to create a responsive header image on a website

I recently incorporated a header image on my website using the following code: #header_area { background: url('my_header_image.com'); background-repeat: no-repeat; background-position: top center; } Although my website is responsive ...

The optimal method for selecting a button from a group of buttons on a calculator using pure JavaScript

I have developed an HTML/CSS code inspired by the Mac/Apple calculator design. It features buttons organized in 5 rows using flexbox. You can view my code on this codepen: <div class="wrapper"> <div class="calheader"> ...

What is a straightforward method to showcase prototype nodes in PHP?

It's like a different flavor of UI, function DisplayUI($elements) { } This function will display the specified elements with name, id, and type in a flexible manner. UPDATE Create an array and get HTML as output. Has anyone experimented with thi ...

Mastering the Art of Utilizing $(this) in jQuery

$(".item_listing").hover(function(){ $(".overlay_items").display(); },function(){ $(".overlay_items").hide(); } ); This is my jQuery code. I am trying to display the "overlay_items" div when the "item_listing" div is hovered ov ...

What is causing the issue with this pseudo class selector not functioning properly?

I've been experimenting with the :after selector to create an inside-border effect in every div I hover over. However, I'm facing an issue where the ":after" pseudo-class doesn't seem to work. Any insights on why this selector isn't fun ...

Unleashing the Power of Aurelia's HTML Attribute Binding

I need to bind the "required" html attribute in my template and model. Along with passing other information like the label value using label.bind="fieldLabel", I also want to pass whether the element is required or not. However, simply using required="tr ...

Surprising Results when Using getBoundingClientRect()

Check out this code on CodePen In my current project, I'm attempting to position the footer div at the bottom of the page in such a way that it aligns with the maximum value of the bottom coordinates of both the menu and content divs (Math.max(menu, ...

Navigate through information within designated boundaries

In order to achieve vertical scrolling of a sidebar div and its content (3 links) between two points, I have utilized the CSS property position:fixed; top: 100px. While this setup works well initially by positioning the sidebar 100px down from the top and ...

Stopping a parent's event from firing when clicking on child elements without interfering with the child element events

Having read several posts such as this one, I am exploring a method to click on the parent div for it to hide, without hiding when clicking on either the search box or the 'click for event 2' text. I have tried using onclick stop propagation to ...

How can I remove the outline when focusing with the mouse, but still retain it when focusing with the

Is there a way in JavaScript to detect if an element received focus from the keyboard or mouse? I only want the outline to show when it's via the keyboard, not the mouse. If this is possible, how can I override the browser's default outlining be ...

What is the default DTD used if it is not specified in the doctype declaration?

On my webpage, the doctype is specified as: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> No DTD is explicitly set. I'm curious which DTD will be defaulted in IE? It seems that it doesn't function the same way as "http ...

Displaying React components using Bootstrap in the navigation bar

I'm feeling a little stuck here. I'm currently working with React Bootstrap and the Navigation component. One of the routes in my application is the dashboard, which is only accessible after logging in. When a user navigates to the dashboard, I ...

Is there a way to recycle an image and ensure that the web browser only needs to download it once?

Is there a way to effectively reuse the same image multiple times on my website without inefficiently downloading it each time? ...

"Transform the appearance of the character pointer in Atom Editor by customizing the theme

Hey everyone, I need help with a couple of things. I'm trying to figure out how to adjust the color of the cursor in Atom. Any tips on how to do this? Another issue I'm having is with the find function in Atom. The highlighted words that ma ...

After submitting the form, the delete button in Bootstrap 4 has transformed into an unusual shape

I have a panel on my page, inside the panel header there is a red delete button. The panel content includes a form with several buttons (the red delete button is not inside the form, but linked to it with the form="abc" attribute). In the image below, whe ...

Instructions for downloading a file using Front End technology in Java and HTML

I am looking to initiate a file download upon clicking a button on the front-end interface. Front End <td><a name="${flow.name}" data-toggle="tooltip" title="Report" class="generateReport"><span class="far fa-file-excel"></span>&l ...

I am attempting to restrict a text box so that it can only accommodate 4 digits

I have successfully implemented a restriction on a text box to allow only a maximum of 4 digits, along with an alert message if the length is less than 4. The code I used is as follows: <tr> <td id="ExamNumber">ExamNumber </td> ...

Tips on eliminating excess white space or margins beneath a flexible video player

My issue is with a video and some content underneath. When I reduce the height of the video on mobile, there is too much white space below it. @media only screen and (max-width: 600px) { video { height: 80%; } Instead of adding margin-bottom ...

Tips for overlaying text on the background in Next.js:

I'm currently working on creating an image element with overlay text. Check out my jsx code below: <div className={styles.img}> <img src={src} alt="" /> <p>{`(${size})`}</p> </div> And here is t ...