Understand Combinator, Attribute, Pseudo-class & Pseudo-elements Selectors – Part 3 of Fundamental CSS Tutorial

Understand Combinator, Attribute, Pseudo-class & Pseudo-elements Selectors – Part 3 of Fundamental CSS Tutorial

Welcome to the third post on the “Fundamental of CSS” series and in today’s post, I will be covering –

  • Combinator Selector
  • Attribute Selector
  • Pseudo-class Selector
  • Pseudo-elements Selector

Read this blog post or watch the video below to learn about the advanced selectors.

combinator selector

Combinator is the bridge between selectors. It is used to explain the relationship between selectors.
There can be more than one simple selector in a CSS selector. A combinator can be inserted between the simple selectors.
 
There are four different combinator selectors possible –
  • Descendant Selector (space)
  • Child Selector (>)
  • Adjacent Sibling Selector (+)
  • General Sibling Selector (~)
Let’s understand each of these different selectors with some examples.

Descendant Selector (space)

All elements that are descendants of a specified element are matched by the descendant selector. In the below example, I have used the descendent selector “.outer p” to identify all p which are the descendants of the element having outer class. Here space is used as the combinator.

<!DOCTYPE html>
<html>
 <head>
 <style>
 .outer p {
 color: red;
 font-size: 5em;
 text-align: center;
 }
 </style>
 </head>
 <body>
 <div class=“outer”>
 <p>Outer Line 1</p>
 <p>Outer Line 2</p>
 <div class=“inner”>
 <p>Inner Line 1</p>
 <p>Inner Line 2</p>
 </div>
 </div>
 </body>
</html>

Child Selector (>)

The child selector picks all children of a specified element. In the below example, I have used the child selector “.outer > p” to identify all p which is the children of the element having outer class. Here > is used as the combinator. As you can see, this time the style is only being applied to the children of the outer class, not all the descendants. This is the difference between a child selector and a descendant selector.

<!DOCTYPE html>
<html>
<head>
 <style>
 .outer > p {
 color: red;
 font-size: 5em;
 }
 </style>
</head>
<body>
 <div class=“outer”>
 <p>Outer Line 1</p>
 <p>Outer Line 2</p>
 <div class=“inner”>
 <p>Inner Line 1</p>
 <p>Inner Line 2</p>
 </div>
 </div>
</body>
</html>

Adjacent Sibling Selector (+)

The adjacent selector is used to select one element that is immediately following another.
The parent element of siblings must be the same, and “adjacent” indicates “directly following.” In the below example, I have used the adjacent sibling selector “h1 + h2” to identify the first h2 that is placed immediately after h1 where both h1 and h2 share the same parent. Here + is used as the combinator. As you can see, this time the style is only being applied to the first h2 and not on all h2s.
<!DOCTYPE html>
<html>
<head>
 <style>
 h1 + h2 {
 color: red;
 font-size: 5em;
 }
 </style>
</head>
<body>
 <h1>Technical Potpourri</h1>
 <h2>Line 1</h2>
 <h2>Line 2</h2>
 <h2>Line 3</h2>
 <h2>Line 4</h2>
</body>
</html>

General Sibling Selector (+)

All elements that are next siblings of a specified element are selected by the generic sibling selector. In the below example, I have used the general sibling selector “h1 ~ h2” to identify all the h2 siblings of h1. Here ~ is used as the combinator. As you can see, this time the style is being applied to all h2s.

<!DOCTYPE html>
<html>
<head>
 <style>
 h1 ~ h2 {
 color: red;
 font-size: 5em;
 }
 </style>
</head>
<body>
 <h1>Technical Potpourri</h1>
 <h2>Line 1</h2>
 <h2>Line 2</h2>
 <h2>Line 3</h2>
 <h2>Line 4</h2>
</body>
</html>

Attribute selector

