Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1604

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T09:41:08+00:00 2024-11-25T09:41:08+00:00

How web browsers work – parsing the CSS (part 4, with illustrations)⏳🌐

  • 62k

After the HTML has been parsed, it's time to parse the CSS (found in in external CSS files and in style elements) and build the CSSOM tree (CSS Object Model).

4. CSS PARSING

When the browser encounters a CSS stylesheet, be it external or embeded, it needs to parse the text into something it can use for styling the layouts. The data structure that the browser turns the CSS into is called the CSSOM. The DOM and the CSSOM follow similar concepts, in the sense that they are both trees, but they are different data structures. Just like building the DOM out of our HTML, building the CSSOM out of CSS is considered a render-blocking process.

Tokenization & building the CSSOM

Similar to HTML parsing, CSS parsing starts with tokenization. The CSS parser takes the bytes and converts them into characters, then tokens, then nodes and finally they are linked into the CSSOM. The browser does something called selector machting which means that each set of styles will be matched against all nodes (elements) on the page.

Image description

The browser starts with the most general rule applicable to a node (e.g: if a node it's the child of the body element, then all body styles are inherited by that node) and then recursively refines the computed styles by applying more specific rules. This is why we say that the style rules are cascading.

Imagine we have the HTML and CSS below:

  body {   font-size: 16px;   color: white; }   h1 {   font-size: 32px; }  section {   color: tomato; }  section .mainTitle {   margin-left: 5px }  div {   font-size: 20px; }  div p {   font-size:  8px;   color: yellow; }   
Enter fullscreen mode Exit fullscreen mode

The CSSOM for this code would look something like this:

Image description

Note that in the schema above, the nested elements have both inherited styles (from the parent – e.g: h1 inherits its color from the body and the section inherits its font-size from the body) and their own styles (which can overwrite rules inherited from the parent or not – e.g: p overwrites both the color and the font-size inherited from the div and mainTitle doesn't get its left margin from a parent node).

Since we can have multiple sources for our CSS and they can contain rules that apply to the same node, the browser must decide which rule will apply in the end. That's when specificity comes into play and if you want to read more about it, you can visit this page.

Imagine you're in the airport and you're looking for your friend John. If you want to find him by calling out his name, you could call for “John”. Chances are that more than one John will be in the airport at the same time, so they might all respond. A better approach would be to call your friend using his full name, so that when you shout “John Doe”, you'll have better chances to finding him, since “John Doe” is more specific than just “John”.

On the same note, let's say we have this element:

  <p>   <a href="https://dev.to/">This is just a link!</a> </p>   
Enter fullscreen mode Exit fullscreen mode

and these CSS styles:

  a {    color: red; }  p  a {    color: blue; }   
Enter fullscreen mode Exit fullscreen mode

Which rule do you think will the browser apply? The answer is the second rules, since all anchor tags inside a paragraph selector combination has more specificity than just all anchor tags selector. If you want to play around with specificity, you can use this Specificity calculator.

IMPORTANT
CSS rules are read from right to left, meaning that if we have something like: section p { color: blue; }, the browser will first look for all p tags on the page and then it will look if any of those p tags have a section tag as a parent. If that's the case, it will apply the CSS rule.

Refrence materials:

  • A brief history of CSS until 2016
  • MDN Web Docs

codenewbiecsshtmlwebdev
  • 0 0 Answers
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.