How to work with CSS selectors? (with use cases for your next project)✨
Select HTML elements like a PRO and create beautiful web pages!

🚀 Web Designer and learning full Stack Web development.
Search for a command to run...
Select HTML elements like a PRO and create beautiful web pages!

🚀 Web Designer and learning full Stack Web development.
In this series, we'll together learn everything about web designing, tips and tricks to develop better UI.
Emmet is a web-developer’s toolkit for boosting HTML & CSS code writing. You can type expressions and it gets converted into code snippets 🪄. Their tagline is Emmet — the essential toolkit for web-developers which is true in some sense, web-devs us...
Promise.all() is a JavaScript method that takes an array of promises as input and returns a single promise that resolves when all of the input promises have resolved, or rejects if any of the input promises are rejected. It is useful for aggregating ...

Learn about how javascript object works, how we can create our own objects, take a sneak peek into console object and much more!

❓ Scope 📥 Scope defines the area, where functions, variables and other things are available or can be accessed. In layman's terms, scope means where to look for things, what things I can use and what I cannot! Let's take an example, here we are defi...

Learn about Array functions in JS with examples.

Beginners guide to Tailwind with a small project

Isn't it frustrating when you can't make an element look the way you want?
Well... We've all been there!
We all know the importance of CSS and cannot even think of the internet without CSS! No excitement, no fun, no emotions, straight up all the content thrown at your screen. See the amazon without CSS 👁️

Ugly right? You can't afford to have your website look like this! And you don't have to because learning everything about CSS could feel overwhelming but getting started with it and making your website look decent is not hard and you will see how, before this blog ends. I PROMISE 🙊
CSS Selectors should be the very first thing to learn! Before jumping on to the properties or the flex or the grid or any other advanced concepts.

But I don't need a blog explaining to me about selectors, I already know how to change the font size of my p tag? I always use
p {
font-size: 40px;
}
is it different from what I already do?
YES! selecting elements directly is not recommended, though using it in your own small project is still fine, you will rarely find it used in complex web apps. Stick around till the end and you will learn something new!

* and give it properties whatever we like. Most of the time you'll see developers use it to remove browser default margins and padding so that the website looks consistent on all browsers.* {
margin: 0;
padding: 0;
outline: 0;
}
div {
display: inline-block;
min-width: 100px;
min-height: 100px;
background-color: #03203C;
border: 1px solid #fff;
}

You will see this often in other people's code
body {
font-family: 'Roboto', sans-serif;
background-color: #03203C;
padding: 15px;
}
<h1 class="dark-mode">The Modern Web Design Course</h1>
<div id="warning" class="dark-mode">The discount will end tonight!!!</div>
<span class="dark-mode">Special Offer: <del>999</del> 499RS</span>
#warning {
color: #e75757;
padding: 5px 0;
}
.dark-mode {
color: #fff;
}
The main difference between classes and ids is that generally, we give the same class name to multiple elements whereas we give a unique ID to the element where it is required. Though we can give ID to multiple tags as well as HTML is very forgiving but it is a good practice to use ID when we need to select a single element.
IDs are usually used when we are working with JS and we need to select a particular element so that we can pinpoint a certain component 🎯

. between the selectors and we go one level deep each time. del.price-slash {
position: relative;
text-decoration: none;
}
del.price-slash::after {
content: "";
position: absolute;
top: 50%;
left: 0;
width: 100%;
height: 1px;
background: #fff;
transform: rotate(-10deg);
}
The above code will make sure to apply the CSS properties only to the del tag which has a price-slash class. Don't worry about ::after as we'll discuss it in this blog itself!
, with this we can select multiple selectors and can give them properties in one go.<h1 class="dark-mode">The Modern Web Design Course</h1>
<div id="warning" class="dark-mode">The discount will end tonight!!!</div>
<span class="dark-mode">Special Offer: <del class="price-slash text-red">999</del> 499RS</span>
.text-red,
#warning {
color: #e75757;
padding: 5px 0;
}

button nested inside a div and a ul.div ul .button {
background-color: #FF6666;
color: #fff;
padding: 5px 20px;
border-radius: 10px;
border: 0;
}

<div>
<span>
Some Useful content which has to be colored!!
</span>
</div>
<span>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Suscipit, rerum?
</span>
div>span {
background-color: #ff6666;
color: #fff;
font-size: 2rem;
padding: 8px;
}

Lets understand this with the help of an example: We are defining the type as confirmpassword which is a custom type
<form action="#">
<input type="password" name="password" id="password" placeholder="Enter your password"><br />
<input type="confirmpassword" name="confirmpassword" id="confirmpassword" placeholder="Confirm your password">
</form>
[type="confirmpassword"] {
color: #e75757;
-webkit-text-security: disc;
}
Custom attribute example
<div myattri>Some Content inside a div</div>
[myattri] {
font-size: 3rem;
color: #35BDD0;
}


We have talked about how to select elements, but now I want to define what will happen if I try to interact with my element. For that we have pseudo-classes, there are many but we'll discuss the ones that are important and we use extensively.
Whenever you hover over any element, this will get triggered. I am just changing the font size on hover, but we can change as many properties as we like.
.submit-button {
padding: 7px 20px;
border: 0;
border-radius: 15px;
background-color: #EF5354;
color: #fff;
font-size: 1.5rem;
transition: font-size 0.3s ease-in-out;
}
.submit-button:hover {
font-size: 1.8rem;
}

input:focus {
background-color: #ff6666;
color: #03203C;
}

:link, as it targets the links that are already visited.:active, we can target the element which is currently active, in the case of a link, from the time we click on it till the time we hold that mouse click this will be triggered.a:link {
color: #35BDD0;
}
a:visited {
color: #F4BE2C;
}
a:active {
color: #CAD5E2;
}

ul>* {
padding: 4px;
margin: 5px;
list-style-type: none;
color: #fff;
}
li:first-child {
background-color: #35BDD0;
}
li:last-child {
background-color: #F4BE2C;
}
li:nth-child(3) {
background-color: #ff6666;
}


input::placeholder {
padding: 15px;
color: #CAD5E2;
}
Here is an example with a text box.
#name:before {
display: block;
font-size: 1.4rem;
content: "Name: ";
color: #fff;
}
#name:after {
display: block;
height: 10px;
content: "Name is required* ";
font-size: .6rem;
color: #ff6666;
}

That's all for this article now. I hope you learned something new!

You can find more resources if you want to read more about them!
Happy learning!