The Wayback Machine - https://web.archive.org/web/20250221142415/https://www.geeksforgeeks.org/css-element-selector/
Open In App

CSS element Selector

Last Updated : 29 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The element selector in CSS is used to select HTML elements that are required to be styled. In a selector declaration, there is the name of the HTML element and the CSS properties which are to be applied to that element is written inside the brackets {}. 

Syntax:

element {
\\ CSS property
}

Example 1: This example shows the element selector in CSS.

html
<!DOCTYPE html>
<html>
<head>
    <title>element selector</title>

    <style>
        /* h1 element selected here */
        h1 {
            color: green;
            text-align: center;
        }

        /* h2 element selected here */
        h2 {
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>GeeksforGeeks</h1>
    <h2>element Selector</h2>
</body>
</html>

Output:

  

Example 2: This example shows the element selector in CSS.

html
<!DOCTYPE html>
<html>
<head>
    <title>element selector</title>
    <style>
        h1 {
            text-align: center;
        }

        h2 {
            text-align: center;
            color: green;
            font: bolder cursive;
        }

        form {
            text-align: center;
        }
    </style>
</head>

<body>
    <h1>FORM</h1>

    <h2>Please fill in details:</h2>

    <form action="#">
        Name: <input type="text" value="name"><br>
        Age: <input type="text" value="age"><br>
        City: <input type="text" value="City"><br>
    </form>
</body>
</html>

Output:

  

Supported Browsers: The browser supported by element selectors are listed below:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Safari
  • Opera

CSS element Selector – FAQs

What is an element selector in CSS?

An element selector targets all elements of a specific type on a web page, such as all paragraphs (<p>) or all headings (<h1>).

How do I use an element selector to style all paragraphs?

Use the element name as the selector, like p { font-size: 16px; } to style all paragraph elements.

Can element selectors be combined with class or id selectors?

Yes, element selectors can be combined with class or id selectors for more specific targeting, such as p.highlight or div#container.

Is the element selector case-sensitive?

No, element selectors are not case-sensitive in HTML, but they are case-sensitive in XML and XHTML documents.

What is the specificity of an element selector compared to other selectors?

Element selectors have lower specificity than class, attribute, or id selectors and will be overridden by them if there is a conflict.


Next Article

Similar Reads

three90RightbarBannerImg