Utilize the nth-child() selector to style list items within a specific tag

How can I style li tags using nth-child when these li tags are nested within a tag like this:

<ul>
    <a href=""><li>this is a test</li></a>
    <a href=""><li>this is a test</li></a>
    <a href=""><li>this is a test</li></a>
    <a href=""><li>this is a test</li></a>
</ul>

I have tried styling them like this:

a:nth-child(even){
    background-color: #f5f5f5;
    border-right: 2px solid #666;
}
    
.li:hover{
    background-color: #555;
    color: #fff;
}

However, it doesn't seem to work. I believe the li tag should be a direct child of the ul tag for it to work properly.

Answer №1

Your CSS code was targeting elements with the class "li" (such as <div class="li">), not the li elements themselves.

It is correct that li should be the direct child of ul tag:

ul li:nth-child(even) {
    background-color: #f5f5f5;
    border-right: 2px solid #666;
 }

ul li:hover{
    background-color: #555;
    color: #fff;
}
<ul>
    <li><a href="">this is test</a></li>
    <li><a href="">this is test</a></li>
    <li><a href="">this is test</a></li>
    <li><a href="">this is test</a></li>
</ul>

If you only want the background styles to apply to the links and not all list items:

ul li:nth-child(even) a {
    background-color: #f5f5f5;
    border-right: 2px solid #666;
}

ul li:hover a {
    background-color: #555;
    color: #fff;
}
<ul>
    <li><a href="">this is test</a></li>
    <li><a href="">this is test</a></li>
    <li><a href="">this is test</a></li>
    <li><a href="">this is test</a></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

Can the SVG def element be modified?

In my SVG file, there is a defs element structured as follows: <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 250 330" onclick="tweenGloss()"> <defs> <linearGradient id="grad" x1="174.6 ...

Display exclusively the chosen option from the dropdown menu

I am facing an issue with the select input on my webpage. Whenever I try to print the page, it prints all the options of the select instead of just the one that is selected. Can someone please guide me on how to modify it so that only the selected option ...

Arrangement of elements using Bootstrap's order and column classes beneath

I'm having trouble with a bootstrap layout. Here is the layout I need to achieve on different screen sizes: On mobile, I want: Title Image Text And on desktop, I want: Image Title Text However, the title div always takes up the image he ...

Steps to ensure that a singular button is visible within a specific jQuery function

I am looking for a solution regarding the placement of a button labeled "add to profile" within a jQuery div. Specifically, I would like this button to only appear after the last "next" button in the sequence. For example, after question 1 and its respect ...

Is there a way to alter the styling of a React element without resorting to querySelector?

Just diving into React and I'm faced with a challenge - how to update the CSS of a class based on a condition. let customizeColumn = document.querySelector(".main-table-body"); !expandFilter ? customizeColumn.setAttribute("style", ...

How to prevent Bootstrap Carousel from automatically sliding on mobile devices without using JavaScript

Is it feasible to halt the autoslide feature on mobile view through CSS? Perhaps using a media query for extra small (xs) screens and then adjusting a carousel element...? <div id="carousel" class="carousel slide" data-ride="carousel"> <!- ...

Using jQuery to adjust the scrollLeft property of a div upon clicking a label

I am attempting to enable horizontal scrolling on a div when a label is clicked. Unfortunately, my current code is not functioning properly. It appears that the code is unable to detect when a label has been clicked and is also failing to scroll the div as ...

Experiencing difficulties with toggling the visibility of div elements when an onchange event occurs

I am experiencing some difficulties triggering an on-change event in the department drop-down field and I'm not certain if I've written it correctly. The error message indicates: Uncaught TypeError: Cannot read property 'style' of null. ...

ClearMedia Logo PredictiveText MaterialUi

I am trying to display the MUI Autocomplete clear text icon every time I enter text. Currently, it only shows when I select an option, not when I type anything. https://i.sstatic.net/ekwES.png https://i.sstatic.net/8wkpm.png ...

The issue with style.background not functioning in Internet Explorer is causing complications

I am currently developing a JavaScript game that involves flipping cards. However, I have encountered an issue with the style.background property. It seems to be functioning correctly in Chrome but not in Internet Explorer. Here is the problematic code sn ...

What is the reason behind the default font size being longer than 1em?

During my experimentation, I encountered an issue. I set the height of a div to 1em, expecting it to contain the entire text within. However, it did not. In my browser, 1em is equivalent to 16px. On not specifying the height of the div, it successfully co ...

Video streaming to mobile browsers is experiencing issues due to incomplete HTTP headers being sent

I have successfully implemented a solution to stream fragmented mp4 video to a mobile browser client using the Nancy MVC framework. The code is functioning as expected. However, one scenario I am facing is that the video being streamed is generated simult ...

Delivering JSON and HTML safely to JavaScript applications

As I brainstorm secure methods for delivering HTML and JSON to JavaScript, my current approach involves outputting JSON in this manner: ajax.php?type=article&id=15 { "name": "something", "content": "some content" } However, I am aware of the ...

Utilize regular expressions to substitute content with HTML tags within a directive

While working with Angular JS to iterate through Twitter tweets using ng-repeat, I encountered the need to highlight certain parts of the tweet string such as @tag and #hash. To achieve this, it was suggested to utilize replace method to wrap these element ...

Visit a webpage on my site

Is it feasible to embed a website within another website? Suppose I have my index.html file. What if in the center of that file, we include an iFrame or similar element that loads , allowing users to explore Google directly on my website? ...

js - stop form submission

After clicking on a button with type="submit", the front-end validation starts, but I also need to perform server-side validation using Ajax. This includes checking if the name is already being used by another person. The issue arises when switching the b ...

How the Material-UI Tooltip zIndex takes precedence over the MenuItem in the Select component

My issue is quite straightforward. I have a <Tooltip> component wrapping a <Select> component, and when I click the Select, the tooltip appears above the MenuItems like this: Expected behavior: https://i.sstatic.net/VOVmf.png Unexpected beha ...

Arranging items in a flex container

My goal is to achieve the following layout with flexbox: #box { display: flex; flex-wrap: wrap; flex-direction: row; justify-content: center; justify-items: center; align-items: center; } #spotLight img { width: 15%; height: 15%; } # ...

Show the user's input on the canvas

Currently, my setup consists of 3 elements - a text field and a submit button at the top, with a canvas below them. I am looking for a way to showcase user input on the canvas after they click the submit button. If you have any JavaScript suggestions, I w ...

Find the identifier that does not currently exist in the collection of objects

There is a situation where I have an array and an object that consists of arrays of ids, which are essentially permission objects. My goal now is to extract the ids that do not exist in the given object. Can someone assist me with devising the necessary l ...