Free HTML tutorial

HTML and CSS: Connect Structure and Presentation

Learn how HTML and CSS work together, including inline styles, embedded stylesheets and external CSS files.

HTML and CSS have different jobs

HTML describes page structure and content. CSS controls presentation, including color, typography, spacing and layout. Keeping those responsibilities clear makes pages easier to update.

Three ways to add CSS

  • Inline styles: a style attribute applies declarations to one element. Use it mainly for small examples or special cases.
  • Embedded stylesheet: a <style> element in the document head adds rules to that page.
  • External stylesheet: a separate CSS file can be shared across pages and is usually the easiest approach to maintain for a site.
<!-- In the document head -->
<link rel="stylesheet" href="styles.css">

If your stylesheet is in a folder beside the HTML file, include that folder in the path. For example, an index.html file can load css/styles.css with href="css/styles.css". The path is resolved from the HTML document's location.

/* styles.css */
h1 {
  color: #174f80;
}

.welcome {
  padding: 1rem;
  background-color: #eef5fb;
}

Check that the stylesheet is connected

  1. Confirm the <link rel="stylesheet"> element is inside the document's <head>.
  2. Make the href path match the real folder and filename, including letter case.
  3. Save both files and refresh the page. If one rule works but another does not, check that the CSS selector matches the HTML element or class.

For a small test, add class="welcome" to a paragraph and check that the background color appears. Then continue with the CSS selectors lesson. See MDN's guide to getting started with CSS for more ways to apply styles.

Continue with the CSS tutorial to learn selectors, layout and responsive styling.

Examples