How To Provide Excellent Customer Service

According to a popular saying, 80 % of a restaurant’s business comes from 20% of its customers. Your customers make up the most part of your business and their satisfaction is your number one…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




How CSS works? A look behind the scenes

CSS FAQS

Getting started with CSS is a cakewalk, but knowing what’s going on under the hood is completely different!

I’m assuming that you know the basic syntax of writing CSS. In this article, you’ll learn the silent concepts of CSS like CSS Parsing process, Specificity, Value Processing, Inheritance, and so on.

Why should I learn all these concepts? It will set you apart from many Front-end developers out there, and knowing the core of CSS may help you in your frontend job interviews! 😄

According to MDN web docs,

In this section, we’ll look at ‘what happens on our browser when we surf(request) a webpage?’.

The very first step is the loading of the initial HTML file in the browser. Then, the decoding of HTML code happens line-by-line, that is, the parsing phase of HTML.

After these two steps, the browser builds the DOM or Document Object Model, which is a family tree of the entire decoded HTML.

You can visualize the flow of these processes from the flowchart given below.

Webpage load-up flowchart
Webpage load-up flowchart

While parsing the HTML, the browser sees a <link> tag to our CSS inside the header tag, so the browser starts loading of CSS too!

Then, the CSS is parsed, and in this process, the browser carries out two major tasks:

(The parsing process is a bit complicated which we will discuss extensively in the next section.)

After parsing the CSS, the browser builds a CSS Object Model(CSSOM), similar to the DOM.

Finally, the DOM and CSSOM together yield the Render Tree which is responsible for rendering the page. Yay!

— — — — — — — — — — — — —

Cascade

For example, the ‘font-size’ property of an element can be defined in several stylesheets or multiple times in the same stylesheet. This ambiguity forms conflicts that are resolved in this process.

💡 You may know the general meaning of ‘Cascade’, but do you know why is it prefixed with ‘Stylesheet’? Let’s check the reason behind it

There are multiple sources of CSS:

So, the browser cascades all of these stylesheets after resolving the conflicts. This is why the Cascade word is prefixed with Stylesheets in CSS.

To properly understand the Cascade process, we strictly need to understand what is declaration conflicts. If it’s not clear yet then read the above section again.

Now, we know what are conflicts and how does it occur. The browser resolves these conflicts by figuring out the Importance, Selector specificity, and Source order of conflicting declarations.

Precedence order of conflict resolution

(i) User !important declaration

(ii) Author !important declaration

(iii) Author declaration

(iv) User declaration

(v) Browser default declaration

If the conflict isn’t resolved yet or conflicting declarations have the same importance, then the Selector specificity figures out the conflict.

It defines the elements to which a set of CSS rules apply. For example,

In the above example, there are 2 Class selectors(.animate and .slide-up), 1 ID selector(#heading), and 2 Elements(h1 and div). These multiple selectors together generate Specificity.

Specificity is a function of (Inline, IDs, Classes, Elements). Just count the number of selectors in the rule and determine the specificity. For example, in the above example, specificity is (0, 1, 2, 2) as we’ve already counted the number of each type of selector. The first value is 0 because there’s no inline-style.

The universal selector (*) has low specificity, while ID selectors are highly specific!

Selector Precedence order
Selector Precedence order

(i) Inline CSS ( <button style=“color : red;” /> )

(ii) IDs (#idname)

(iii) Classes (.className), pseudo-classes (:hover, :focus), attributes

(iv) Elements (h1, div), pseudo-elements (:after, :before)

A selector with 1 ID is more specific than one with 1000 classes.

A selector with 1 class is more specific than one with 1000 elements.

Tip: Rely more on specificity than on the order of selectors.

If there’s still a tie, then the last CSS declaration will be applied.

The last declaration in the code will override all other declarations and will be applied.

— — — — — — — — — — — — —

Value Processing is a six-steps process. We will discuss how exactly the declared values are processed in six different steps starting from the declared value to the final actual value.

Let’s understand it by an example

(Please zoom the above picture for better visibility. I’ve written the same CSS example code below)

In the above example, the HTML paragraph (<p>) tag is associated with two CSS rules, i.e., p element selector and .amazing class selector. There is a width property defined in both of these selectors, forming a conflict!

How to resolve it? Yes, cascade, and in this case, it’s quite simple because a Class selector is always more specific than an element selector.

So, the cascaded value for the <p>tag will be 66% of the parent container. We need to find the actual value, i.e., in pixels, after applying the six-step method.

Step 1: DECLARED VALUE (author declaration)

Declared values are all the values/ properties written in the CSS file. In our example, declared values for width property of <p> is 140px and 66%.

Step 2: CASCADED VALUE (after the cascade)

In this step, the conflicts are resolved and cascaded. So, we are left with a single Cascaded value, which is 66%.

Step 3: SPECIFIED VALUE (defaulting, if there is no cascaded value)

Each property has an initial default value, used if nothing is declared by author or user (and if there is no inherited value like font-family is inherited from the parent element, if not specified).

For example, the browser’s default font size is 16px.

In our case, the Cascaded value is available, so the Specified value is also 66%.

Step 4: COMPUTED VALUE (converting relative values to absolute)

In this step, values with relative units are converted to pixels so that they can be inherited. CSS keywords like auto, bold, etc are computed and replaced in this step.

Here, in our case, we’ve percentage value which is technically not a unit. So, the computed value is also 66%.

Step 5: USED VALUE (final calculation based on layout)

In this calculation step, the CSS engine uses the rendered layout to figure out some of the remaining values, for example, percentage(%), view height(vh) and view width(vw) values that depend upon the layout.

In our example, the parent of <p> tag is <div> with a width of 280px declared in .section class. So, the width of our <p> tag will be ( 66% of 280px ), that is, 184.8px.

Step 6: ACTUAL VALUE (browser and device restrictions)

The browsers do not display pixels with decimal values, so it simply round-off the pixel values which are in turn the Actual value of a property.

In our case, 184.8px will be round-off to 185px.

Finally, the width property is converted from 66% to 185px.

Now you know how this works behind the scenes of CSS!

(This isn’t the end. There are a few other topics like How inheritance works internally, CSS Architecture, Components, and BEM. Maybe, I’ll cover these topics in a later blog in continuity to this part.)

— — — — — — — — — — — —

Kudos! You’re a step ahead of other developers.👏

Add a comment

Related posts:

It is Not Worth Giving Up Your Life Over A Loss

I hesitated before deciding to write this article. Although I have long healed, writing this out to the public made me felt exposed and vulnerable. However, I hope to use my story to help you if you…

The Point about reviews and summaries

when I saw several channels on telegram that highlights that they save your time because of sharing short summaries of lectures and business- related books I thought that it doesn’t feel right from…

Unspoken Emotional Distress and Its Role in Physical Illness

Illness can be brought on or exacerbated by an inability to communicate our mental concerns or emotional needs. Uncertain times can turn our lives into a whirlwind of confusion. Alexithymia is often unrecognized as a physical manifestation of emotional difficulties.