CSS Selector element1~element2








The element1~element2 selector selects element2 that are preceded by element1.

Both elements must have the same parent. element2 does not have to be immediately preceded by element1.

Summary

CSS Version
3

CSS Syntax

element1~element2 { 
   style properties 
}

Browser compatibility

element1~element2 Yes 7.0 Yes Yes Yes




Example

An example showing how to use element1~element2 CSS selector.

<!DOCTYPE html>
<html>
<head>
<style> 
p~h2{<!--  ww w  . java 2  s . c  o m-->
   background:red;
}
</style>
</head>
<body>
    <div>A div element.</div>
    <p>The first paragraph.</p>
    <h2>Another list</h2>
    <p>The second paragraph.</p>
</body>
</html>

Click to view the demo