Issue encountered with nth-child selector

I came across the following CSS code snippet:

a {
    display:table;
}

a:hover {
    text-decoration:none;
}

a:first-child {
    color:#00FF00; /* green */
}

a:nth-child(3) {     
    color:#FF0000; /* red */
}
<a href="www.google.com">Google<a>
<a href="www.google.com">Google<a>
<a href="www.google.com">Google<a>

Question: I noticed that the second "Google" is red while the third one is not. Can someone explain why this is happening?

Answer №2

end </a>

*{
    padding: 0;
    margin: 0;
}


a {

    display:block;
}
a:hover {

 text-decoration:none;

}

a:first-child {
 color:#00FF00;
}
a:nth-child(3) {     
 color:#FF0000;      
}
<a href="www.google.com">Google</a>
<a href="www.google.com">Google</a>
<a href="www.google.com">Google</a>  

Answer №3

Take a look at the following link for more information: https://jsfiddle.net/4k5f03yq/

The nth-child cannot be located due to an unclosed tag in the code.

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

Django: The download page content is being downloaded by Internet Explorer instead of being rendered

I'm currently working on a website using the Django Web Framework. Everything looks great when I check it in browsers like Chrome, Safari, and Firefox. However, if I try to open the page in Internet Explorer, I encounter this issue: https://i.sstatic ...

How can I position divs in a way that the top right corner of the child div touches the bottom left corner

I am currently working on implementing a message box feature on my website. When a user clicks on the inbox icon, I want a messages box to appear, similar to how stackoverflow handles it. Stackoverflow achieves this by placing a div outside the ordered lis ...

Identifying all unused CSS class selectors in the code that do not have any CSS rulesets assigned

Is there a way to retrieve a list of all the CSS classes I have used in my code that do not have a corresponding "CSS ruleset" defined for them? For example: /*Definition (or Rule set) for .some-random-class doesn't exist or is ...

Struggling to adjust the width of a div accurately following the population of AJAX content

This particular piece of code is responsible for populating an inner div within an image slider by making an Ajax call : $.ajax({ type: "GET", dataType: 'json', url: $(this).attr('href') }).done(function (result) { ...

Loading necessary CSS when needed in React JS

I am currently in the process of converting a bootstrap template to react framework. My question is, is there a way for me to load stylesheets on demand? For instance, if I have 2 components and I import the same stylesheet separately in both components, ...

What is the best method to eliminate excess space between images on a webpage using CSS?

Currently, I am developing a small project just for myself. The main page will consist of multiple images, each spanning the entire page. I am having trouble eliminating this unwanted spacing: https://i.sstatic.net/7eLl5.jpg Your assistance would be gre ...

I need some assistance, please. Can someone explain why the Bootstrap card is staying aligned to the left in mobile view

I've tried everything, but it's still not working. Can anyone help me out? Here are some photos. BEFORE AFTER When I view the card on mobile, I want it to stay centered. Could there be some missing code that I'm overlooking? I would great ...

switch from material ui lists on/off

Trying to learn React through coding, I've encountered an issue with displaying the 'StarBorder' icon next to folders when clicked. Currently, clicking on any folder displays the 'StarBorder' icon for all folders. Any tips on how t ...

I am having trouble with my 'SELECT DISTINCT' command, it's not functioning correctly

When I attempted to populate my drop down menu with values from the database using 'Select DISTINCT', it only retrieved some of the data instead of all of it. Below is my PHP code: <? $sqlretrive = mysql_query("Select DISTINCT type from ...

`Trying to Implement jQuery Function with No Success`

My webpage includes the following HTML code: <div class="new-nav"> </div> Below is my jQuery script: var div1 = " <div class=\"metro-nav-block nav-block-orange\">\n <a data-original-t ...

Adjust the size of a collapsed element in a bootstrap accordion panel

I am working with a classic bootstrap accordion in my code. I want to adjust the width of the expanded ul/li elements when a h5 element is clicked (decrease width). Currently, the width expands fully when the h5 element is clicked. Can anyone provide ass ...

Can you explain the distinction between escapeXml and escapeHtml functions?

When it comes to escaping characters in JSP pages, I am wondering which is the better option - using escapeXml or escapeHtml? ...

Postback does not reload all controls in the NameValueCollection

Recently, I designed a custom User Control in asp.net. public class InputMask : CompositeControl, IPostBackDataHandler, IPostBackEventHandler Imagine the custom control is enclosed within a div element: <div runat="server" id="inputArea"> < ...

The $(window).load(function() function is unable to run once the entire document has finished loading

I have not been able to find the solution in the following circumstances: In an HTML document, I successfully load multiple other HTML files. However, within one of these included HTML files, specifically "navmenu.html," I want to execute a script with a ...

Navigating to a separate .html page in Vue.js: A step-by-step guide

Hey there! I'm having an issue with Vue.js where I am trying to navigate to a different .html page that is located in my public folder when clicking a button on the page. <button @click="window.location='public/test.html'">See Results ...

Using jQuery to animate the width of a container after it is fixed to the top of the page based on the position of the

Wow, I've been tinkering away for hours and can't seem to crack this code puzzle. Let's dive into some CSS: #wrapper { margin: 0 auto; padding: 0 20px; width: 960px; } #wrapper .block { width: 920px; height: 690px; } ...

Initiate a page break at the beginning of the table in case there are not enough lines on the first page

Does anyone know how to ensure that a table starts on a new page when printing, rather than continuing at the bottom of the previous page? I have tables with repeating headers and many rows, and sometimes a new table will start at the very end of a page. ...

How can we seamlessly transition from adding a class to setting scrollTop on a particular div?

I have successfully implemented a jQuery script to change the background color of my navbar on scroll. Here's the code I used: jQuery(document).ready(function($) { $(window).scroll(function() { var scrollPos = $(window).scrollTop(), nav ...

Correcting CSS to ensure alignment of container-fluid container

Having trouble aligning the login box to the center of the page. Any suggestions on how to achieve this? https://i.sstatic.net/3AFoL.png The following code attempts to achieve center alignment using Bootstrap 4.x base_nologin.html <!DOCTYPE html> ...

Retrieve the dimensions of the image in degrees from the component

I need to retrieve the dimensions of an image I uploaded along with its base64 code. Below is the code snippet I am using: Image img = new Image(); img.src = Selectedimage.src; Img.onload = function { this.width = img.width; this.height = img.height; } T ...