Welcome to Bucaro TecHelp!

Bucaro TecHelp
Maintain Your Computer and Use it More Effectively
to Design a Web Site and Make Money on the Web

About Bucaro TecHelp About BTH User Agreement User Agreement Privacy Policy Privacy Site Map Site Map Contact Bucaro TecHelp Contact RSS News Feeds News Feeds

Victims of Sandy Hook

Stop the Slaughter of Innocents. Congress is bought and paid for by gun lunatics and gun promotion groups. If you want to live in a safe America, help buy Congress back for America. Send a donation to Mayors Against Illegal Guns, 909 Third Avenue, 15th Floor New York, NY 10022

CSS nth-child Selector

In order to apply a style to an html element on a webpage, you must first select the element. There are many ways to select an element on a webpage, ranging from the CSS3 * selector, which selects every element in the document, to the # selector, which selects only an element with a specific id. In this article, I describe the CSS3 nth-child selector. The nth-child selector is used to reference elements that are a child of another element.

The example below shows code for a div element that has five p child elements. The nth-child selector is used to set the font for the third child element to bold.

<!DOCTYPE html>
<html lang="en">
<head>

<style>
div p:nth-child(3)
{
font-weight:bold;
}
</style>

</head>
<body>

<div>
<p>Paragraph one</p>
<p>Paragraph two</p>
<p>Paragraph three</p>
<p>Paragraph four</p>
<p>Paragraph five</p>
</div>

</body>
</html>
 Show Example

The example below shows how to set a style for all the child elements, i.e. the font-family is set for the parent element and is inherited by all the child elements. Then the nth-child selector is used to set the font for the third child element to bold.

<!DOCTYPE html>
<html lang="en">
<head>

<style>
#myDiv
{
font-family:verdana;
}
p:nth-child(3)
{
font-weight:bold;
}
</style>


</head>
<body>

<div id="myDiv">
<p>Paragraph one</p>
<p>Paragraph two</p>
<p>Paragraph three</p>
<p>Paragraph four</p>
<p>Paragraph five</p>
</div>

</body>
</html>
 Show Example

The example below shows how to use the first-child value to set the font for the first child element of the div to bold.

<!DOCTYPE html>
<html lang="en">
<head>

<style>
#myDiv
{
font-family:verdana;
}
p:first-child
{
font-weight:bold;
}
</style>

</head>
<body>

<div id="myDiv">
<p>Paragraph one</p>
<p>Paragraph two</p>
<p>Paragraph three</p>
<p>Paragraph four</p>
<p>Paragraph five</p>
</div>

</body>
</html>
 Show Example

RSS Feed RSS Feed


Follow Stephen Bucaro Follow @Stephen Bucaro


Web Design Sections

Fire HD
[Site User Agreement] [Privacy Policy] [Site map] [Search This Site] [Contact Form]
Copyright©2001-2016 Bucaro TecHelp 13771 N Fountain Hills Blvd Suite 114-248 Fountain Hills, AZ 85268