Relationship between semantic tags and CSS styling

Semantic tags and CSS styling have a close relationship in web development. Semantic tags provide a meaningful structure and context to the content, while CSS styling controls the visual presentation of that content. Let’s explore this relationship with an example and reference link:

Example: Consider a simple HTML structure using semantic tags:

<body>
  <header>
    <h1>Website Title</h1>
    <nav>
      <ul>
        <li><a href="#">Home</a></li>
        <li><a href="#">About</a></li>
        <li><a href="#">Contact</a></li>
      </ul>
    </nav>
  </header>

  <main>
    <section>
      <h2>About</h2>
      <p>This is the about section of the website.</p>
    </section>
    <section>
      <h2>Contact</h2>
      <p>This is the contact section of the website.</p>
    </section>
  </main>

  <footer>
    <p>&copy; 2023 Website Name. All rights reserved.</p>
  </footer>
</body>

In this example, semantic tags like <header>, <nav>, <main>, <section>, and <footer> are used to structure the content. The heading tags <h1> and <h2> provide hierarchy and emphasize important headings. The <nav> tag represents the navigation menu.

Now, let’s see how CSS styling can be applied to these semantic tags:

/* Example CSS styling */
body {
  font-family: Arial, sans-serif;
}

header {
  background-color: #f2f2f2;
  padding: 20px;
}

h1 {
  font-size: 24px;
  color: #333;
}

nav ul {
  list-style: none;
  padding: 0;
  margin: 0;
}

nav li {
  display: inline-block;
  margin-right: 10px;
}

nav a {
  text-decoration: none;
  color: #333;
}

main {
  padding: 20px;
}

section {
  margin-bottom: 20px;
}

h2 {
  font-size: 20px;
  color: #666;
}

footer {
  background-color: #f2f2f2;
  padding: 10px;
  text-align: center;
  font-size: 12px;
  color: #666;
}

In this CSS example, we apply styles to the semantic tags using selectors. For instance, we target the <header> tag and apply background color and padding styles. We target heading tags like <h1> and <h2> to control their font sizes and colors. We also apply styles to the <nav>, <ul>, <li>, and <a> tags to modify the appearance of the navigation menu.

The relationship between semantic tags and CSS styling is that semantic tags provide structure and meaning to the content, while CSS styling is responsible for visually presenting that content. By using appropriate semantic tags and applying CSS styling, we can create well-structured and visually appealing web pages.

Reference link:

Leave a Reply

Your email address will not be published. Required fields are marked *