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:
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 Content Using web-sa
2 min read
How to Add Image in CSS?
Images are an essential part of web design, bringing the visual appeal and providing the context to content. In web development, images can be added to the websites using HTML and styled or manipulated using CSS. CSS can offer various ways to incorporate images into web pages, allowing the developer
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
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
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 usi
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:
3 min read
How To Add Text Outline With CSS?
Since CSS does not provide a direct text-outline property, the CSS text-shadow property is used to create an outline effect around text by adding multiple shadows with offsets. 1. Using text-shadow PropertyThe text-shadow property is commonly used to add shadows to text, by applying multiple shadows
1 min read
How to Add Shadow to Image in CSS ?
Adding shadow to an image can enhance its visual appeal and make it stand out on your webpage. In this article, we will explore various approaches to add shadow to an image using CSS. Table of Content Add Shadow on Image using box-shadow PropertyAdd Shadow on Image on HoverAdd Shadow on Image using
2 min read