We use CSS to style web pages. Sometimes, styles come from different places. Knowing which style wins helps us control how the page looks.
Inline vs external precedence in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
CSS
/* External CSS example */ button { color: blue; } <!-- Inline CSS example --> <button style="color: red;">Click me</button>
External CSS is written in separate files or inside <style> tags.
Inline CSS is written directly inside an element's style attribute.
Examples
CSS
<button style="color: red;">Red Button</button>CSS
/* In external CSS file or <style> tag */ button { color: blue; }
color: red; wins over external color: blue;.CSS
<button style="color: red;">Red Button</button> /* External CSS */ button { color: blue; }
Sample Program
The first button uses the external CSS color blue. The second button uses inline CSS color red, which overrides the external style.
CSS
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Inline vs External CSS</title> <style> button { color: blue; font-size: 1.5rem; padding: 0.5rem 1rem; } </style> </head> <body> <button>External Blue Button</button> <button style="color: red;">Inline Red Button</button> </body> </html>
Important Notes
Inline styles have higher priority than external styles.
Use inline styles sparingly to keep your code clean and easy to maintain.
External styles are better for styling many elements consistently.
Summary
Inline CSS overrides external CSS for the same property on an element.
External CSS is good for consistent styling across many elements.
Use inline CSS only for quick, specific changes.
Practice
1. Which CSS style has the highest priority when applied to the same HTML element and property?
style="color: red;" vs external stylesheet setting color: blue;easy
Solution
Step 1: Understand CSS specificity rules
Inline CSS (style attribute) has higher specificity than external CSS selectors.Step 2: Compare inline and external styles on the same property
When both define the same property, inline CSS overrides external CSS.Final Answer:
Inline CSS inside the style attribute -> Option CQuick Check:
Inline CSS > External CSS [OK]
Hint: Inline styles override external styles for same property [OK]
Common Mistakes:
- Thinking external CSS always overrides inline
- Confusing order of CSS files with inline priority
- Assuming equal priority for inline and external
2. Which of the following is the correct syntax to add inline CSS to an HTML element?
easy
Solution
Step 1: Recall inline CSS syntax
Inline CSS uses thestyleattribute with CSS rules inside quotes.Step 2: Check each option for correct attribute and format
OnlyText usesstyle="color: blue;"correctly.Final Answer:
<div style="color: blue;">Text</div> -> Option AQuick Check:
Inline CSS uses style attribute [OK]
Hint: Use style="property: value;" for inline CSS [OK]
Common Mistakes:
- Using class or css attribute instead of style
- Missing quotes around CSS rules
- Using invalid attribute names
3. Given the HTML and CSS below, what color will the text inside the <p> tag be?
<style>
p { color: green; }
</style>
<p style="color: orange;">Hello</p>medium
Solution
Step 1: Identify CSS rules applied to <p>
External style setscolor: green;, inline style setscolor: orange;.Step 2: Apply CSS precedence rules
Inline style overrides external style for the same property.Final Answer:
Orange -> Option BQuick Check:
Inline color overrides external color [OK]
Hint: Inline style color beats external stylesheet color [OK]
Common Mistakes:
- Choosing external CSS color instead of inline
- Ignoring inline style precedence
- Assuming default color applies
4. You have this HTML and CSS:
The button background is still blue. What is the likely problem?
<style>
.button { background-color: blue; }
</style>
<button class="button" style="background-color: red;">Click</button>The button background is still blue. What is the likely problem?
medium
Solution
Step 1: Check inline style syntax
Inline style must be insidestyle="..."attribute correctly.Step 2: Understand CSS precedence
Inline styles override external styles unless syntax is wrong or missing.Final Answer:
The inline style syntax is incorrect -> Option AQuick Check:
Incorrect inline syntax means external CSS applies [OK]
Hint: Check inline style attribute syntax carefully [OK]
Common Mistakes:
- Assuming external CSS overrides inline
- Ignoring syntax errors in inline style
- Thinking browser blocks inline styles
5. You want all paragraphs to be blue except one special paragraph that should be red. You have an external CSS file:
Which is the best way to make only the special paragraph red without changing the external CSS file?
p { color: blue; }Which is the best way to make only the special paragraph red without changing the external CSS file?
hard
Solution
Step 1: Understand constraints
You cannot change the external CSS file, so options A and D are invalid.Step 2: Use inline CSS for specific override
Inline CSS overrides external CSS, so addingstyle="color: red;"works immediately.Final Answer:
Add style="color: red;" inline to the special paragraph -> Option DQuick Check:
Inline CSS overrides external without file changes [OK]
Hint: Use inline style for quick, single-element overrides [OK]
Common Mistakes:
- Trying to edit external CSS when not allowed
- Using JavaScript unnecessarily
- Adding classes without CSS rules