HTML elements containing specified attribute values can also be styled. To select elements with a specific attribute, use the [attribute] selector. In the below example, I have used the type attribute from the input element to provide different styles for different input types. For example, text inputs are having border-color as green whereas the email inputs are having border-color as red.
<!DOCTYPE html>
<html>
<head>
 <style>
 input[type=‘text’] {
 width: 100%;
 padding: 12px 20px;
 margin: 8px 0;
 box-sizing: border-box;
 border-color: green;
 }
 input[type=’email’] {
 width: 100%;
 padding: 12px 20px;
 margin: 8px 0;
 box-sizing: border-box;
 border-color: red;
 }
 </style>
</head>
<body>
 <div>
 <h2>Registration Form</h2>
 First Name: <input type=“text”/><br/>
 Last Name: <input type=“text”/><br/>
 Email: <input type=“email”/><br/>
 </div>
</body>
</html>
Attribute Selector

Pseudo-Class selector

A pseudo-class selector is used to define an element’s particular state. There are many states which can be targetted, but below are few examples where pseudo-class selectors can be used.
  • When a user hovers their mouse over an element, it is styled.
  • First Element, last element, or the n-the element.
In the below example, I have used –
  • “.outer h2:first-child” to put the style on the first h2.
  • “.outer h2:last-child” to put the style on the last h2.
  • “.outer h2:nth-child(2)” to put the style on the second h2.
  • “a:hover” to put the style a when someone will hover on the link
<!DOCTYPE html>
<html>
<head>
 <style>
 .outer h2:first-child {
 color: green;
 }
 .outer h2:last-child {
 color: orange;
 }
 .outer h2:nth-child(2) {
 color: blue;
 }
 a:hover {
 color: orange;
 }
 </style>
</head>
<body>
 <div class=“outer”>
 <h2>Line 1</h2>
 <h2>Line 2</h2>
 <h2>Line 3</h2>
 <h2>Line 4</h2>
 </div>
 <a href=“#”>Try hovering over this link.</a>
</body>
</html>
Pseudo-Class Selector

Pseudo-Elements selector

Style can be applied to certain parts of an element with a CSS pseudo-element. It can, for example, be used to:
  • Put style on the initial letter, or line, of an element.
  • Insert content before or after an element’s content.
In the below example, I have used –
  • “.outer p::first-letter” to put the style on the first letter.
  • “.outer p::first-line” to put the style on the first line.
<!DOCTYPE html>
<html>
<head>
 <style>
 .outer p::first-letter {
 color: red;
 font-size: 5em;
 }
 .outer p::first-line {
 font-size:1em;
 font-family: Georgia, ‘Times New Roman’, Times, serif;
 font-style: italic;
 }
 </style>
</head>
<body>
 <div class=“outer”>
 <p>Technical Potpourri. <br/>
 Sudipta Deb</p>
 </div>
</body>
</html>
Pseudo-Element Selector

Conclusion

Note – If you would like to see the code, here is the Github link:
 
Please provide your feedback/questions in the comment section. I will see you in the next tutorial soon. Till then stay safe and keep learning.

Comments

Understand Basic CSS Selectors and Specificity – Part 2 of Fundamental CSS Tutorial

Understand Basic CSS Selectors and Specificity – Part 2 of Fundamental CSS Tutorial

Welcome to the second post on the “Fundamental of CSS” series and in today’s post, I will be covering –

  • What is CSS Selector?
  • Basic CSS Selectors
  • CSS Specificity
  • CSS Specificity Rules

Read this blog post or watch the video below to learn about the advanced selectors.

What is CSS Selector?

A CSS Selector is made up of an element selector and a value that identifies a web element on a page. HTML tags, attributes, Id, and Class are all represented as strings. As such, they are patterns that match against tree elements and are one of the various approaches for selecting nodes in an XML text.

Basic CSS Selectors

Here are the basic CSS Selectors.

Id

We can target an id by prefixing the hash symbol to a selector. This is by far the most popular application; however, when using id selectors, exercise caution. Id selectors are not flexible and don't enable reuse. If at all possible, use a tag name, one of the new HTML5 components, or even a pseudo-class as a starting point.

