Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 5743

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 27, 20242024-11-27T12:05:07+00:00 2024-11-27T12:05:07+00:00

How to make CSS first-child work

  • 60k

With the onset of CSS3 a lot of new selectors were introduced. Many of these new selectors have been of a lot of help to us. The first-child selector is one such selector. Using the first-child selector, you can target the element which is the first child of its parent element. This means if you want to target the first <li> of an unordered list or <ul> you can use the :first-child selector to target the first “li” element. The code for it would look similar to the following :

ul li:first-child{   color:red; } 
Enter fullscreen mode Exit fullscreen mode

The above code would target the first <li> of the <ul> and change its color to red. This is a really simple and useful property. However, remember that :first-child would work only when the element you are targeting is the first child of the parent element. This means that if there is any other sibling element before the element you are targeting, then the first-child property would NOT work as expected. Let’s consider the following example :

<div>  <h1>Some Heading</h1>  <p>My first paragraph</p>  <p>My second paragraph</p> </div>  <style> /* This would NOT work as expected ! */  div p:first-child{  color:red; } </style> 
Enter fullscreen mode Exit fullscreen mode

In the above example, you can notice that the first <p> within the <div> is being targeted. However, this would NOT work because the <p> is technically NOT the first child of the <div>. The first child of the <div> is the <h1> tag.

So, how to make the :first-child work in CSS ?

If there are other sibling elements before the “first child element” that you are targeting, you should use the first-of-type selector. The first-of-type selector would target the elements which are of a “certain type or tag” and are the first ones of its type within its parent element. To understand this clearly please take a look at the following example :

<div>  <h1>Some Heading</h1>  <p>My first paragraph</p>  <p>My second paragraph</p> </div>  <style> /* This would work ! */  div p:first-of-type{  color:red; } </style> 
Enter fullscreen mode Exit fullscreen mode

In the example above, the first paragraph within the <div> would have the text color as red. The key here is to understand that the <p> or paragraph is NOT technically the first child and that when you want to target the paragraph or <p> you should target the <p> which is the first child of its parent <div> and of the ‘certain’ type or of the ”

type “.

Hope you understood the concept and liked the article. Please like us on Facebook and follow us on Twitter.

The post How to make CSS first-child work first appeared on MoreOnFew.

csshtmltutorialwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.