What is the best way to choose all list items that do not contain an unordered list element?

I am trying to target the <li> elements that do not have an immediate parent of <ul>.

Even after using the selector mentioned above, it doesn't seem to work as expected.

.level1:not(:has(ul)) {
  background: red;
}
<ul>
  <li class="level1">Link1</li>
  <li class="level1">Link2
    <ul>
      <li>Link2</li>
    </ul>
  </li>
</ul>

Answer №1

To style a specific class within an li element that does not contain the ul tag, you can add a new class and apply CSS to it.

.some-class {
  background: red
}
<ul>
  <li class="level1 some-class">Link1</li>
  <li class="level1">Link2
    <ul>
      <li>Link2</li>
   </ul>
  </li>
</ul>

EDIT

If you are unable to modify your HTML markup and this is your final code, you can use :first-child or :first-of-type in your CSS selector like below:

.level1:first-child {
  background: red
}
<ul>
  <li class="level1">Link1</li>
  <li class="level1">Link2
    <ul>
      <li>Link2</li>
    </ul>
  </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

Add styles to IMG elements only if there are no existing styles already applied

Feeling a bit embarrassed asking such a basic question, but I seem to be struggling with it. I want to use CSS to style all IMG elements on my page that do not have any other class applied to them. For instance, I want all images to have a red border by ...

Content in the Bootstrap carousel vanishes without warning after a period of time

I am facing an issue with a carousel I added to a section of my website using HTML, CSS, and Bootstrap 5. The problem is that after some time, the content and the carousel inside that section disappear. Reloading the page temporarily fixes this issue but ...

Is it possible to have a page transition when clicking a form button

I've been experimenting with this on my website Everything is functioning well, except when I add data-ftrans="slide" to a form button. For example: <form action"http://google.com"> <button data-ftrans="slide" type="submit" style="h ...

Why doesn't the CSS height of Textarea work in Internet Explorer versions 6 and 7?

Here is my code: <!DOCTYPE html> <html> <head> <title>Demo</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style type="text/css"> body { background: n ...

How to arrange DIVs and expand containers?

Is there a way to position a common div at the bottom of the page without forcing it out of its container, without specifying the container's height, and without setting its position to absolute? I want to place a div (alpha) at the bottom of the pag ...

Tips for adding a CSS class to an element while excluding any nested or child elements with the same style

Hey, I'm having some trouble applying a style to a body element without affecting a specific nested element selected by a certain selector. I attempted to use the :not pseudo-class but it doesn't seem to work for child elements. Here's an ex ...

Tips for modifying navbar appearance as user scrolls?

I am currently working on a website using Bootstrap 4. The goal is to create a navbar with a transparent background that transitions to white smoothly when the user scrolls down. I want the navbar to return to its initial state when the user scrolls back u ...

Improving the layout of text by adjusting the width within a flex-box styled card

I have a card with the style property "display: flex;" applied to it along with other stylings, all of which are checked in the snippet below. My goal is for the text within the card to move to a new line once its width reaches either 120px or 7 ...

json success function not being triggered

I am facing an issue with executing the success function in my code while trying to post the value of an input box and retrieve the value of a checked radio button. The objective is to perform a query based on which radio button is checked. Here is the HT ...

What are the best techniques for resizing a Text Field with media queries?

The contact form is responsive and functioning correctly for the textarea field, but there are issues with the input type=text fields on smaller screens. I attempted to address this by incorporating media queries. Below is my code: <!doctype html> ...

The peculiar characteristics of the dragLeave event in various web browsers

I observed a peculiar behavior while working with drag events in Internet Explorer 11. It seems that adding a 'width' property to the elements triggers the dragLeave event, whereas without it, the event does not fire. Can anyone shed light on why ...

"Is it possible to move the text on the canvas by dragging it to where you want it to be

Seeking help again after an unsuccessful attempt. How can I allow the user to add text to the canvas by dragging it to their desired location? For example, if they input text somewhere, it should appear on the canvas and then be draggable to any position ...

Automatically adjust height directive to fill up the remaining space

Is there a way to dynamically adjust the height of an element to fill the remaining space within its container? In my current layout, I have a fixed-height header (or menu) in pixels, a content area that may overflow the window height and require scroll b ...

Tips for ensuring that lengthy text remains within the confines of a table td by implementing CSS styling

What is the best way to ensure that 10,000 characters will fit inside a <td> without overflowing? I've experimented with various solutions such as: word-wrap: break-word overflow-wrap white-space: initial ...and several others, but nothing s ...

CSS Image Width Percentage Guide

After incorporating Bootstrap into my ASP.NET webpage, I ran into issues when using percentages in the width attribute. Strangely enough, everything worked fine when I switched to pixels instead of percentage. Check out the images below for a visual refere ...

Tips for changing the value of a textview in an HTML document using Java

I'm currently working on a project that involves changing the text value of an HTML file's text view using Java. Within my Java code, I have a value stored in a String variable like this: String message ="HAI"; My goal is to input this "HAI" i ...

Updating the UI in Angular for keyboard events is not working as expected

Looking to update the page based on keyboard events. I've connected the keyboard event using $window.document.onkeydown. (I understand it's not ideal, but it should still work) However, despite changing the model, I'm not seeing any updates ...

Hide the li tag by using a class to make it disappear

I'm looking to hide the dropdown class, which will only be used for specific li tags. I attempted to do this with the following CSS, but it didn't work: .dropdown{ display:none; } Is there a correct way to achieve this? Am I missing somethi ...

Tips on how to ensure a function in jQuery completes before executing another function

My query relates to a div called "#ImageBingingDiv" that is initially set to display:none. I use a fadeIn effect and then adjust the HTML content to include an image. However, when trying to add elevateZoom to the image tag, it doesn't work as intende ...

Steps for downloading a PDF file in PHP from a MySQL-stored path

I have developed a script that allows me to write PDF files to the server. The script sends the file to a specific directory named DATA and then stores the filename in a MySQL database. $target = "data/"; $target = $target . basename( $_FILES['file& ...