Why is my :last-child not working?

Tell us what’s happening:
Describe your issue in detail here.
why is my :last-child not working? i am trying to make it so only the last fieldset doesnt have a bottom-border line. using bottom-border: none; and border: none; dont work.

   **Your code so far**
/* file: index.html */
<!DOCTYPE html>
<html>
 <head>
   <title>Registration Form</title>
	  <link rel="stylesheet" type="text/css" href="styles.css" />
 </head>
 <body>
   <h1>Registration Form</h1>
   <p>Please fill out this form with the required information</p>
   <form action='https://register-demo.freecodecamp.org'>
     <fieldset>
       <label>Enter Your First Name: <input type="text" name="first-name" required /></label>
       <label>Enter Your Last Name: <input type="text" name="last-name" required /></label>
       <label>Enter Your Email: <input type="email" name="email" required /></label>
       <label>Create a New Password: <input type="password" name="password" pattern="[a-z0-5]{8,}" required /></label>
     </fieldset>
     <fieldset>
       <label><input type="radio" name="account-type" /> Personal Account</label>
       <label><input type="radio" name="account-type" /> Business Account</label>
       <label>
         <input type="checkbox" name="terms" required /> I accept the <a href="https://www.freecodecamp.org/news/terms-of-service/">terms and conditions</a>
			  </label>
     </fieldset>
     <fieldset>
       <label>Upload a profile picture: <input type="file" name="file" /></label>
       <label>Input your age (years): <input type="number" name="age" min="13" max="120" />
			  </label>
       <label>How did you hear about us?
         <select name="referrer">
           <option value="">(select one)</option>
           <option value="1">freeCodeCamp News</option>
           <option value="2">freeCodeCamp YouTube Channel</option>
           <option value="3">freeCodeCamp Forum</option>
           <option value="4">Other</option>
         </select>
       </label>
       <label>Provide a bio:
         <textarea name="bio" rows="3" cols="30" placeholder="I like coding on the beach..."></textarea>
			  </label>
     </fieldset>
     <input type="submit" value="Submit" />
   </form>
 </body>
</html>
/* file: styles.css */
body {
 width: 100%;
 height: 100vh;
 margin: 0;
 background-color: #1b1b32;
	color: #f5f6f7;
 font-family: Tahoma;
	font-size: 16px;
}

h1, p {
 margin: 1em auto;
 text-align: center;
}

form {
 width: 60vw;
	max-width: 500px;
	min-width: 300px;
	margin: 0 auto;
}

fieldset {
 border: none;
	padding: 2rem 0;
 border-bottom: 3px solid #3b3b4f
}

fieldset:last-child {
 border: none;
}

label {
	display: block;
	margin: 0.5rem 0;
}

   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36

Challenge: Step 41

Link to the challenge:

HI @seonathankk !

I would suggest resetting the lesson.

Your task is to select all but the last fieldset element.

You will need to first read about the not pseudo class

Then you will need to read about the last of type css pseudo class

Hope that helps!

@seonathankk The lesson is expecting you to use a specific pseudo-selector (which has not been introduced yet), but to answer your question of why the border does not go away when using last-child, the reason is the last sibling element of the group of fieldset elements which the selector sees is the input element.

That is why the step ends up wanting you to use last-of-type with the not instead of the last-child. You can test this by commenting out the input element below the last fieldset element while using the CSS you tried to submit.

i will take a look thank you!

thank you for replying! why does it see the last input element as a sibling? wouldn’t the fieldset:last-child just look for the last fieldset element? does a submit button count as its own fieldset?

No, that is not the way that selector works. Let me break it down:

Using the following:

fieldset:last-child {
  border: none;
}

the fieldset part looks at the fieldset element (obviously), but as soon as you add the :last-child, the selector looks at all the siblings in parent of one or more fieldsets and targets the last sibling element.

It (the input element) is a sibling because it has the same parent as the fieldset elements that were targeted. It is a little confusing, but once you understand the concept of siblings, it will make sense.

The last-of-type works similar regarding looking at the same parent but only considers siblings that are of the same type (in this case fieldset elements).

thank you for your time! I now understand :blush:
it was trying to find a fieldset element which also has to be last sibling of the parent (form) but since input was the last sibling it didnt work.

1 Like

Let me clarify one thing. The fieldset:last-child selector ignores the 3rd fieldset because it is not the last sibling and a fieldset element.

The use of last-of-type basically filters out all the other siblings and then looks at the last of the same type.

For example, if I had the following:

HTML

<div id="one">
  <p>a</p>
  <p>b</p>
  <p>c</p>
</div>

<div id="second">
  <p>1</p>
  <p>2</p>
  <p>3</p>
  <div>
    text in div
  </div>
</div>

CSS

p:last-child {
  background: green;
}

the only background that turns green is for the p with c as its text.

The nested div in the second div (with id=“second”) is the last sibling of the second group of p elements, but since it is not a p element, it does not get a green background.

If I use the same HTML but use the following CSS:

p:last-of-type {
  background: green;
}

both the c and the 3 get a green background.