EthicsComputerCourses
Join training +91 90532 10052

Internal CSS

CSS · Lesson 8 of 240 Beginner 10 min read Updated

By the end of this lesson you will write internal CSS with the <style> element, place it correctly, control which screens it applies to with the media attribute, and decide when internal CSS is a good choice and when it is not.

You met the <style> element briefly in CSS Syntax & Rules, and the previous lesson, Inline CSS, showed why styling one element at a time does not scale. Internal CSS is the next step up.

What internal CSS is

Internal CSS is CSS written inside a <style> element in the HTML page itself. Unlike the style attribute, it holds complete rules, each with a selector and a block of declarations. That means it can do everything CSS can do: classes, hover states, pseudo-elements and media queries.

HTML
<style>
  h1 { color: #12355b; }
  .price { font-weight: bold; }
  .price:hover { text-decoration: underline; }
</style>

The styles apply only to the page that contains them. Think of a notice pinned up inside one classroom. Every student in that room sees it and follows it, but the class next door never does. If every classroom needs the same rule, pinning a copy in each room quickly becomes a problem, which is exactly what the next lesson, External CSS, solves.

Where to put the style element

Put the <style> element inside the <head>, usually after the <meta> and <title> elements:

HTML
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Chai Point Menu</title>
  <style>
    body { font-family: system-ui, sans-serif; }
  </style>
</head>

Why the head? The browser reads the HTML from top to bottom. Style sheets in the head are render-blocking: the browser waits until it has read them before it draws the page. So when the first pixel appears, the styles are already known, and the visitor never sees the plain unstyled version.

Browsers will also apply a <style> element that appears lower down, inside the <body>. But the content above it may be drawn first without those styles and then jump when they arrive. Keep your page-wide styles in the head and you avoid that problem.

Step 1: style a one-page menu

Here is a complete page for a small chai stall in Kochi. All the styling lives in one <style> element.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Chai Point, Kochi</title>
  <style>
    body {
      font-family: system-ui, sans-serif;
      background: #fdf8f2;
      color: #3b2a1a;
      padding: 1rem;
    }
    h1 { color: #8a3324; margin-bottom: 0.25rem; }
    .tagline { color: #7a6a5a; margin-top: 0; }
    .item {
      display: flex;
      justify-content: space-between;
      max-width: 20rem;
      padding: 0.4rem 0;
      border-bottom: 1px dashed #c9a27e;
    }
    .item:hover { background: #f3e5d5; }
  </style>
</head>
<body>
  <h1>Chai Point</h1>
  <p class="tagline">Fresh chai since 1998, MG Road, Kochi</p>
  <p class="item"><span>Masala chai</span><span>₹20</span></p>
  <p class="item"><span>Cardamom chai</span><span>₹25</span></p>
  <p class="item"><span>Pazhampori</span><span>₹15</span></p>
</body>
</html>

You should see a cream page, a maroon heading and three menu lines with the price pushed to the right. Move the mouse over a line and it gets a light brown background. That hover effect would be impossible with inline styles, because it needs the :hover selector. Do not worry about display: flex yet; you will learn it in CSS Flexbox Introduction.

Several style elements and their order

A page may have more than one <style> element. The browser combines all of them, together with any linked style sheets, in the order they appear in the HTML. When two rules have the same specificity and set the same property, the one that comes later wins.

Step 2: see source order in action

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Two style blocks</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }
    .result { color: #12355b; background: #eef4ff; padding: 0.5rem; }
  </style>
  <style>
    /* Later block: same selector, so these values win */
    .result { background: #e3f6e8; }
  </style>
</head>
<body>
  <p class="result">Class 10 results will be announced on 15 May.</p>
</body>
</html>

The text is dark blue, from the first block, because the second block does not set color. The background is pale green, because both blocks set background and the second one comes later. Swap the two <style> elements and run it again: the background turns pale blue.

The same order rule applies between <link> and <style>. A <style> placed after a <link rel="stylesheet"> can override rules from that file, and a <style> placed before it cannot. You will study this properly in Cascade & Source Order.

The media attribute

The media attribute tells the browser when the styles in a <style> element should apply. Its value is a media query, a condition about the device or screen. If you leave it out, the default is all, so the styles apply everywhere.

HTML
<style media="print">
  /* Only used when the page is printed */
  nav, .ad-banner { display: none; }
</style>

<style media="(max-width: 600px)">
  /* Only used on screens 600px wide or narrower */
  h1 { font-size: 1.5rem; }
</style>

Step 3: a style block for narrow screens

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Media attribute</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; }
    .banner { background: #12355b; color: white; padding: 1rem; font-size: 1.5rem; }
  </style>
  <style media="(max-width: 500px)">
    .banner { background: #8a3324; font-size: 1rem; }
  </style>
</head>
<body>
  <p class="banner">Lucknow Book Fair: 10 to 20 January</p>
</body>
</html>

On a wide output panel the banner is dark blue with large text. Make the output narrower than 500 pixels, by narrowing your browser window or by opening this page on a phone, and the banner turns maroon with smaller text. Inside a style sheet, you write the same idea with an @media rule, which you will learn in Media Queries. Print styles get their own lesson too: Print Stylesheets.

When to use internal CSS

Internal CSS is a good choice when the styles belong to one page only:

  • Single-page projects and demos. A small landing page, a school project or a code example stays in one file, which is easy to share. Every Try-it example in this course uses internal CSS for this reason.
  • Prototypes. You can try ideas quickly without managing files.
  • Small page-specific additions. One special page on a site may need a few extra rules on top of the shared style sheet.
  • Critical CSS. Large sites sometimes put the few rules needed for the top of the page in a <style> element so that it can draw sooner. You will learn this in Critical CSS.

It is a poor choice for a site with many pages. If ten pages each carry a copy of the same <style> block, a colour change means ten edits, and the copies slowly drift apart. The browser also downloads those styles again with every page, because they are part of the HTML. An external file is downloaded once and reused.

MethodHoldsReachCached separately
style attributeDeclarationsOne elementNo
<style> elementFull rulesOne pageNo
External fileFull rulesEvery page that links itYes

Common mistakes

  • Writing <style src="style.css">. The <style> element has no src attribute. To load a file, use <link rel="stylesheet" href="style.css">.
  • Using HTML comments for CSS. Inside <style>, write comments as /* ... */, not <!-- ... -->.
  • Forgetting </style>. Without the closing tag, the rest of the page is read as CSS and your content disappears.
  • Putting the block at the bottom of the body. Content can flash unstyled first. Keep it in the head.
  • Copying the same block into every page. Once two pages share styles, move them to an external file.

Interview questions

1. What is internal CSS?

Internal CSS is CSS written inside a <style> element in an HTML document. It contains full rules with selectors, so it supports classes, pseudo-classes and media queries. Its styles apply only to that one document.

2. Why should the style element go in the head?

Style sheets in the head are render-blocking, so the browser has all the styles before it first paints the page. A style block placed later in the body can cause content above it to appear unstyled and then change. Keeping it in the head also makes the styles easy to find.

3. What does the media attribute on a style element do?

It takes a media query, and the styles apply only while that query matches. For example, media="print" applies the styles only when printing. If the attribute is missing, the default is all.

4. If two style elements set the same property with the same selector, which wins?

The one that appears later in the document wins, because at equal specificity the cascade uses source order. The same rule applies between <style> elements and linked style sheets.

5. When would you choose internal CSS over an external style sheet?

For a single-page project, a demo or a prototype, where keeping everything in one file is convenient. It is also used for small page-specific rules and for inlining critical CSS. For multi-page sites, an external file is better because it is shared and cached.

Key points

  • Internal CSS is written in a <style> element and holds full rules, so it can use any selector.
  • Put <style> in the <head> so the styles are ready before the page is drawn.
  • Several <style> and <link> elements combine in document order; later wins at equal specificity.
  • The media attribute applies a block only when its media query matches; the default is all.
  • The type="text/css" attribute is not needed in new code.
  • Use internal CSS for single pages, demos and critical CSS; use external files for multi-page sites.

Exercise

Farhan is making a one-page timetable for his coaching class in Rohtak. Build a page with a heading and a list of four classes with their times. In a <style> element in the head, style the heading, give each class line a class called slot with a bottom border, and add a hover background. Then add a second <style> element with media="print" that removes the hover background colour and makes the text black, so the printed copy is clean. Explain in one sentence why the second block must come after the first.

Show solution
HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Coaching timetable</title>
  <style>
    body { font-family: system-ui, sans-serif; padding: 1rem; color: #12355b; }
    h1 { color: #8a3324; }
    .slot { padding: 0.4rem 0; border-bottom: 1px solid #c9d6ea; }
    .slot:hover { background: #eef4ff; }
  </style>
  <style media="print">
    body, h1 { color: black; }
    .slot:hover { background: none; }
  </style>
</head>
<body>
  <h1>Farhan Coaching Centre, Rohtak</h1>
  <p class="slot">Maths: 7:00 am</p>
  <p class="slot">Physics: 8:30 am</p>
  <p class="slot">Chemistry: 4:00 pm</p>
  <p class="slot">English: 5:30 pm</p>
</body>
</html>

The print block uses the same selectors as the first block, so it has the same specificity and wins only because it comes later in the document.

Quick Quiz

1. What can a <style> element contain that a style attribute cannot?

2. What is the default value of the media attribute on <style>?

3. Two <style> elements both contain .note { color: ... }. Which colour is used?

4. How do you load a CSS file into a page?

Learn CSS with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call