Understanding Inline, Internal, and External CSS and Their Priority Order

Nitish Kumar Singh

Dec 8, 2023

Hello developers! In this blog post, we will explore Inline, Internal, and External CSS in web development and understand their priority order. For example, if all three types of CSS are applied to an element, then which CSS is effective.

What is CSS?

CSS, or Cascading Style Sheets, is a fundamental language used to style web pages in web development. We use CSS to style web pages, making them visually appealing, controlling their appearance, laying out web pages, and adding animations and transitions. Key components of CSS include CSS selectors (like classes, tags, ids, etc.) used to select or target HTML elements and CSS properties (like color, border, display, etc.) used to apply a specific style to elements using CSS values. The image below shows how YouTube looks with or without CSS.

Image showing use and importance of CSS

Inline CSS

The CSS used in the style attribute of an HTML element is known as Inline CSS. We use inline CSS to quickly style elements. This is easy to apply in HTML files and by using JavaScript. The priority level of inline CSS is above all CSS types. However, the reusability of inline CSS is zero because it is written specifically for one element. Using inline CSS is not a good practice in large projects. The following examples show how inline CSS is applied to an element.

<!-- In HTML file -->
<p style="color: red;font-weight: bold;text-align: justify;font-size: 25px;">
    Hello, I'm a red bold text with justified alignment and a font size of 25px.
</p>
// In JavaScript
const element = document.getElementById("an-element");
// Apply a new set of inline CSS, replacing all applied CSS
element.style = 'color: red;font-weight: bold;text-align: justify;font-size: 25px;';
// Apply a specific style while preserving all other applied CSS
element.style.color = "red";
element.style.textAlign = "justify";

Internal CSS

The CSS used in the style tag under the head of an HTML document is known as Internal CSS. We use internal CSS to easily style elements of an HTML document. This is internal to a single HTML file, so it can be reused on multiple elements. The priority level of Internal CSS is between inline and external CSS. The following example shows how Internal CSS is applied to an element.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
        .para{
            color: red;
            font-weight: bold;
            text-align: justify;
            font-size: 25px;
        }
    </style>
  </head>
  <body>
    <p class="para">Hello, I'm a red bold text with justified alignment and a font size of 25px.</p>
  </body>
</html>

External CSS

The CSS written in another CSS file and linked through a link of rel: stylesheet in the head of an HTML file is known as External CSS. We use external CSS when we want to use the same CSS in multiple HTML files and/or keep it organized in a separate file for better readability and maintainability. The priority level of external CSS is the lowest of all types of CSS. The following codes are example of external CSS.

/* In a file named style.css */
.para {
  color: red;
  font-weight: bold;
  text-align: justify;
  font-size: 25px;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <!-- Link External CSS of the style.css file like below -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <p class="para">
      Hello, I'm a red bold text with justified alignment and a font size of 25px.
    </p>
  </body>
</html>

CSS Priority Order

The CSS priority order of all CSS types is: Inline CSS > Internal CSS > External CSS. The below image show example of the priority order effect of CSS-Types.

Image showing priority order of Inline, Internal and External CSS

The priority order of CSS within a CSS type is: earlier written CSS has a less priority level than later. The priority order of CSS based on the selector is: #id > .class > tag. The below image show example of the priority order effect of CSS-Selector.

Image showing priority order of CSS-Selectors
Published on Dec 8, 2023
Comments (undefined)

Read More

Four Ways to Remove All Children from an HTML Element in JavaScript

Hello, developers! In this blog post, we will explore 4 methods for removing all children, including elements, texts, and comments, from an element in JavaScript.

Dec 7, 2023

20 DOM Methods Every Developer Should Know

Hello developers! In this blog post, we will explore 20 fundamental DOM methods that every developer should be familiar with. The Document Object Model (DOM) is a crucial aspect of web development, serving as a programming interface for web documents.

Dec 6, 2023

Retrieve Element Intended for Click, But Click Event Canceled by Java Code in WebView

I want to retrieve an element (JavaScript object of an HTML element) in JavaScript code that is about to be clicked, but it was not clicked because I canceled the click event in Java code within the onTouch method of OnTouchListener attached to the WebView.

Dec 5, 2023

Understanding Single Page Applications in Pure JavaScript

Hello, developers! In this blog post, we will learn and understand how to build a Single Page Application (SPA) on the web using pure JavaScript and how it works.

Nov 29, 2023

Creating Single Page Applications in Pure JavaScript

Hello developers! In this blog post, we will create a Single Page Application using pure JavaScript, HTML, and CSS. We will not use any library or framework.

Dec 4, 2023

How to Display Images on a Web Page Without Cumulative Layout Shift (CLS)

Hello developers! In this blog post, we will learn how to display images on a web page without Cumulative Layout Shift (CLS) and adhere to our special requirements. This will help us improve our page experience and optimize for Search Engine Optimization (SEO) because significant layout shifts also affect SEO.

Nov 28, 2023