Compare inline, internal, and external stylesheets.
To style a page, connect your CSS to the HTML. There are three common ways:
<style> element in the HTML document.style attribute.Each method can make the heading green. The difference is where the CSS lives and how easily you can reuse it.
An external stylesheet is a separate resource, usually saved as a file with a .css extension. Several HTML pages can link to the same file, so changing that file can update their shared styles.
Save these two files in the same folder. Start with the heading rule in style.css, then find the link that loads it in index.html.
h1 {
color: green;
}Save both files, then open or reload index.html in a browser. The browser loads style.css through the link, matches the h1 selector to the heading, and applies color: green to its text.
The <link> element belongs inside the HTML document's <head>. Its attributes tell the browser what to load:
rel="stylesheet" identifies the linked resource as a stylesheet.href="style.css" gives its location.The path must match the file's location. If you move the CSS file into a folder named styles, change the link to:
<link rel="stylesheet" href="styles/style.css">A CSS file contains CSS directly. Do not surround its rules with HTML <style> tags.
An internal stylesheet places CSS inside a <style> element in the HTML document's <head>.
To try it, replace the stylesheet link in the example with this element:
<style>
h1 {
color: green;
}
</style>The heading stays green, but the rule now lives in the HTML file. The selector h1 still chooses which elements receive the style.
These rules apply to the document containing the <style> element; another HTML page does not automatically receive them.
An inline style puts CSS declarations directly inside an element's style attribute:
<h1 style="color: green;">Documentation hub</h1>Here, color: green; is a declaration: a property and its value. There is no selector or pair of braces because the attribute is already attached to the heading being styled.
You can include several declarations, separated by semicolons:
<h1 style="color: green; font-weight: bold;">Documentation hub</h1>These styles apply only to this heading. To style another heading this way, edit its style attribute too.
Choose a method based on how you will use the styles:
If you repeat the same styles across pages or elements, you must update each copy when the design changes.
Choose the method before reading each answer:
<head>.The same declaration can appear in all three places. Choose based on how widely you need to reuse it, not because the declaration itself changes.
Try it yourself
HTML + CSS
.css file connects to HTML through a stylesheet link and can serve multiple pages.<style> element holds rules inside one HTML document.style attribute holds declarations directly on an element, without a selector or braces.href must point to the stylesheet's actual location.