CSS Selector Cheat Sheet - Top Selectors For Front-End Development
CSS Selector Cheat Sheet - Top Selectors For Front-End Development
Christina Kopecky
CSS selectors are used to locate HTML elements you want to style in
your programs. Think of these like patterns or codewords that all
you to specifically search for and modify particular aspects of your
code. Using CSS selectors will speed up your front-end life and make
it far easier to edit code quickly.
Today, we’ll show how to implement all the most useful CSS
selectors, from basic to advanced psuedo-selectors.
The CSS Selector dictates which HTML element apply the properties
to.
Basic Selectors
Tag Selectors
p {
font-size: 14px;
}
.none {
display: none;
}
ID Selectors
#container {
margin: 0 auto;
padding: 0;
}
Combinator Selectors
Combinator selectors join multiple basic selectors with a
combinator to form a more complex selection criteria. A combinator
is a character that will instruct the compiler how to select an HTML
element. There are four different types:
Descendent Selectors
ul a {
text-decoration: none;
color: black;
cursor: pointer;
}
We can also use target a specific child element using the first-
child or last-child selectors.
p:first-child {
color: red;
}
div p:last-child {
color: red;
Read this example as “Select all p tags that come immediately after
div tags”. In this example, we are selecting any p tag that has the
same parent element as the div tag, where the p tag immediately
comes after the div.
You can use the general sibling selector to select all siblings of an
element. In this example, we are selecting any p tag that is a sibling
of a div to change the font-size and color.
<body>
<p></p> <!-- the CSS selector will select this element beca
use a div tag is one of the siblings. -->
<div></div>
<p></p> <!-- the CSS selector will select this element beca
use a div tag is one of the siblings. -->
</body>
<br>
Pseudo-Selectors
Pseudo-Class Selectors
a:hover {
font-size: 12px;
color: pink;
font-weight: bold;
}
div:nth-child(2) {
background-color: red;
color: white;
font-weight: normal;
}
Pseudo-Element Selectors
div::selection{
font-size: 12px;
background-color: pink;
}
h1::before{
content: 😎;
}
h1::after{
content: 😎;
}
Note the two colons. This was a change from previous versions
of CSS. Modern CSS uses two colons to better differentiate
between pseudo-class and a pseudo-element.
Attribute Selectors
div[data-tab] {
display: none;
}
div[data-tab=“one”] {
display: block;
background-color: ivory;
}
There are two types of attribute selectors: one that specifies only if
the element has the named attribute and one that specifies the
named attribute and attribute value. This type of selector is
particularly useful if you are using custom attributes in your HTML.
This example targets a div that has the custom attribute data-tab
and sets display to none by default. The next CSS rule is even more
specific when we select the data-tab attribute that has the value of
“one” to display it.
To help you learn these advanced topics, Educative has created the
course The Complete Advanced Guide to CSS
(https://www.educative.io/courses/the-complete-advanced-guide-
to-css). This course teaches you helpful advanced techniques used
by front-end developers every day, all with interactive examples.
By the end, you’ll have not only mastered selectors but other
advanced CSS topics like CSS grid manipulation, custom properties,
dynamic positioning, and more.
Happy learning!
Christina Kopecky
Full Name
Subscribe
(/)
(/explore) (/github-students)
Edpresso
(/edpresso)
Blog
(/blog)
Pricing
(/unlimited)
For Business
(/business)
CodingInterview.com
(//codinginterview.com/)
CONTRIBUTE LEGAL
(/authors) (/privacy)
(/enterprise-terms)
MORE
Our Team
(/team)
Careers
(//angel.co/educativeinc/jobs)
For Bootcamps
For Bootcamps
(//try.educative.io/bootcamps)
(/blog/enterprise)
Quality Commitment
(/quality)
FAQ
(/courses/educative-faq)
Contact Us
(/contactUs)
Copyrigh
©2021
Educative
(//linkedin.com/company/educative- (//www.youtube.com/channel/UCT_8FqzTIr2Q1BOtvX_DPPw/? Inc. All
(//facebook.com/educativeinc) (//twitter.com/educativeinc) (//educativesessions.podbean.com)
inc/) sub_con rmation=1) rights
reserved.