<!DOCTYPE html>
<html>
<head>
 <style>
 /* ID Selector */
 #helloWorld {
 color: green;
 text-align: center;
 font-size: 5em;
 }
 </style>
</head>
<body>
 <h1 id="helloWorld">CSS Selectors and Specificity </h1>
</body>
</html>

In the above code, "helloWorld" id is being used to apply to style. Adding the # symbol before the id is the way to use the ID selector.

class

<!DOCTYPE html>
<html>
<head>
 <style>
 /* ID Selector */
 #helloWorld {
 color: green;
 text-align: center;
 font-size: 5em;
 }
 /* Class Selector */
 .heading{
 color: blue;
 text-align: center;
 }
 </style>
</head>
<body>
 <h1 id="helloWorld">CSS Selectors and Specificity </h1>
 <h2 class="heading">Technical Potpourri</h2>
 <h2 class="heading">Sudipta Deb</h2>
</body>
</html>

In the above code, I have used the class "heading" to select the element. The way to use the class selector is using the . (dot) symbol before the class name. The difference between id and class selector is that you can target multiple components with the latter. When you want your styling to apply to a group of elements, use classes. 

Type

<!DOCTYPE html>
<html>
<head>
 <style>
 /* ID Selector */
 #helloWorld {
 color: green;
 text-align: center;
 font-size: 5em;
 }
 /* Class Selector */
 .heading{
 color: blue;
 text-align: center;
 }
 /* Type Selector */
 p {
 color: black;
 text-align: center;
 }
 </style>
</head>
<body>
 <h1 id="helloWorld">CSS Selectors and Specificity </h1>
 <h2 class="heading">Technical Potpourri</h2>
 <h2 class="heading">Sudipta Deb</h2>
 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor 
 incididunt ut labore et dolore magna aliqua. 
 Vitae nunc sed velit dignissim sodales ut eu sem integer. 
 Tellus id interdum velit laoreet id. 
 </p>
</body>
</html>
What if you wanted to target all elements on a page based on their kind instead of using an id or classname? Then use a type selector. In the above code, I have used the type selector to apply the style to all paragraphs on my webpage. 
 
There are few more advanced selectors available as well in CSS, which I will be covering in my next post. But these three basic selectors are good enough to understand the specificity concept from CSS.
 
Note - If you would like to see the code, here is the Github link:

CSS Specificity

As per MDN - 

Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. 

That is, CSS specificity is a collection of rules that browsers utilize to determine which of the developer-defined styles will be applied to a particular element. To apply a style to a specific element, the developer must follow the guidelines so that the browser understands how to do it.

Let's understand the hierarchy of CSS Specificity. CSS calculates specificity in 0-0-0-0 model. Don't worry. I will explain what is this strange 0-0-0-0 means.

As you can see from the above image (specificity is mentioned in highest to lowest) , 

  • First left-hand side 0 represents the weightage of Inline styling.
  • The next one represents the weightage of ID styling.
  • Next one represents the weightage of Class styling.
  • and the last one (right most) represents the weightage of Tag styling. 

The specificity of a style is determined by the position of the selector in relation to other competing selectors. 

The best way to understand this by going through some examples.

Example 1 - Inline Styling

<!DOCTYPE html>
<html>
<head>
 <style>
 </style>
</head>
<body>
 <!-- CSS Specificity: 1-0-0-0 -->
 <h1 style="color: blue; text-align: center;">
 CSS Specificity - Inline Styling  - Technical Potpourri
 </h1>
</body>
</html>

In this code, I have used Inline Styling to style my h1. And according to the above arithmetic model, CSS Specificity here is 1-0-0-0 as the first digit represents the Inline Styling weightage.

Example 2 - ID Styling

<!DOCTYPE html>
<html>
<head>
 <style>
 /* ID Selector */
 /* CSS Specificity: 0-1-0-0 */
 #helloWorld {
 color: green;
 text-align: center;
 }
 </style>
