CSS Problem: The first-child, last-child, and only-child pseudo-classes are not functioning as expected when applied to lists

I am facing an issue with the following pseudo classes:

  • first-child: The first a element within each div should have a red background.
  • last-child: The last a element within each div should have a blue background.
  • only-child: Only the a element of the last div should have a grey background with 60% border radius and white text color.

The a element in the middle of each div ("Two") should have a green background.

However, all a elements appear with grey background, 60% border radius, and white text (essentially displaying only-child properties).

What modifications should I make to my code?

li {
  list-style: none;
}

a {
  padding: 10px;
  display: inline-block;
  background-color: green;
  color: yellow;
  text-decoration: none;
}

a:first-child {
  background-color: red;
}

a:last-child {
  background-color: blue;
}

a:only-child {
  border-radius: 60%;
  background-color: grey;
  color: white;
}
<div>
  <ul>
    <li><a href="#">One</a>
      <!-- Background should be RED and Yellow text -->
    </li>
    <li><a href="#">Two</a>
      <!-- Background should be GREEN and Yellow text -->
    </li>
    <li><a href="#">Three</a>
      <!-- Background should be BLUE and Yellow text -->
    </li>
  </ul>
</div>
<div>
  <ul>
    <li><a href="#">One</a>
      <!-- Background should be RED and Yellow text -->
    </li>
    <li><a href="#">Two</a>
      <!-- Background should be GREEN and Yellow text -->
    </li>
    <li><a href="#">Three</a>
      <!-- Background should be BLUE and Yellow text -->
    </li>
  </ul>
</div>
<div>
  <ul>
    <li><a href="#">One</a>
      <!-- Background should be RED and Yellow text -->
    </li>
    <li><a href="#">Two</a>
      <!-- Background should be GREEN and Yellow text -->
    </li>
    <li><a href="#">Three</a>
      <!-- Background should be BLUE and Yellow text -->
    </li>
  </ul>
</div>
<div>
  <ul>
    <li><a href="#">Last one</a>
      <!-- Background should be GREY, border radius at 60% and White text -->
    </li>
  </ul>
</div>

Answer №1

  • You have misinterpreted the information, the 'a' here is a child of li. -Meaning that any styles you apply should be applied to the child of ul, which is li.
  • ul li:fisrt-child. -ul li:last-child. -ul li:only-child.

li {
  list-style: none;
}

a {
  padding: 10px;
  display: inline-block;
  background-color: green;
  color: yellow;
  text-decoration: none;
}

ul li:first-child a {
  background-color: red;
}

ul li:last-child a {
  background-color: blue;
}

ul li:only-child a {
  border-radius: 60%;
  background-color: grey;
  color: white;
}
<body>
  <div>
    <ul>
      <li><a href="#">One</a>
        <!-- Background should be RED and Yellow text -->
      </li>
      <li><a href="#">Two</a>
        <!-- Background should be GREEN and Yellow text -->
      </li>
      <li><a href="#">Three</a>
        <!-- Background should be BLUE and Yellow text -->
      </li>
    </ul>
  </div>
  <div>
    <ul>
      <li><a href="#">One</a>
        <!-- Background should be RED and Yellow text -->
      </li>
      <li><a href="#">Two</a>
        <!-- Background should be GREEN and Yellow text -->
      </li>
      <li><a href="#">Three</a>
        <!-- Background should be BLUE and Yellow text -->
      </li>
    </ul>
  </div>
  <div>
    <ul>
      <li><a href="#">One</a>
        <!-- Background should be RED and Yellow text -->
      </li>
      <li><a href="#">Two</a>
        <!-- Background should be GREEN and Yellow text -->
      </li>
      <li><a href="#">Three</a>
        <!-- Background should be BLUE and Yellow text -->
      </li>
    </ul>
  </div>
  <div>
    <ul>
      <li><a href="#">Last one</a>
        <!-- Background should be GREY, border radius at 60% and White text -->
      </li>
    </ul>
  </div>
</body>

Answer №2

It is recommended to apply the CSS pseudo-classes first-child, last-child, and only-child to the li elements rather than the a elements.

li {
  list-style: none;
}

a {
  padding: 10px;
  display: inline-block;
  background-color: green;
  color: yellow;
  text-decoration: none;
}

ul li:first-child a {
  background-color: red;
}

ul li:last-child a {
  background-color: blue;
}

ul li:only-child a {
  border-radius: 60%;
  background-color: grey;
  color: white;
}
<body>
  <div>
    <ul>
      <li><a href="#">One</a>
        <!-- Background should be RED and Yellow text -->
      </li>
      <li><a href="#">Two</a>
        <!-- Background should be GREEN and Yellow text -->
      </li>
      <li><a href="#">Three</a>
        <!-- Background should be BLUE and Yellow text -->
      </li>
    </ul>
  </div>
  <div>
    <ul>
      <li><a href="#">One</a>
        <!-- Background should be RED and Yellow text -->
      </li>
      <li><a href="#">Two</a>
        <!-- Background should be GREEN and Yellow text -->
      </li>
      <li><a href="#">Three</a>
        <!-- Background should be BLUE and Yellow text -->
      </li>
    </ul>
  </div>
  <div>
    <ul>
      <li><a href="#">One</a>
        <!-- Background should be RED and Yellow text -->
      </li>
      <li><a href="#">Two</a>
        <!-- Background should be GREEN and Yellow text -->
      </li>
      <li><a href="#">Three</a>
        <!-- Background should be BLUE and Yellow text -->
      </li>
    </ul>
  </div>
  <div>
    <ul>
      <li><a href="#">Last one</a>
        <!-- Background should be GREY, border radius at 60% and White text -->
      </li>
    </ul>
  </div>
