Connect a stylesheet
Definition and purpose
An external stylesheet is a separate CSS file linked from HTML. Internal CSS lives in a style element, while inline CSS is a declaration list in an element’s style attribute. External files make reuse across pages easier; inline styling is local to one element.
Internal and inline styles
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <style>h1 { color: navy; }</style> </head> <body> <h1 id="a">Studio</h1> <p id="b" style="color: green;">Open today</p> </body></html>The internal rule styles the heading; the inline declaration styles just the paragraph.
Syntax and mental model
Put <link rel="stylesheet" href="styles.css"> inside the HTML head when both files are in the same folder. In styles.css write CSS only, without style tags. An internal style element can contain several rules; an inline style attribute contains declarations, not a selector or braces. Relative href paths resolve against the document base URL. Check spelling, directory and DevTools Network if a stylesheet fails to load.
External stylesheet pair
<!doctype html><html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>CSS practice</title> <link rel="stylesheet" href="styles.css"> </head> <body> <h1 id="a">Workshop</h1> <p id="b">Schedule</p> </body></html>Save the HTML and CSS as index.html and styles.css in the same directory. The link loads the two rules.
Common mistakes
Do not put a style tag in a .css file or wrap inline declarations in braces. A missing file is a loading problem, not a reason to change every selector. The supplied external pair should be opened together from the same directory.
Practice and next steps
After trying the examples, complete the linked coding exercise and review questions. Predict the result before running your code. Prerequisite: What CSS does. Next lesson: Rules and declarations.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Connect a stylesheet — knowledge check
Five questions on connect a stylesheet.
Where should a reusable external rule be written?
Which link references a sibling file named styles.css?
What belongs inside an inline style attribute?
What should you check first if the CSS file never loads?
Which method allows a single file to style multiple pages?