</head>
<body>
 <!-- CSS Specificity: 1-0-0-0 -->
 <h1 style="color: blue; text-align: center; font-size: 5em;">
 CSS Specificity - Inline Styling
 </h1>
 <h1 id="helloWorld">Technical Potpourri</h1>
</body>
</html>

In this code, I have used ID Styling to style my second h1. And according to the above arithmetic model, CSS Specificity for that h1 is 0-1-0-0 as the second digit represents the ID Styling weightage.

Example 3 - Class Styling

<!DOCTYPE html>
<html>
<head>
 <style>
 /* ID Selector */
 /* CSS Specificity: 0-1-0-0 */
 #helloWorld {
 color: green;
 text-align: center;
 }
 /* Class Selector */
 /* CSS Specificity: 0-0-1-0 */
 .heading{
 color: orange;
 text-align: center;
 }
 </style>
</head>
<body>
 <!-- CSS Specificity: 1-0-0-0 -->
 <h1 style="color: blue; text-align: center; font-size: 5em;">
 CSS Specificity - Inline Styling
 </h1>
 <h1 id="helloWorld">Technical Potpourri</h1>
 <h2 class="heading">Sudipta Deb</h2>
</body>
</html>

In this code, I have used Class Styling to style h2. And according to the above arithmetic model, CSS Specificity for that h2 is 0-0-1-0 as the third digit represents the Class Styling weightage.

Example 4 - Type Styling

<!DOCTYPE html>
<html>
<head>
 <style>
 /* ID Selector */
 /* CSS Specificity: 0-1-0-0 */
 #helloWorld {
 color: green;
 text-align: center;
 }
 /* Class Selector */
 /* CSS Specificity: 0-0-1-0 */
 .heading{
 color: orange;
 text-align: center;
 }
 /* Type Selector */
 /* CSS Specificity: 0-0-0-1 */
 p {
 color: black;
 text-align: center;
 }
 </style>
</head>
<body>
 <!-- CSS Specificity: 1-0-0-0 -->
 <h1 style="color: blue; text-align: center; font-size: 5em;">
 CSS Specificity - Inline Styling
 </h1>
 <h1 id="helloWorld">Technical Potpourri</h1>
 <h2 class="heading">Sudipta Deb</h2>
 <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod 
 tempor incididunt ut labore et dolore magna aliqua. 
 Vitae nunc sed velit dignissim sodales ut eu sem integer. 
 Tellus id interdum velit laoreet id. 
 </p>
</body>
</html>

In this code, I have used Type Styling to style p. And according to the above arithmetic model, CSS Specificity for that h2 is 0-0-0-1 as the fourth digit represents the Type Styling weightage.

CSS Specificity Rules

Again the best way to understand this is by going through examples.

Example 1 - Higher Specificity will override Lower Specificity

<!DOCTYPE html>
<html>
<head>
 <style>
 /* ID Selector */
 /* CSS Specificity: 0-1-0-0 */
 #helloWorld {
 color: green;
 text-align: center;
 }
 /* Class Selector */
 /* CSS Specificity: 0-0-1-0 */
 .heading{
 color: orange;
 text-align: center;
 }
 </style>
</head>
<body>
 <!-- CSS Specificity: 1-0-0-0 -->
 <h1 id="helloWorld" class="heading" style="color: blue; text-align: center; 
 font-size: 5em;">CSS Specificity
 </h1>
</body>
</html>
In the above code, I have used Inline Styling,  ID Styling, and Class Styling. But the specificity for Inline Styling is 1-0-0-0 which is definitely higher than any other CSS specificity. That is why when you open this page in the browser you will see that inline styling is being applied into h1 and not the other style. This is due to the fact CSS will apply the style with higher specificity.
Now if I change the same example a little bit by removing the inline styling, then the code will look like this -
<!DOCTYPE html>
<html>
<head>
 <style>
 /* ID Selector */
 /* CSS Specificity: 0-1-0-0 */
 #helloWorld {
 color: green;
 text-align: center;
 }
 /* Class Selector */
 /* CSS Specificity: 0-0-1-0 */
 .heading{
 color: orange;
 text-align: center;
 }
 </style>
</head>
<body>
 <h1 id="helloWorld" class="heading">CSS Specificity</h1>
</body>
</html>

In the above code, I have used ID Styling and Class Styling. But the specificity for ID Styling is 0-1-0-0 which is definitely higher than any other CSS specificity. That is why when you open this page in the browser you will see that ID styling is being applied into h1 and not the other style. This is also due to the fact CSS will apply the style with higher specificity.

Example 2 - Class Selectors will override multiple Element Selectors

Let's first write the code as shown below -

<!DOCTYPE html>
<html>
<head>
 <style>
 /* Class Selector */
 /* CSS Specificity: 0-0-1-0 */
 .heading{
 color: orange;
 text-align: center;
 }
 /* Type Selector */
 /* CSS Specificity: 0-0-0-3 */
 body > div > h1 {
 color: green;
 text-align: center;
 }
 </style>
</head>
<body>
 <div>
 <h1 class="heading">CSS Specificity</h1>
 </div>>
</body>
</html>

In the above code, I have used Class Styling and Type Styling. Specificity for Class Styling is 0-0-1-0 which is definitely higher than Type Styling specificity which 0-0-0-3. As 0-0-1-0 is greater than 0-0-0-3, the class styling will be applied to h1.

Example 3 - The last rule wins

Here is the code -

<!DOCTYPE html>
<html>
<head>
 <style>
 /* ID Selector */
 /* CSS Specificity: 0-1-0-0 */
 #helloWorld {
 color: green;
 text-align: center;
 }
 /* ID Selector */
 /* CSS Specificity: 0-1-0-0 */
 #helloWorld {
 color: blue;
 text-align: center;
 }
 </style>
</head>
<body>
 <h1 id="helloWorld">CSS Specificity</h1>
</body>
</html>

In the above code, I have used two Class Stylings. Specificity is the same for the selectors. So in this type of situation when there is a draw, always the one written at the last will win. So here the text will have a blue color.

Example 4 - The !Important rule

Let's write the code as shown below -

<!DOCTYPE html>
<html>
<head>
 <style>
 /* ID Selector */
 /* CSS Specificity: 0-1-0-0 */
 #helloWorld {
 color: green;
 text-align: center;
 }
 /* Class Selector */
 /* CSS Specificity: 0-0-1-0 */
 .heading{
 color: orange !important;
 text-align: center;
 }
 </style>
</head>
<body>
 <h1 id="helloWorld" class="heading">CSS Specificity</h1>
</body>
</html>
In the above code, I have used ID Styling and Class Styling. As per the model, ID Styling should be applied as the specificity for ID Styling is higher than Class Styling, but when you will open this page, you will find that the text color is orange. The reason is the !important keyword. Important is the magic wand which will override anything and always apply that style.
 
I will never recommend you to use the !important keyword as this makes it very difficult to maintain the code. I will rather recommend you to use chrome debugger to find out which CSS style is being used and then change accordingly. In the youtube video, I have shown how you can use chrome debugger. Please watch it below -

Final words

Don't get frustrated next time you have a clashing style! Remind yourself of the hierarchy of specificity, which starts with inline styles and progresses through IDs, classes, types.
Also, keep in mind that you should only be as specific as you need to be while developing CSS. In some circumstances, being overly explicit with your style standards will make it more difficult to challenge such styles.
Note - If you would like to see the code, here is the Github link:
Please provide your feedback/questions in the comment section. I will see you in the next tutorial soon where I will cover "Attribute Selectors, User Interface Selectors, Relational Selectors & Combinators". Till then stay safe and keep learning.

Comments

CSS Introduction & Setup – Part 1 of Fundamental CSS Tutorial

CSS Introduction & Setup – Part 1 of Fundamental CSS Tutorial

This post is the first post of the “Fundamental of CSS” series. In this post, I will be covering –

  • Introduction to CSS
  • Why we should use CSS?
  • CSS Syntax
  • Ways to include CSS
  • Setup you need to continue in this tutorial
  • And finally, the “Hello World” program

Read this blog post or watch the video below to learn about the advanced selectors.

What is CSS?

CSS stands for Cascading Style Sheets. CSS is the presentation layer of the web. It describes how the different HTML elements will be displayed. It can control multiple web pages look and feel from one central positions, thus saving a lot of work.

Why we should use CSS?

Here are the few advantages of using CSS.

Maintainability

CSS provides the consistency while creating the website. When a change is needed to your website let’s say font color, button background color etc., you will do the changes in the CSS style sheet and the same changes will be reflected in your entire website, in every pages of the website. If you have a large website, then this simple feature will save you lot of time.

Performance

One way to improve speedy delivery of CSS is by using inlining. Inlining is nothing but inserting the some content of your CSS style sheet directly into the HTML documents. This is good when you are dealing with small resources. This approach cuts down the amount of bandwidth your browser needs to download before it can render the page.
 
Now the above trick is good when you are dealing with small amount of CSS. Browser will not cache the CSS, so for every future rendering of the page, browser will load the same CSS again from the scratch, which doesn’t make much sense. That is why you will find many developers use the inlining only in the home page, but use external CSS is other pages. If you are thinking what is external CSS, don’t worry. I will explain that as well very soon in this post.
 
Another way to solve the above problem is to load the CSS asynchronously, which is not possible natively. But using loadCSS.js you can asynchronously load the CSS content.

Appearance

CSS will allow to make your website stylish because it offers a wide array of expressive style capabilities. With CSS, you as a website developer has more authority to decide the look and feel of your website. CSS is that magic wand which you need while designing your website.

Search Engine Optimization (SEO)

The moment you incorporate CSS into your website design, you will find the search engines will find your website more easily. CSS’s clear coding language makes it easier for search engines to understand the content of your website. By making your website search engine optimized, you will grow your audience.

Browser Compatibility

CSS will work with almost all the known browsers. Any browser compatibility issue is always taken very seriously and resolved as quickly as possible.

CSS Syntax

CSS syntax consists of two parts – selector and a declaration block.

In the above code, p is the selector and rest is the declaration block. Inside the declaration block, color is the property and green is the value.

Ways to include CSS

There are three ways to include CSS and here they are –

External Style Sheet

In the above code, helloWorld.css is the external stylesheet (a separate file) where all the CSS codes will be written and then this is the way to include that externals style sheet in the code.

Embedded Styles

You can also embed the style between style tags like shown above.

You can also embed the style between style tags like shown above.

Inline Styles

You can also embed style right next to the selector. This is good for quick testing, but is not a best practice for production ready code. You should try to put all your styles in external style sheet and then add that in your code as shown in the first option.

Setup 

You basically need Visual Studio Code where we will be writing all the codes. Installing Visual Studio Code is very straight forward and you can read the installation instruction here .
 
Once Visual Studio Code is installed, you need to install one extension named “Live Server” from Marketplace and that’s all. If you need help in installing this extensions, I have covered that in my video for this post. Video is added at the top of this post.

Hello World Program

Here is the very basic index.html and helloWorld.css.

index.html

<!DOCTYPE html>
<html>
<head>
 <link rel=“stylesheet” href=“helloWorld.css”/>
</head>
<body>
 <p>Hello World! – Welcome to Technical Potpourri</p>
</body>
</html>

helloWorld.css

p {
 color: green;
 text-align: center;
 font-size: large;
} 
As you can see from the above code, I have added the styling in helloWorld.css and then added the same into my index.html.
The code is basically adding the styles (mentioned in helloWorld.css) in all the p directives present in index.html.
With that here is the output:
Please provide your feedback / questions in the comment section. I will see you in the next tutorial soon where I will cover “Understand basic CSS Selectors and Specificity“. Till then stay safe and keep learning.

Comments