</body>

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

Determine the body's height by adding the heights of the header and footer

In my design, there is a header with a height of 8rem; .header { top: 0; left: 0; height: 8rem; } Following that, there is a footer with a height of 5rem; footer { width:100%; height: 5rem; } I am attempting to calculate the heig ...

Application of id missing on all buttons in Bootstrap tour template

I'm having an issue with applying a new id to the next button in a Bootstrap tour template. I added the id to the button, but it only seems to work for the first stage and not the subsequent ones. Can anyone provide insight into why this might be happ ...

Bootstrap causing issues with correct display of div element

I'm having trouble getting my divs to display on the same row. It seems like when I have 2 rows, the button is positioned correctly. However, with only one row, the button ends up on the second row. Here is the current layout: https://i.sstatic.net/L ...

Maximizing the potential of image srcset in HTML5

After configuring the img srcset as shown below: <img srcset="http://via.placeholder.com/320x150 320w, http://via.placeholder.com/480x150 480w, http://via.placeholder.com/800x150 800w" src="http://via.placeholder.com/8 ...

Elastic canvas to always match the full size of the container or screen

I am facing an issue with my responsive canvas that should resize to fit the screen window within a div. The canvas is intended to be 1100x1100 but needs to scale based on the screen size while maintaining a square aspect ratio. The problem arises when th ...

Ways to ensure a function is only triggered once using onmouseover

I'm fairly new to JavaScript and I've been attempting to create a function that only runs once. Here's the logo I've been trying to animate: <img src="img/logot2.png" id="logoutjs" onmouseover="move()" ...

Incorporate a CSS framework into the Angular vendor bundle

My current situation : The website is built with Angular 4 Started using Angular Starter Kit Utilizing Semantic UI as the CSS framework The challenge I'm facing : Integration of Semantic UI into webpack is not having any impact. Steps I've ...

Is it possible to control my CSS using ModX Resources?

I have recently been experimenting with the ModX Revolution manager and had a question about converting my CSS into ModX Resources instead of files. I am interested in utilizing Templates and their variables as well. Do you know if this is possible? Also, ...

tips on displaying textbox content on a php webpage

I have a piece of code where, when text is entered into a textbox and the add attribute button is clicked, the entered value is displayed on the page twice. One appears in the first row of a table, and the other appears in the first row of a second table. ...

Hiding a Hide/Show DIV when hovering over the Google+ Follow Button

I'm currently working on a project that involves a hover drop-down panel with Google+, Facebook, Youtube, and Twitter social widgets. These widgets are listed for visitors to easily click on each one without the panel closing. Initially, I created a ...

Finding the dynamic width of a div using JavaScript

I have two divs named demo1 and demo2. I want the width of demo2 to automatically adjust when I drag demo1 left to right or right to left. <div class="wrapper"> <div class="demo"></div> <div class="demo2"&g ...

How can I update the color scheme in styled components based on the active state of the component?

I'm currently working on a react accordion component that requires changing styles based on the active and inactive states. I have successfully set the state and added two props for color and active color. The goal is to change colors when the user cl ...

Utilizing CSS to Display a Variety of Colors within a Text String

Is it possible to style different words in a sentence with various colors using only CSS, without using the span element? For instance, how can I make the word "Red" appear in red, "Blue" in blue, and "Green" in green within an h1 tag? ...

How can I effectively display the city weather information that I have chosen from the drop-down menu?

Is there a way to display city weather information that I have selected from a drop-down menu using JSON? I am able to retrieve the city using jQuery, but I am not sure how to load the weather data for that city. Any guidance on this would be appreciated ...

Ensuring that the empty bubble remains undisturbed, here's a guide on effectively implementing the if

I need assistance with adding an "if condition" to my text field. The condition should prevent the empty bubble from appearing when the send button is clicked. function verifyInput(){ var currentText = document.getElementById("demo").innerHTML; var x ...

What is the best way to access the inner html of every cell within a table row that I have selected?

I want to implement a feature on my website where users can click a "save" button on a specific row in a table and save the entire row's innerHtml onto another page as their favorite hiking trails. I've been trying to achieve this by adding clic ...

Tips for creating a navigation bar that bypasses body padding in HTML and CSS

I have set padding in my 'body' tag, but I want my top navigation bar to cover the entire page without being affected by it. I attempted to fix this by setting the width to 100% and using negative padding and margin values, but it did not work a ...

When attempting to click the submit button, the action of submitting a form using jQuery/AJAX is not functioning as expected

When attempting to submit a form using jquery/AJAX, my function does not get called when clicking on the submit button. The structure of my website is as follows: CarMenu.php <html lang="en"> <html> <head> <meta charset="ISO-8859- ...

Only capitalized keyboard input will be registered by the document on keydown event

I'm currently working on developing a shared canvas wall that looks the same to everyone viewing the webpage and can be edited in real-time by all users. However, I'm facing some challenges when it comes to accurately capturing keyboard input to ...

I am attempting to properly arrange my navigation links and logo in alignment

I need help aligning the logo in the exact center while keeping the navigation links as they are. Here is how it currently looks: https://i.stack.imgur.com/qOgVJ.jpg My CSS: .logo { display: inline-block; text-align: center; } na ...