WEB TUTORIALS
PRACTICE EXAMPLES
HTML REFERENCES
CSS REFERENCES
PHP REFERENCES

What is the CSS Selector for First Element with Specific Class

Topic: HTML / CSSPrev|Next

Answer: Use the Child and General Sibling Selectors

You can simply use the child selector (>) in combination with the general sibling selector () to select the first element in a group of elements with specific class.

Let's try out the following example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CSS Selector for First Element with Class</title>
<style>
    .wrapper > .red {
        background: yellow;
    }
    .wrapper > .red ~ .red {
        background: none;
    }
    .red {
        color: red;
    }
</style>
</head>
<body>
    <div class="wrapper">
        <div>Random</div>
        <p class="red">First</p>
        <p class="red">Second</p>
        <p class="red">Third</p>
    </div>
</body>
</html>

Related FAQ

Here are some more FAQ related to this topic:

Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties