CSS SelectorCSS selectors are used to select the content you want to style. Selectors are the part of CSS rule set. CSS selectors select HTML elements according to its id, class, type, attribute etc. There are several different types of selectors in CSS.
1) CSS Element SelectorThe element selector selects the HTML element by name. Test it NowOutput: This style will be applied on every paragraph. Me too! And me! 2) CSS Id SelectorThe id selector selects the id attribute of an HTML element to select a specific element. An id is always unique within the page so it is chosen to select a single, unique element. It is written with the hash character (#), followed by the id of the element. Let?s take an example with the id "para1". Test it NowOutput: Hello Javatpoint.com This paragraph will not be affected. 3) CSS Class SelectorThe class selector selects HTML elements with a specific class attribute. It is used with a period character . (full stop symbol) followed by the class name. Note: A class name should not be started with a number.Let's take an example with a class "center". Test it NowOutput: This heading is blue and center-aligned.This paragraph is blue and center-aligned. CSS Class Selector for specific elementIf you want to specify that only one specific HTML element should be affected then you should use the element name with class selector. Let's see an example. Test it NowOutput: This heading is not affectedThis paragraph is blue and center-aligned. 4) CSS Universal SelectorThe universal selector is used as a wildcard character. It selects all the elements on the pages. Test it NowOutput: This is headingThis style will be applied on every paragraph. Me too! And me! 5) CSS Group SelectorThe grouping selector is used to select all the elements with the same style definitions. Grouping selector is used to minimize the code. Commas are used to separate each selector in grouping. Let's see the CSS code without group selector. As you can see, you need to define CSS properties for all the elements. It can be grouped in following ways: Let's see the full example of CSS group selector. Test it NowOutput: Hello Javatpoint.comHello Javatpoint.com (In smaller font)This is a paragraph. Next TopicHow to add CSS |