Open In App

How does CSS work under the hood ?

Cascading Style Sheets referred to as CSS, is a stylesheet language used to design and describe the presentation of the webpage to make it attractive. The main use of CSS is to simplify the process of making web pages presentable.

The way elements should be rendered on screen is described by CSS. CSS applies styling to web pages. More importantly, CSS enables this styling independent of the HTML.



CSS is broadly categorized into 3 types:

Why use CSS?



Syntax: A CSS comprises style rules sets that comprise of a selector followed by a declaration block. These style rules are interpreted by the browser. 

p { 
    background-color: aliceblue; 
    font-size: 18px; 
}

Example: In this example, all “h1” elements will be center-aligned, have a green color, and have a font size of 30px.




<!DOCTYPE html>
<html>
  
<head>
    <style>
        h1 {
            color: green;
            font-size: 30px;
            text-align: center;
        }
    </style>
</head>
  
<body>
    <h1>
        Welcome to GeeksforGeeks
    </h1>  
</body>
  
</html>

Output:

How does CSS work under the hood?

Are you confused whether the HTML will be interpreted before CSS’s interpretation or after? Or will it apply as soon as the browser constructs the DOM. Let’s discuss how it actually works. While displaying a document, the browser must combine the document’s content with style information. The document is processed in a number of stages.

Working: 

   Now if the CSS is parsed already, the elements in the parsed CSS get styled immediately as soon as they are laid on the page. (Preferred to load in <head>)  On the other hand, if CSS is loaded late, elements are shown in their ” unstyled form” until their corresponding styles are parsed known as “Flash Of Unstyled Content”.

The below diagram explains the working of CSS in a more simplified manner.

Limitations of CSS:


Article Tags :