Skip to content

Understanding Inline, Internal, and External CSS Styles

Posted on:April 15, 2023 at 11:00 AM

Before we start talking about applying CSS styles to HTML, it is important we cover the various means by which we can actually write CSS. When styling an HTML document with CSS, you have three options: inline styles, internal styles, and external styles. Each option has its advantages and disadvantages, and understanding the differences between them is crucial to writing clean, maintainable code. In this article, we’ll explore the differences between inline, internal, and external styles, and when to use each.

Inline Styles

Inline styles are styles that are applied directly to an HTML element using the style attribute. Inline styles take precedence over all other styles, including styles defined in internal and external stylesheets.

Here’s an example of an HTML element with an inline style:

<p style="color: red;">This text is red.</p>

While inline styles can be convenient in certain situations, such as quick prototyping or testing, they should generally be avoided in favor of more scalable and maintainable solutions. Inline styles can make it difficult to update styles across an entire website or application, and can clutter your HTML code with unnecessary styling information.

Internal Styles

Internal styles are defined within an HTML document using the <style> element. Internal styles apply only to the document they are defined in and take precedence over external styles, but not over inline styles.

Here’s an example of an HTML document with an internal style:

<!DOCTYPE html>
<html>
<head>
	<title>Internal Styles Example</title>
	<style>
		p {
			color: red;
		}
	</style>
</head>
<body>
	<p>This text is red.</p>
</body>
</html>

Internal styles are a good solution for small-scale websites or individual pages that require unique styles. However, as the size of the website or application grows, internal styles can become difficult to manage and maintain. Additionally, using internal styles can make it difficult to reuse styles across multiple pages or websites.

External Styles

External styles are defined in separate CSS files that are linked to an HTML document using the <link> element. External styles are the preferred way to define styles for large-scale websites and applications because they allow you to centralize your styling information and reuse styles across multiple pages.

Here’s an example of an HTML document with an external style:

<!DOCTYPE html>
<html>
<head>
	<title>External Styles Example</title>
	<link rel="stylesheet" href="style.css">
</head>
<body>
	<p>This text is red.</p>
</body>
</html>

The external style is defined in a separate CSS file called style.css:

p {
  color: red;
}

Using external styles allows you to keep your HTML code clean and focused on content, while your CSS code can handle all the styling information. Additionally, external styles make it easy to update styles across an entire website or application, as you only need to update the styles in one location.

Conclusion

In conclusion, when it comes to styling an HTML document with CSS, there are three options: inline styles, internal styles, and external styles. While each option has its advantages and disadvantages, external styles are generally the preferred way to define styles for large-scale websites and applications. However, inline styles and internal styles can be useful in certain situations, such as quick prototyping or testing. As you become more comfortable with CSS, it’s important to understand the differences between each option and use them appropriately.

What’s next?

In the coming article, we will be covering how to apply CSS to HTML elements. Don’t forget to follow and share this with your friends that are just starting their web development journey