FE Interview Hub
HTMLIntermediate

How to include CSS in HTML? What is the priority order?

AI Practice

Three Ways to Include CSS in HTML

1. External Stylesheet

<head>
  <link rel="stylesheet" href="style.css">
</head>
  • The most recommended approach
  • CSS is separated from HTML, making it easy to maintain and cache
  • Multiple pages can share the same CSS file

2. Internal (Embedded) Style

<head>
  <style>
    p { color: red; }
  </style>
</head>
  • Suitable for single-page or page-specific styles
  • Cannot be shared across pages; not cache-friendly

3. Inline Style

<p style="color: red; font-size: 16px;">Text</p>
  • Written directly in the element's style attribute
  • Has the highest priority, but is hard to maintain — generally avoid overusing it

CSS Priority (Specificity)

When multiple rules apply to the same element, priority from highest to lowest:

  1. !important (highest priority — use with caution)
  2. Inline styles
  3. ID selectors (#id)
  4. Class / attribute / pseudo-class selectors (.class, [attr], :hover)
  5. Element / pseudo-element selectors (p, ::before)
  6. Inherited / browser default styles (lowest)

When specificity is equal, the later-defined rule wins (last-write wins).

✦ AI Mock Interview

Type your answer and get instant AI feedback

Sign in to use AI scoring