CSS3 :only-child Selector - HTML CSS CSS Selector

HTML CSS examples for CSS Selector:only-child

Description

The :only-child selector selects element that is the only child of its parent.

Example

Select every <p> element that is the only child of its parent:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<style>
p:only-child {<!--from w  w  w. j  av  a2s.com-->
    background: red;
}
</style>
</head>
<body>

<p>This is a paragraph.</p>

<div><p>This is a paragraph.</p></div>

<div><span>This is a span.</span><p>This is a paragraph.</p></div>


</body>
</html>

Related Tutorials