Adding CSS (Cascading Style Sheets) to your HTML is essential for creating visually appealing and user-friendly web pages. In this guide, we will explore the three primary methods to link CSS to HTML documents: inline, internal, and external CSS. Each method has its advantages and best-use scenarios, helping you decide the best approach for your web development needs.
What is CSS?
CSS, or Cascading Style Sheets, is a stylesheet language used to control the layout and appearance of HTML elements on a web page. By linking CSS to your HTML, you can enhance the look and feel of your website, ensuring a better user experience.
Why Use CSS?
- Separation of Concerns: CSS separates content (HTML) from design (CSS), making it easier to manage.
- Consistency: By using CSS, you can maintain a consistent look across multiple pages of a website.
- Responsive Design: CSS enables you to create responsive designs that adapt to different screen sizes.
Methods to Link CSS to HTML
Note: It is a best practice to keep your CSS separate from your HTML
1. Inline CSS
Inline CSS allows you to apply styles directly within HTML tags using the style attribute. This method is useful for small-scale styling or when you need to apply a unique style to a single element.
Example: In this example, we will add CSS using inline styling.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Inline CSS</title>
</head>
<body>
<h2 style="color: green;">
Welcome to
<i style="color: green;">
GeeksforGeeks
</i>
</h2>
</body>
</html>
Output:
.png)
2. Internal CSS
Internal CSS involves adding CSS styles directly within the HTML file by including them inside the <style> tags. This method is more efficient for applying styles to a single HTML document and keeps your CSS code more organized compared to inline styling.
Note: It's best to add the <style> tags within the <head> section to ensure the styles are read before the body content is loaded.
Example: In this example, we will use the internal CSS approach for styling the web page.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Internal CSS</title>
<style>
h2 {
color: green;
}
</style>
</head>
<body>
<h2>Welcome to GeeksforGeeks</h2>
</body>
</html>
Output:
.png)
3. External CSS
External CSS involves creating a separate CSS file with a .css extension and linking it to the HTML file using the <link> tag. This method is the most efficient for large projects, as it separates the design from the content, allowing for better code maintainability and reusability.
Example of External CSS:
1. Create a file named styles.css:
CSS
h2 {
color: green;
font-size: 20px;
}
2. Link this CSS file in your HTML:
HTML
<!DOCTYPE html>
<html>
<head>
<title>External CSS</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h2>Welcome to GeeksforGeeks</h2>
</body>
</html>
Output:
.png)
Linking CSS to HTML allows you to create well-designed web pages. While inline CSS is suitable for small, unique styles, internal CSS is best for single-document styling. However, external CSS is the recommended best practice for larger projects due to its maintainability and reusability.
Similar Reads
How To Add Font In CSS?
Adding fonts to a website enhances its design and readability. In CSS, custom fonts can be applied using several techniques that allow web designers to include specific fonts that arenât system defaults.PrerequisitesHTMLCSSThese are the approaches to add a font in CSS: Table of ContentUsing web-safe
2 min read
How to add .css file to ejs
Styling is one of the most important parts of web development as it enhances the visual appearance of a website. In today's era visual presentation of content holds immense importance. EJS, short for Embedded JavaScript offers a seamless integration of JavaScript functionality within HTML templates.
3 min read
How to add CSS in XML File ?
XML stands for Extensible Markup Language. It is a dynamic markup language. It is used to transform data from one form to another form. In this article, we will discuss how to add CSS in the XML file. The CSS can be used to display the contents of the XML document in a clear and precise manner. It g
2 min read
HTML and CSS
This article aims to provide you with a thorough understanding of how to use HTML and CSS to construct websites or web pages. Here, we present a complete overview to kickstart your journey in learning HTML and CSS. What is HTML?HTML, an acronym for HyperText Markup Language, is the standard language
4 min read
How to Add Stroke using CSS ?
Adding a stroke using CSS means applying an outline or border around text or elements to enhance visibility and style. This effect is typically achieved with properties like `text-shadow` for text or stroke in SVG elements, creating bold, defined edges.Several methods can be used to add strokes usin
2 min read
How to apply inline CSS ?
In this article we will learn how to apply inline CSS, Inline CSS contains the CSS property in the body section attached to the element is known as inline CSS. This kind of style is specified within an HTML tag using the style attribute. It is used to apply a unique style to a single HTML element. S
1 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 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
W3.CSS Cards
A card is a flexible and extensible content container. It can include header, footer and a wide variety of content. W3.CSS helps the developers to add paper-like effects to the elements. It contains three types of classes: Sr. No. Class Name Description 1. w3-card It is used to add a box shadow of 2
3 min read
W3.CSS Badges
The .w3-badge class is used to add additional information to the content. For example, some websites associate some number of notifications to the link. The notification number is seen when logged in to a particular website which tells the numbers of news or notifications to see by clicking it. This
4 min read