Academic Block

Add Internal CSS Get Certified

What is Internal CSS?

Internal CSS is a method of styling a web page by writing CSS rules inside the <style> element within the <head> section of an HTML document. The styles apply only to that single HTML page, making Internal CSS useful when a webpage requires unique styling that is not shared with other pages.

Why Use Internal CSS?

Internal CSS is ideal for styling a single webpage without creating a separate CSS file. It keeps all HTML and CSS in one document, making it convenient for small websites, testing layouts, creating prototypes, and learning CSS. Since the styles remain inside the HTML file, they only affect that specific page.

Syntax of Internal CSS

What this code does: Places a <style> block inside the <head> so any CSS rules written there apply only to this HTML page.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
h1 {
    color: blue;
}
</style>
</head>
<body style="font-family:Arial,sans-serif;padding:24px;">
<h1>Hello World</h1>
<p>This heading is styled with Internal CSS.</p>
</body>
</html>

Step 1: Add the <style> Tag

Inside the <head> section of your HTML document, create a <style> element. This is where all CSS rules for the page will be written.

What this code does: Adds a light-blue background to the whole page by putting a simple body rule inside a <style> tag in the head.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    background: #ADD8E6;
    font-family: Arial, sans-serif;
    padding: 24px;
}
</style>
</head>
<body>
<h1>Light Blue Background</h1>
<p>The body background is set with Internal CSS.</p>
</body>
</html>

Step 2: Write CSS Rules

After creating the <style> element, write your CSS rules inside it. The browser reads these rules and applies them to the matching HTML elements on the same page.

What this code does: Styles a full page with a light background, Arial text, a cyan heading, and a pink paragraph — all rules live inside the same HTML file.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    background: #f5f9ff;
    font-family: Arial, sans-serif;
    padding: 24px;
}
h1 {
    color: #0DADE0;
}
p {
    color: #D92759;
    font-size: 18px;
}
</style>
</head>
<body>
<h1>Hello, John</h1>
<p>This paragraph is styled using Internal CSS.</p>
</body>
</html>

How Internal CSS Works

When the browser loads an HTML document, it reads the CSS rules inside the <style> element before rendering the page. These styles are applied only to the current HTML document and do not affect other webpages.

Example of Internal CSS

What this code does: Builds a complete page with internal styles for the body, heading, paragraph, and a blue button that darkens on hover — everything stays in one HTML file.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
    background: #f5f9ff;
    font-family: Arial, sans-serif;
    padding: 24px;
}
h1 {
    color: #0056b3;
}
p {
    color: #444;
    font-size: 18px;
}
button {
    background: #007BFF;
    color: white;
    border: none;
    padding: 10px 20px;
    font-size: 16px;
    border-radius: 5px;
    cursor: pointer;
}
button:hover {
    background: #0056b3;
}
</style>
</head>
<body>
<h1>Welcome to Academic Block</h1>
<p>This webpage is styled using Internal CSS.</p>
<button>Click Me</button>
</body>
</html>

Advantages of Internal CSS

Advantage
Benefit
Description
Single File
Easy Management
HTML and CSS remain in one document.
Quick Testing
Fast Development
Perfect for learning, testing, and prototypes.
Page Specific
No Side Effects
Styles affect only the current webpage.
No External File
Simple Setup
No need to create or link a separate CSS file.
Easy Debugging
Quick Changes
CSS can be modified directly within the HTML page.

Common Mistakes

Beginners often place the <style> element inside the <body>, forget the opening or closing <style> tag, write invalid CSS syntax, or accidentally use multiple conflicting CSS rules. These mistakes may prevent styles from working correctly.

What this code does: Shows the correct placement of <style> inside <head>, which is where the browser expects stylesheet rules.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- Correct placement -->
<style>
h1 {
    color: #16a34a;
}
body {
    font-family: Arial, sans-serif;
    padding: 24px;
}
</style>
</head>
<body>
<h1>Correct: style is inside head</h1>
<p>Always put the <style> tag in the <head>, not in the body.</p>
</body>
</html>

Best Practices

  • Always place the <style> element inside the <head> section.
  • Keep CSS properly indented for better readability.
  • Group related CSS rules together.
  • Use comments to organize large stylesheets.
  • Avoid repeating the same CSS rules.
  • Use meaningful class names whenever possible.
  • Use Internal CSS for page-specific styling only.
  • Move reusable styles to an external stylesheet for large websites.

Web Resources on How to add Internal CSS?

1. MDN Web Docs : Getting started with CSS
2. Dev.to : Inline, Internal, and External CSS Explained
3. Dev.to : Advantages and Disadvantages of Inline, Internal, and External Styles
4. HTML Cheat Sheet.com : CSS syntax and how to include them in HTML

🧪 Test Your CSS Code

Edit the CSS code on the left and click “Run Code” to see the result on the right.

📝 CSS Code
👁️ Preview (HTML rendered with your CSS)

Click “Run Code” to see the result here.