Internal CSS is a method of adding CSS to a single HTML document. The CSS rules are written inside the <style> tag, which is placed within the <head> section of the HTML document.
In this chapter, you will learn what Internal CSS is, its syntax, how to use it, examples of Internal CSS, and its advantages and disadvantages.
Internal CSS is used to apply styles to a single HTML document. All CSS rules are written inside the <style> element, which is placed within the <head> section of the HTML page.
Unlike Inline CSS, where styles are applied to individual elements, Internal CSS allows multiple HTML elements to share the same set of CSS rules. This makes the HTML code cleaner and easier to maintain for a single web page.
The following syntax is used to add Internal CSS to an HTML document.
<head>
<style>
selector {
property: value;
}
</style>
</head>
In this syntax:
The following example demonstrates how to use Internal CSS in an HTML document.
Explanation
In this example:
One of the main advantages of Internal CSS is that a single CSS rule can style all matching HTML elements on the page.
<!DOCTYPE html>
<html>
<head>
<style>
h2 {
color: blue;
}
p {
color: green;
}
</style>
</head>
<body>
<h2>Learning CSS</h2>
<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
</body>
</html>
Explanation
The h2 selector styles every <h2> element, while the p selector styles every paragraph in the document.
Internal CSS can also style elements using class and id selectors.
<!DOCTYPE html>
<html>
<head>
<style>
.highlight {
color: orange;
}
#title {
color: purple;
}
</style>
</head>
<body>
<h2 id="title">CSS Tutorial</h2>
<p class="highlight">This paragraph is highlighted.</p>
<p>This paragraph uses the default style.</p>
</body>
</html>
Explanation
Internal CSS is suitable when:
<style>
table {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
border: 1px solid black;
padding: 8px;
}
</style>
Explanation
The CSS rules are applied only to the current HTML page. Any other HTML page will not use these styles unless the same rules are added to its <style> section.
We request you to subscribe our newsletter for upcoming updates.