Recents in Beach

Combinator Selectors In CSS

Combinator Selectors In CSS

A Combinator is something that explains the relationship between the selectors. A CSS selector can contain more than one simple selectors, we can include a combinator.

Selectors in CSS Image




There are four different combinators in CSS:

descendant Selectors (Space)

child Selectors (>)

adjacent Sibling Selector (+)

general Sibling Selector (~)

Descendant Selectors in CSS with Example:

A descendant Selector Matches any element that is a descendant of another .

descendant Selectors are great for styling elements that exists inside of  other elements .

Example 1 : Descendant Selectors ( space)

<!DOCTYPE html>

<html>

<head>

<style>

div p {

  background-color: red;

  font-style: italic;

}

</style>

</head>

<body>

 

<div>

  <p>Paragraph 1 in the div.</p>

  <p>Paragraph 2 in the div.</p>

  <section><p>Paragraph 3 in the div.</p></section>

</div>

 

<p>This para is outside of div.</p>

<p>This wont be in red background.</p>

 

</body>

</html>

 

Output:

descendant selectors in css

Child Selectors (>) in CSS with Example:

The child selector selects all elements that are the children of a specified element.

child selector only selects immediate descendant of an ancestor.

This means that elements which are not directly a child of the specified parent element are not selected

The child selector uses an additional symbol- an angle bracket( >) to show the relationship between two elements

Example 2 : Child Selectors ( >)

<!DOCTYPE html>

<html>

<head>

<style>

div > p {

  background-color: red;

  font-style: italic;

}

</style>

</head>

<body>

 

<div>

  <p>Paragraph 1 in the div.</p>

  <p>Paragraph 2 in the div.</p>

  <section><p>Paragraph 3 in the div.</p></section>

  <p>Paragraph 4 in the div.</p>

</div>

 

<p>This para is outside of div.</p>

<p>This wont be in red background.</p>

 

</body>

</html>


Output:

Child selector in css


Adjacent Sibling Selectors (+) in CSS with Example:

The adjacent sibling selector selects all elements that are the adjacent (Immediately) siblings of a specified element.

Sometimes you need to select a tag based not on its parent tag but on its sibling tags that shares a common parent.

The selectors uses an additional symbol-plus (+) to show the relationship between the two elements.

Example 3 : Adjacent Sibling Selectors ( +)

<!DOCTYPE html>

<html>

<head>

<style>

div + p {

  background-color: red;

  font-style: italic;

}

</style>

</head>

<body>

 

<div>

  <p>Paragraph 1 in the div.</p>

  <p>Paragraph 2 in the div.</p>

  <section><p>Paragraph 3 in the div.</p></section>

  <p>Paragraph 4 in the div.</p>

</div>

 

<p>This para is outside of div.</p>

<p>This wont be in red background.</p>

 

</body>

</html>


Output:

adjacent sibling selectors in css

General Sibling Selectors (~) in CSS with Example:

The general sibling selector selects all elements that are siblings of a specified element.

General sibling selectors selects element only if both are children of the same parent.

The general sibling selector uses an additional symbol tilde (~) to indicate the relationship between the two elements .

Example 4 : General Sibling Selectors ( +)

<!DOCTYPE html>

<html>

<head>

<style>

div ~ p {

  background-color: red;

  font-style: italic;

}

</style>

</head>

<body>

 

<div>

  <p>Paragraph 1 in the div.</p>

  <p>Paragraph 2 in the div.</p>

  <section><p>Paragraph 3 in the div.</p></section>

  <p>Paragraph 4 in the div.</p>

</div>

 

<p>This para is outside of div.</p>

<H3>Welcome launchpadlwd blog</H3>

<p>This will be in red background.</p>

 

</body>

</html>


Output:

General sibling Selectors in css

Forms In Html Please Visit This Link:


Tables In Html Please Visit This Link:


Meta Tags In Html Please Visit This Link:




Post a Comment

0 Comments