Introduction to Linking CSS and HTML
Cascading Style Sheets (CSS) play a crucial role in web development by defining the visual styles and layout of HTML documents. To apply CSS styles to an HTML document, you need to establish a connection between the two. In this article, we will explore the various methods of linking CSS to HTML and understand how to leverage them to create visually appealing web pages.
Inline CSS
One way to apply CSS styles to HTML elements is by using inline CSS. With inline CSS, you can specify the styles directly within the HTML tags using the style
attribute. Here’s an example:
<p style="color: red; font-size: 18px;">Hello, world!</p>
In this example, the style
attribute is used to define the color and font size of the paragraph element. While inline CSS provides a quick way to apply styles, it is generally not recommended for large-scale projects due to its lack of maintainability and separation of concerns.
Internal CSS
Another method of linking CSS to HTML is by using internal CSS. In this approach, you embed the CSS styles within the <style>
tags within the HTML document’s <head>
section. Here’s an example:
<!DOCTYPE html>
<html>
<head>
<style>
p {
color: red;
font-size: 18px;
}
</style>
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
In this example, the CSS styles are defined within the <style>
tags in the <head>
section of the HTML document. The styles are then applied to the <p>
element in the body of the document. Using internal CSS allows you to keep the styles within the HTML document, but it can become cumbersome to maintain and update styles across multiple pages.
External CSS
The most common and recommended method of linking CSS to HTML is by using external CSS files. With external CSS, you separate the styles into a separate CSS file and link it to the HTML document using the <link>
tag. Here’s an example:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<p>Hello, world!</p>
</body>
</html>
In this example, the CSS styles are stored in a file called styles.css
. The <link>
tag with the rel
attribute set to "stylesheet"
and the href
attribute pointing to the CSS file establishes the link between the HTML document and the external CSS file. This approach provides modularity, reusability, and easy maintenance of styles across multiple HTML pages.
Additional Link Attributes
The <link>
tag used to link CSS to HTML supports additional attributes for specific purposes:
media
: Specifies the media type for which the CSS styles are intended. For example, you can usemedia="screen"
to target screens ormedia="print"
to target print media.integrity
: Enables Subresource Integrity (SRI) by providing a cryptographic hash of the linked CSS file. This ensures that the file hasn’t been tampered with.crossorigin
: Specifies how the browser should handle cross-origin requests when retrieving the CSS file. It can be set to"anonymous"
or"use-credentials"
.
Cascading Order
When using multiple CSS sources, such as inline, internal, and external styles, it’s important to understand the cascading order in which styles

My name is Mark Stein and I am an author of technical articles at EasyTechh. I do the parsing, writing and publishing of articles on various IT topics.
+ There are no comments
Add yours