Recents in Beach

Selectors In CSS with examples

 Selectors In CSS with examples

In CSS, selectors are used to target the HTML elements we want to style .There are a wide variety of CSS selectors available. 

Selectors in css

CSS Selectors Can be divided into Five categories:

1. Simple Selectors (Element selector, Id selector, Class selector, Universal selector)

2. Combinator Selectors(Descendant selector, child selector, sibling selector, General selector)

3. Attribute Selector(allow you to select elements by referring their attributes)

4. Pseudo-classselectors (select elements based on a certain state)

5. Pseudo-elements selectors (select and style a part of an element)

Element Selector in CSS with Example:

An element selector is the name of an html element

also called as type or tag selectors

Example 1 : Element Selectors

<!DOCTYPE html>

<head>

  <title>Selector</title>

  <style>

    p{color: red;}

  </style>

</head>

<body>

 <h2>This is heading so it will color will not change</h2>

  <p>

    Welcome to launchpadlwd blog <br>

    note this paragraph will become red

</p>

</body>

</html>


Output:

Selectors in  css

Id Selector in CSS with Example:

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 unique element. 

It is written with the hash character (#), followed by the id name

Example 2 : Id Selectors

<!DOCTYPE html> 

<html>

<head>

<style>

#para1{color: blue}

#para2{color: red;}  

</style> 

</head>

<body>

<p id="para1">Welcome To</p>

<p id="para2">launchpadlwd blog.</p>

</body>

</html>

Output:

Class Selector in CSS with Example:


CSS Class Selector The class selector selects HTML elements with a specific class attribute.

It is used with a period (dot) character '.'  (full stop symbol) followed by the class name.

The Class selector is used when you want to change a group of elements within your HTML page

The Class Name should not start with number.

Example 3 : Class Selectors

<!DOCTYPE html> 

<html>

<head>

<style>

.head{color: blue;}

.para1{color: red;}  

</style> 

</head>

<body>

<h1 class="head1">Welcome To</h1>

<p class="para1">launchpadlwd blog.</p>

</body>

</html>



Output:


Universal Selector in CSS with Example:

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

The Universal Selector Is represented by an asterisk(*)

Syntax : *{Color:blue};

Example 3 : Class Selectors

<!DOCTYPE html> 

<html>

<head>

<style>

 *{

   font-size: 20px;

   color: red;

   text-align: center;

 }

</style> 

</head>

<body>

<h1>Welcome To</h1>

<p>launchpadlwd blog. </p>

<p>Universal selector will Apply to all elements</p>

</body>

</html>


Output:


Group Selector in CSS with Example:

The grouping selector is used to select all the elements with the same style definitions. 

 It is used to minimize the code.

 Commas are used to separate each selector in grouping.

Example 4 : Group Selectors

<!DOCTYPE html> 

<html>

<head>

<style>

 h1,p,B{

   font-size: 20px;

   color: orange;

 }

</style> 

</head>

<body>

<h1>Welcome To</h1>

<p>launchpadlwd blog. </p>

<B>Group selector will Apply to selected elements</B>

</body>

</html>


Output:


Combinator Selectors In CSS 

Attribute Selectors In CSS 

Pseudo-Class Selectors In CSS 

Pseudo-Element Selectors In CSS 




Post a Comment

0 Comments