How to Link External CSS to HTML?
Last Updated :
08 Oct, 2024
External CSS is a method used to style multiple HTML pages with a single stylesheet. This approach involves creating a separate CSS file with a .css extension that contains style properties applied to various selectors (such as classes, IDs, headings, etc.). By using external CSS, you can maintain a consistent design across multiple web pages efficiently.
How to Link an External CSS to HTML
To link an external CSS file to an HTML document, you need to use the <link> element within the <head> section of your HTML file. The <link> element should have the rel attribute set to "stylesheet" and the href attribute specifying the path to your CSS file.
Syntax:
To link an external CSS file to an HTML document, you need to use the <link> element within the <head> section of your HTML file. The <link> element should have the rel attribute set to "stylesheet" and the href attribute specifying the path to your CSS file.
<link rel="stylesheet" href="path/to/your/styles.css">
Example 1: In this example, we are using an external CSS style to provide styling to our div, h1, and p tags.
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="geeks.css" />
</head>
<body>
<div class="main">
<h1 class="GFG">
GeeksForGeeks
</h1>
<p id="geeks">
A computer science portal for geeks
</p>
</div>
</body>
</html>
CSS
/* Geeks.css */
.main {
text-align: center;
}
.GFG {
font-size: 60px;
color: green;
}
#geeks {
font-size: 25px;
color: skyblue;
};
Output:
.png)
Example 2: Here’s another example demonstrating how to use external CSS to style multiple HTML elements, such as creating card-like sections.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width,
initial-scale=1.0">
<title>External CSS Example</title>
<link rel="stylesheet" href="geeks.css">
</head>
<body>
<h1>GeeksforGeeks</h1>
<p>
A computer science portal for geeks.
</p>
<div class="geeks">
<h2>HTML</h2>
<p>
HTML stands for Hyper Text Markup Language.
</p>
</div>
<div class="geeks">
<h2>CSS</h2>
<p>
CSS stands for Cascading Style Sheet.
</p>
</div>
</body>
</html>
CSS
/* Geeks.css */
h1 {
color: green;
margin-bottom: 10px;
}
p {
font-size: 18px;
line-height: 1.6;
margin-bottom: 30px;
}
.geeks {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
border: 2px solid black;
border-radius: 10px;
background-color: skyblue;
margin-bottom: 10px;
width: 20%;
}
.geeks h2 {
color: green;
font-size: 24px;
margin-bottom: 10px;
}
.geeks p {
text-align: center;
font-size: 16px;
color: black;
}
Output:
.png)
Advantages of External CSS
- Improved Maintainability: Having styles in a separate file makes it easier to manage and update your styles without modifying each HTML document.
- Enhanced Reusability: The same CSS file can be linked to multiple HTML files, promoting consistent design across your site.
- Efficient Caching: Browsers cache external CSS files, leading to faster page load times on subsequent visits.
Disadvantages of External CSS
- Loading Time: Pages may not render correctly until the external CSS file is fully loaded, potentially causing a flash of unstyled content (FOUC).
- Performance Concerns: Linking multiple CSS files can increase download times, affecting overall site performance.
- Versioning and Caching Challenges: Large-scale projects may encounter difficulties in versioning and caching, leading to inconsistencies in styles.
Linking external CSS to your HTML is a consistent and maintainable design across multiple web pages. By following the steps outlined above, you can easily apply external styles to your HTML documents and enhance the visual appeal of your website. For a seamless experience, ensure that your CSS files are correctly linked and optimized.
Similar Reads
How to Link a CSS to HTML? To link a CSS file to an HTML file, Create a separate CSS file (styles.css) and write styles on it. Now we need to use the <link> element inside the <head> section of the HTML file to attach the CSS file.Syntax:<link rel="stylesheet" href="styles.css">rel="stylesheet": It specifies
3 min read
How to link CSS with HTML Document? CSS is probably the most powerful style language in web development. It allows you to keep presentations of a document separate from the structure using CSS, making the maintenance and updating of a web page quite easy. CSS provides possibilities to style HTML elements, handle layouts, and create re
5 min read
How to Add Tailwind CSS to HTML ? Tailwind CSS is a utility-first framework offering pre-defined classes for rapid styling of HTML elements. It simplifies customization and accelerates web development workflows.Integration Options:CDN Integration: Quickly add Tailwind CSS via a CDN link in the <head> of your HTML.Using npm: In
3 min read
How to override inline styles with external in CSS ? In this article, we are going to learn how we can override inline styles with external CSS. Generally, we use inline CSS to override all the other styles. In some circumstances, we have to do the opposite. We have to override the inline CSS which has come from foreign sources and cannot be removed.
3 min read
How to Style Links in CSS ? Styling links in CSS is one of the fundamentals in web design that helps in creating an interactive and pleasant user experience through navigation. Links are core components for web pages, and using CSS, you can easily come up with the kind of look you want in terms of the overall appearance of the
7 min read
How to Add HTML and CSS into PDF ? HTML and CSS must now regularly be converted into PDF format during web development. PDFs enable the creation of printable documents, information exchange across several platforms, and preserving a webpage's original layout. There are several ways to convert HTML and CSS to PDF files, from simple on
4 min read
HTML <link> Tag The HTML <link> tag defines the relationship between the current document and an external resource, often for stylesheets or favicons. It's an empty element with attributes like href and rel.Note: The <link> tag also supports the Global Attributes and  Event Attributes in HTML.Syntax
2 min read
How to Change Font Color in HTML? We can use <font> tag to change the text color in HTML. This tag was used in older versions of HTML but is deprecated in HTML5. So we can use inline CSS as the best and quickest way to change the font color. 1. Change Font Color using <Font> tagThe <font> tag was used to change tex
2 min read
How to define the URL of an external script file in HTML5 ? In this article, we will learn how to define the URL of an external script file inside HTML. This may be needed when a large script code is needed to be included, and it may create confusion if all the code is written in a single file. Also, there may be a requirement for the same script to execute
1 min read
How to import a CSS file in React ? In React applications, styling is a crucial aspect of creating visually appealing and responsive user interfaces. CSS (Cascading Style Sheets) files are commonly used to define styles for React components. Importing CSS files into React components allows developers to apply styles to specific elemen
3 min read