Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

CSS Selector


CSS 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 Selector
  2. CSS Id Selector
  3. CSS Class Selector
  4. CSS Universal Selector
  5. CSS Group Selector

1) CSS Element Selector

The element selector selects the HTML element by name.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. p{
  6.     text-align: center;
  7.     color: blue;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <p>This style will be applied on every paragraph.</p>
  13. <p id=“para1”>Me too!</p>
  14. <p>And me!</p>
  15. </body>
  16. </html>

Output:

This style will be applied on every paragraph.

Me too!

And me!


2) CSS Id Selector

The 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”.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. #para1 {
  6.     text-align: center;
  7.     color: blue;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <p id=“para1”>Hello Kreationnext.com</p>
  13. <p>This paragraph will not be affected.</p>
  14. </body>
  15. </html>

Output:

Hello Kreationnext.com

This paragraph will not be affected.


3) CSS Class Selector

The 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”.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. .center {
  6.     text-align: center;
  7.     color: blue;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h1 class=“center”>This heading is blue and center-aligned.</h1>
  13. <p class=“center”>This paragraph is blue and center-aligned.</p>
  14. </body>
  15. </html>

Output:

This heading is blue and center-aligned.

This paragraph is blue and center-aligned.


CSS Class Selector for specific element

If 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.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. p.center {
  6.     text-align: center;
  7.     color: blue;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h1 class=“center”>This heading is not affected</h1>
  13. <p class=“center”>This paragraph is blue and center-aligned.</p>
  14. </body>
  15. </html>

Output:

This heading is not affected

This paragraph is blue and center-aligned.


4) CSS Universal Selector

The universal selector is used as a wildcard character. It selects all the elements on the pages.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. * {
  6.    color: green;
  7.    font-size: 20px;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h2>This is heading</h2>
  13. <p>This style will be applied on every paragraph.</p>
  14. <p id=“para1”>Me too!</p>
  15. <p>And me!</p>
  16. </body>
  17. </html>

Output:

This is heading

This style will be applied on every paragraph.

Me too!

And me!


5) CSS Group Selector

The 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.

  1. h1 {
  2.     text-align: center;
  3.     color: blue;
  4. }
  5. h2 {
  6.     text-align: center;
  7.     color: blue;
  8. }
  9. p {
  10.     text-align: center;
  11.     color: blue;
  12. }

As you can see, you need to define CSS properties for all the elements. It can be grouped in following ways:

  1. h1,h2,p {
  2.     text-align: center;
  3.     color: blue;
  4. }

Let’s see the full example of CSS group selector.

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <style>
  5. h1, h2, p {
  6.     text-align: center;
  7.     color: blue;
  8. }
  9. </style>
  10. </head>
  11. <body>
  12. <h1>Hello Kreationnext.com</h1>
  13. <h2>Hello Kreationnext.com (In smaller font)</h2>
  14. <p>This is a paragraph.</p>
  15. </body>
  16. </html>

Comments

comments