Jump to main content
Menu

CSS Implementation Options

Embedded CSS

  • There are multiple ways of associating CSS with an HTML document.
  • The easiest one to start with when learning CSS is the embedded approach.
  • Embedded CSS is placed entirely within the <head></head> of the HTML document.
  • The CSS is surrounded by <style></style> tags.

Embedded CSS Example

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <title>Embedded CSS</title>
  <meta charset="utf-8" />
  <style>
  body {font-size: 1rem;}
  h1 {font-size: 1.5rem;}
  </style>
</head>
<body>

<h1>A Sample Level 1 Heading, Sized at 1.5rem</h1>

<p>Some content, sized at 1rem because it is using the 'body' settings.</p>

</body>
</html>

Inline CSS

  • Inline CSS closely intermingles structure and presentation because the CSS is located inside the HTML tag using the style attribute.
  • Due to this very close integration of structure and presentation, inline CSS should be used only in rare cases, such as for one-time style changes that occur on only one page in a website.
  • Drawbacks of inline CSS:
    • Time-consuming to update/maintain
    • Impossible to reuse and cache
    • Adds to HTML file sizes due to all the repetition
  • Inline styles will override other CSS instructions if there are conflicting instructions. Learn more about the CSS Cascade, which is why inline overrides other styles.
Attribute Usage and Effect Values
style Used to specify CSS for an element.

Can be applied to any tag valid inside <body>, including <body> itself.
Any CSS property: value combination

Inline CSS Example

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <title>Inline CSS</title>
  <meta charset="utf-8" />
</head>
<body>

<h1 style="color: green; font-size: 1.5rem;">
This level 1 heading is styled</h1>

<h1>This level 1 heading uses browser defaults because the inline styles applied
to the other heading are restricted to that one tag.</h1>

</body>
</html>

External CSS using @import

  • @import is specified one of two ways:
    • Within <style></style> tags as the first line of CSS code
    • As the first content in a <link /> stylesheet
  • If not listed first in both cases, @import will fail to work.
  • Multiple stylesheets can be brought in using separate @import directives (all directives must precede other CSS rules).
  • Historically, the most common usage of @import was to bring in a stylesheet containing rules that ancient browsers should not download, because they did not recognize the @import directive.
  • Because it involves rules in an external document, @import shares all the other advantages of the <link /> approach (such as caching and separating structure from presentation).

@import Example

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
  <title>@import CSS</title>
  <meta charset="utf-8" />
  <style>
  @import url(link.css);
  </style>
</head>
<body>

<h1>This level 1 heading is styled by an external CSS file</h1>

<h2>This level 2 heading is also styled by an external CSS file</h2>

</body>
</html>

The link.css file contains:

h1 {font-size: 1.5rem; color: green;}
h2 {font-size: 1.25rem;}