For style purposes, the HTML element(s) are chosen using a CSS selector. HTML elements are chosen using CSS selectors based on their id, class, type, attribute, etc.

The component of a CSS ruleset known as a selector really chooses the content you wish to style. Below is a list of various CSS selectors kinds.

Universal Selector

Similar to a wildcard character, the universal selector selects every element on a page. The styles offered in the example will be applied to every element on the page.

* {
  color: "green";
  font-size: 20px;
  line-height: 25px;
}

Element Type Selector

This selector matches one or more identical HTML elements. The provided styles in the example will be applied to each and every ul element on the page.

ul {
  line-style: none;
  border: solid 1px #ccc;
}

ID Selector

Any HTML element that has an ID property with the same value as the selector is matched by this selector. In the example presented, all elements on the page that have ID as a container will have the provided styles applied to them.

#container {
  width: 960px;
  margin: 0 auto;
}

<div id="container"></div>

Class Selector

All elements on the page whose class property is set to the same value as the class are also matched by the class selector. In the example supplied, all elements on the page with ID box will have the provided styles applied to them.

.box {
  padding: 10px;
  margin: 10px;
  width: 240px;
}

<div class="box"></div>

Descendant Combinator

You can combine two or more selectors using the descendant selector, or more precisely, the descendant combinator, to make your selection process more precise.

#container .box {
	float: left;
	padding-bottom: 15px;
} 

<div id="container">
	<div class="box"></div>
	
	<div class="box-2"></div>
</div>

<div class=?box?></div>

All elements with a class of box that is inside an element with an ID of the container will be affected by this declaration block. It's important to remember that the .box element doesn't have to have a direct child in order for the styles to be applied; there might be another element wrapping the .box element.

Child Combinator

Similar to a selector that employs a descendant combinator, a selector that utilizes the child combinator only targets immediate child items.

#container> .box {
	float: left;
	padding-bottom: 15px;
}

<div id="container">
	<div class="box"></div>
	
	<div>
		<div class="box"></div>
	</div>
</div>

All elements that have the class of box and are direct children of the #container element will be matched by the selection. That implies that there cannot be another element wrapping, unlike the descendant combinator. It must be a direct child element of box.

General Sibling Combinator

A selection that compares components based on their sibling relationships using a general sibling combinator. In the HTML, the chosen elements are next to one another.

h2 ~ p {
	margin-bottom: 20px;
}

<h2>Title</h2>
<p>Paragraph example.</p>
<p>Paragraph example.</p>
<p>Paragraph example.</p>
<div class=?box?>
	<p>Paragraph example.</p>
</div>

All paragraph elements <p>) in this example will be styled according to the given rules, but only if they are siblings of <h2> elements. The styles would still be in effect if there were other items between the <h2> and <p>

Adjacent Sibling Combinator

The plus sign (+) designates a selector that employs the adjacent sibling combinator, which is nearly identical to the general sibling selector. The targeted element must be an immediate sibling rather than a general sibling, which is the distinction.

p + p {
	text-indent: 1.Sem;
	margin-bottom: 0;
}

<h2>Title</h2>
<p>Paragraph example.</p>
<p>Paragraph example.</p>
<p>Paragraph example.</p>

<div class=?box?>
	<p>Paragraph example.</p>
	<p>Paragraph example.</p>
</div>

The paragraph elements in the aforementioned example that immediately follow other paragraph elements will be the only ones to receive the given styles. This implies that these styles would not be applied to the page's initial paragraph element. Additionally, the styles wouldn't be applied to the second paragraph in a pair if another element appeared in between them.

Attribute Selector

The attribute selector is expressed using square brackets and targets elements depending on the existence and/or value of HTML attributes.

input [type=?text?] {
	background-color: #444;
	width: 200px;
}

<input type="text">

Recommended Posts

View All