In CSS, what is the purpose of a selector, and how does the id selector differ from the class selector?

                      What is CSS?

  1. CSS stands for Cascading Style Sheets.
  2. CSS describes how HTML elements are to be displayed on screen, paper, or in other media.
  3. CSS saves a lot of work. It can control the layout of multiple web pages all at once.
  4. External stylesheets are stored in CSS files.



  • CSS is used to define styles for your web pages, including the design, layout and variations in display for different devices and screen sizes.


Example
<html>

 <head>
    <style> body { background-color: lightblue; } </style>
 </head> 

<body>
    <h1>Hello, World!</h1> 
    <p>This page has a light blue background color.</p> 
</body>

 </html>




  • A CSS rule consists of a selector and a declaration block.

Structure:

selector {
    property: value;
}


Example:

p {
    text-align: center;
}

1.Selector: Points to the HTML element you want to style. For example, p points to paragraph elements.

2.Declaration Block: Contains one or more declarations surrounded by curly braces {}.

3.Declaration: Each declaration includes:
   (i)A CSS property name (like color, font-size, etc.).
(ii)A value (like blue, 16px, etc.) for that property.

4.Multiple Declarations: Declarations are separated by semicolons inside the block.


CSS selectors are used to "find" (or select) the HTML elements you want to style. We can divide CSS selectors into five categories: 
  1. Simple selectors (select elements based on name, id, class)
  2. Combinator selectors (select elements based on a specific relationship between them)
  3. Pseudo-class selectors (select elements based on a certain state) 
  4. Pseudo-elements selectors (select and style a part of an element) 
  5. Attribute selectors (select elements based on an attribute or attribute value)


 


  • The id selector uses the id attribute of an HTML element to select a specific element.
  • The id of an element is unique within a page, so the id selector is used to select one unique element!
  • To select an element with a specific id, write a hash (#) character, followed by the id of the element.
Example :
<p id="para1">This text will be centered.</p>

 #para1 {
text-align: center;
}


  • The class selector selects HTML elements with a specific class attribute.
  • To select elements with a specific class, write a period (.) character, followed by the class name.
Example : 

<p class="center">This text will be centered.</p>


.center {  text-align: center;
}
  • You can also specify that only specific HTML elements should be affected by a class.
Example : 
<p class="center">This text will be centered and red.</p>

p.center {
 text-align: center;  color: red;
}



  • The universal selector (*) selects all HTML elements on the page.

 Example : 

* {
     text-align: center;
     color: blue;
   }




  • The grouping selector selects all the HTML elements with the same style definitions.
































  • The descendant selector matches all elements that are descendants of a specified element.
  • The descendant selector in CSS allows you to target and style elements that are nested (or "descended") within other elements.






<div> <p>This paragraph is inside a div and has a yellow background.</p>
 <p>Another paragraph inside the same div, also with a yellow background.</p> </div>


Post a Comment

0 Comments