How to Add CSS to HTML
In this tutorial, you will learn how to add CSS to an HTML document using internal, external, and inline styles. CSS (Cascading Style Sheets) is a style sheet language that is used to describe the look and formatting of a document written in HTML or XML. It allows you to style web pages and user interfaces, making it easier to create visually appealing and responsive websites.
How to Add CSS to HTMLβ
There are three ways to add CSS to an HTML document:
1. Internal CSSβ
Internal CSS is added within the <style>
element in the <head>
section of an HTML document. It allows you to define styles that apply only to that specific document. Here's an example of how to add internal CSS to an HTML document:
<!DOCTYPE html>
<html>
<head>
<title>Internal CSS Example</title>
<style>
body {
background-color: lightblue;
}
h1 {
color: navy;
text-align: center;
}
</style>
</head>
<body>
<h1>Welcome to My Website</hh1>
<p>This is an example of internal CSS.</p>
</body>
</html>
In this example, the background color of the <body>
element is set to lightblue
, and the color and alignment of the <h1>
element are defined using internal CSS.
When you run this HTML document in a web browser. It will display the content with the styles defined in the internal CSS.
Welcome to My Website
This is an example of internal CSS.
2. External CSSβ
External CSS is added in a separate CSS file and linked to an HTML document using the <link>
element in the <head>
section. It allows you to define styles that can be shared across multiple documents. Here's an example of how to add external CSS to an HTML document:
<!DOCTYPE html>
<html>
<head>
<title>External CSS Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Welcome to My Website</hh1>
<p>This is an example of external CSS.</p>
</body>
</html>
And create a separate CSS file named styles.css
with the following content:
body {
background-color: lightblue;
}
h1 {
color: navy;
text-align: center;
}
In this example, the CSS styles are defined in a separate file named styles.css
, which is linked to the HTML document using the <link>
element. The styles defined in the external CSS file are applied to the elements in the HTML document.
When you run this HTML document in a web browser. It will display the content with the styles defined in the external CSS.
Welcome to My Website
This is an example of external CSS.
3. Inline CSSβ
Inline CSS is added directly to an HTML element using the style
attribute. It allows you to define styles that apply only to that specific element. Here's an example of how to add inline CSS to an HTML document:
<!DOCTYPE html>
<html>
<head>
<title>Inline CSS Example</title>
</head>
<body>
<h1 style="color: navy; text-align: center;">Welcome to My Website</h1>
<p style="color: green;">This is an example of inline CSS.</p>
</body>
</html>
In this example, the color and alignment of the <h1>
element and the color of the <p>
element are defined using inline CSS.
When you run this HTML document in a web browser. It will display the content with the styles defined in the inline CSS.
Welcome to My Website
This is an example of inline CSS.
By using internal, external, and inline CSS, you can style web pages and user interfaces to create visually appealing and responsive websites.
- Make sure the
styles.css
file is in the same directory as theindex.html
file. - The
<link>
element should be placed in the<head>
section of the HTML document. - The
href
attribute of the<link>
element should point to the location of the external CSS file. <link rel="stylesheet" href="styles.css">
where:rel="stylesheet"
specifies the relationship between the HTML document and the linked CSS file.href="styles.css"
specifies the location of the external CSS file.
- The
style
attribute is used to add inline CSS to an HTML element. - The
style
attribute contains one or more CSS property-value pairs separated by a semicolon. - The CSS property is followed by a colon (
:
) and the property value. - The CSS property-value pairs are enclosed in double quotes (
"
). - The CSS property-value pairs are separated by a semicolon (
;
). - The
style
attribute is added directly to the HTML element. - The
style
attribute overrides any styles applied to the element using internal or external CSS. - The
style
attribute is not recommended for large-scale styling as it can make the HTML document harder to maintain.
Conclusionβ
In this tutorial, you learned how to add CSS to an HTML document using internal, external, and inline styles. Internal CSS is added within the <style>
element in the <head>
section, external CSS is added in a separate CSS file and linked to the HTML document using the <link>
element, and inline CSS is added directly to an HTML element using the style
attribute. By using these methods, you can style web pages and user interfaces to create visually appealing and responsive websites.