These are used to select the HTML elements for styling.
HTML
<div>
<h2>Hello There!</h2>
<p>How do you do?</p>
</div>
CSS
p {
color: orange;
}
This will produce the following result −
HTML
<div>
<h2 id="greet">Hello There!</h2>
<p>How do you do?</p>
</div>
CSS
#greet {
color: red;
}
This will produce the following result −
HTML
<div>
<h2>Hello There!</h2>
<p class="text">How do you do?</p>
<h2 class="text">Hello World!</h2>
<p>Give the details</p>
</div>
CSS
.text {
color: lightgreen;
font-size: 18px;
}
This will produce the following result −
CSS
* {
padding: 0px;
margin: 0px;
box-sizing: border-box;
}
A CSS pseudo-class is a keyword added to a selector that specifies a special state of the selected element(s). For example, :hover can be used to change a box color when the user’s pointer hovers over it.
example ::hover
:focus
and :first-child
//css
<style>
.hover-example {
width: 100px;
height: 100px;
background-color: limegreen;
color: white;
}
.hover-example:hover {
background-color: crimson;
width: 150px;
height: 150px;
}
input:focus {
background-color: yellow;
}
</style>
//html
<div class="hover-example">Hover your mouse over me</div>
<form>
First name: <input type="text" name="firstname">
</form>
Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. Specificity is based on the matching rules which are composed of different sorts of CSS selectors.
pseudo-elements in CSS allows you to insert content onto a page without it needing to be in the HTML.While the end result is not actually in the DOM, it appears on the page as if it is.
example: ::before
::after
and ::first-letter
//CSS
<style>
div::before {
content: "before";
}
div::after {
content: "after";
}
p::first-letter {
font-weight: bold;
}
</style>
//HTML
<div>
before
<!-- rest of stuff inside the div -->
after
</div>
<p>
Lorem
</p>