Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Linking CSS to HTML
📖 Scenario: You are creating a simple webpage for a local bakery. You want to make the page look nice by adding colors and fonts using CSS.
🎯 Goal: Build a basic HTML page and link an external CSS file to style the page with background color and font changes.
📋 What You'll Learn
Create an HTML file with a basic structure including <!DOCTYPE html>, <html>, <head>, and <body> tags.
Add a <link> tag inside the <head> to connect an external CSS file named styles.css.
Create a CSS file named styles.css that changes the background color of the page and the font family of the text.
Ensure the CSS file is correctly linked so the styles appear when the HTML file is opened in a browser.
💡 Why This Matters
🌍 Real World
Linking CSS to HTML is a fundamental skill for creating visually appealing websites. It separates content (HTML) from design (CSS), making websites easier to maintain and update.
💼 Career
Web developers and designers must know how to link CSS files to HTML to build professional and maintainable websites.
Progress0 / 4 steps
1
Create the basic HTML structure
Write the basic HTML skeleton with <!DOCTYPE html>, <html lang="en">, <head>, and <body> tags. Inside the <body>, add a <h1> heading with the text Welcome to the Bakery.
CSS
Hint
Start by typing the basic HTML tags. Don't forget the lang attribute in the <html> tag and the <h1> heading inside the body.
2
Add the link to the external CSS file
Inside the <head> section of your HTML, add a <link> tag that links to the CSS file named styles.css. Use the attributes rel="stylesheet" and href="styles.css".
CSS
Hint
Use a <link> tag with rel="stylesheet" and href="styles.css" inside the <head> section.
3
Create the CSS file with styles
Create a CSS file named styles.css. Inside it, write CSS rules to set the body background color to #f8e8d4 and the font family to Arial, sans-serif.
CSS
Hint
Write CSS rules for the body selector. Use background-color and font-family properties.
4
Verify the CSS is linked and styles apply
Open the HTML file in a browser to check that the background color is light beige and the text uses the Arial font. If needed, add a <meta charset="UTF-8"> tag inside the <head> to ensure proper character display.
CSS
Hint
Make sure the <meta charset="UTF-8"> tag is present in the <head> and the CSS file is linked correctly.
Practice
(1/5)
1. What is the correct HTML tag to link an external CSS file to an HTML document?
easy
A. <script>
B. <style>
C. <link>
D. <css>
Solution
Step 1: Identify the tag used for linking external resources
The <link> tag is used in HTML to connect external files like CSS.
Step 2: Confirm the tag's purpose for CSS
<style> is for internal CSS, <script> is for JavaScript, and <css> is not a valid tag.
Final Answer:
<link> -> Option C
Quick Check:
Link CSS with <link> tag [OK]
Hint: Use <link> tag inside <head> to connect CSS [OK]
Common Mistakes:
Using <style> tag for external CSS
Using <script> tag instead of <link>
Trying to use a non-existent <css> tag
2. Which of the following is the correct syntax to link a CSS file named "styles.css" in your HTML document?
easy
A.
B.
C.
D.
Solution
Step 1: Check the correct attributes for linking CSS
The <link> tag needs rel="stylesheet" and href="filename.css" to link CSS properly.
Step 2: Eliminate incorrect attribute usage
rel="script" is wrong, <style> does not use src, and <script> is for JavaScript files.
Final Answer:
<link rel="stylesheet" href="styles.css"> -> Option A
Quick Check:
Use rel="stylesheet" and href for CSS link [OK]
Hint: Use rel="stylesheet" and href="file.css" in <link> [OK]
Common Mistakes:
Swapping rel and href attribute values
Using <style> tag with src attribute
Using <script> tag for CSS files
3. Given this HTML snippet, what will be the color of the paragraph text?
Assuming the file "main.css" exists in the same folder.
medium
A. The <link> tag is correctly placed and written.
B. The <link> tag should be inside the <body> tag.
C. The href attribute value is incorrect.
D. The <link> tag is missing the 'rel' attribute.
Solution
Step 1: Check the <link> tag attributes
The <link> tag has rel="stylesheet" and href="main.css", which is correct.
Step 2: Verify placement of the <link> tag
The <link> tag is correctly placed inside the <head> section, which is standard practice.
Final Answer:
The <link> tag is correctly placed and written. -> Option A
Quick Check:
Correct <link> tag with rel and href in <head> [OK]
Hint: Place <link> in <head> with rel and correct href [OK]
Common Mistakes:
Putting <link> inside <body> instead of <head>
Forgetting rel="stylesheet" attribute
Incorrect file path in href
5. You want to link two CSS files, "base.css" and "theme.css", to your HTML page. "theme.css" should override some styles in "base.css". Which is the correct way to link them?
hard
A.
B.
C.
D.
Solution
Step 1: Understand CSS loading order
CSS files loaded later override earlier ones if selectors match.
Step 2: Place base.css first, then theme.css
Link base.css first, then theme.css so theme.css can override base styles.
Step 3: Check options for correct syntax and order
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="theme.css"> links base.css first, then theme.css correctly. <link rel="stylesheet" href="theme.css">
<link rel="stylesheet" href="base.css"> reverses order. <style>@import url('theme.css'); @import url('base.css');</style> uses @import inside <style> tag but loads theme.css first then base.css, so base overrides theme incorrectly. <link rel="stylesheet" href="base.css" theme="theme.css"> is invalid syntax.
Final Answer:
<link rel="stylesheet" href="base.css">
<link rel="stylesheet" href="theme.css"> -> Option D
Quick Check:
Load base.css before theme.css for overrides [OK]
Hint: Link base.css first, then theme.css to override [OK]