Utilizing <section> elements for CSS grid layout child HTML tags to enhance WCAG accessibility

Is it feasible to use CSS Grid for structuring a webpage in a way that allows semantic grouping of tags like <section> without disrupting the grid layout, especially when the grouped items are supposed to be direct children of the grid container?

I aim to create this semantic grouping to ensure compliance with WCAG guidelines by organizing content into meaningful units (https://www.w3.org/WAI/GL/wiki/Using_HTML5_section_element). However, I am struggling to figure out how to group the header within a <section> while preserving my layout, which includes a grid cell dedicated to the logo.

HTML snippet

<!DOCTYPE html>
<html>
  <head>
    <title>test</title>
    <link rel="stylesheet" type="text/css" href="styles.css" />
  </head>
  <body>

    <!-- Starting Wrapper -->
    <!-- I attempted replacing a <main> tag instead of a div here, but it resulted in 
         displaying only the logo at the top left corner of the page with nothing else -->       
    <div id="wrapper">

      <!-- Header Section: intended semantic grouping #1 -->
      <div id="logo" ><img src="../assets/images/some_image.png"/></div>
      <div id="header"><h1>header h1</h1></div>

      <!-- Content Section: desired semantic grouping #2 -->
      <div id="left_aside">
        <nav>
          <ul>
            <li><a href="#">link 1</a></li>
            <li><a href="#">link 2</a></li>
            <li><a href="#">link 3</a></li>
            <li><a href="#">link 4</a></li>
            <li><a href="#">link 5</a></li>
          </ul>
        </nav>
      </div>
      <div id="right_content">test right content</div>

      <!-- Footer Section: planned semantic grouping #3 -->
      <footer>test footer</footer>

    </div>
    <!-- Ending Wrapper -->

  </body>

CSS styling

/* entire */
body {
    margin  : 0;
    height  : 100vh;
    overflow: hidden;
}
#wrapper {
    height               : 100vh;
    width                : 100vw;
    display              : grid;
    grid-template-columns: 1fr 8fr;
    grid-template-rows   : 2fr 17fr 1fr;
    grid-gap             : 2px 2px;
}

/* header */
#logo {
    grid-column    : 1/2;
    grid-row       : 1/2;
    display        : flex;
    justify-content: center;
    align-items    : center;
    padding        : 1em;
}
img {
    max-width  : 150px;
    max-height : 90px;
}
#header {
    grid-column    : 2/4;
    grid-row       : 1/2;
    display        : flex;
    justify-content: center;
    align-items    : center;
}
h1 {
    text-align    : center;
}

/* content */
#left_aside { 
    background : yellow;
    grid-column: 1/2;
    grid-row   : 2/3;
}
#right_content { 
    background : black;
    color      : white;
    grid-column: 2/4;
    grid-row   : 2/3;
}

/* footer */
footer { 
    background : orange;
    grid-column: 1/4;
    grid-row   : 3/4;
}

The advice against favoring grid over semantic grouping is valid, as removing semantic elements to make the layout work can lead to issues. It's crucial to maintain a well-structured document to avoid such problems.

Resources and examples have not provided a clear solution to my current dilemma.

Answer №1

Is setting the additional grouping elements to display:contents the solution you are looking for? Here is an example:

/* entire */
body {
    margin  : 0;
    height  : 100vh;
    overflow: hidden;
}
#wrapper {
    height               : 100vh;
    width                : 100vw;
    display              : grid;
    grid-template-columns: 1fr 8fr;
    grid-template-rows   : 2fr 17fr 1fr;
    grid-gap             : 2px 2px;
}

section {
  display:contents;
}

/* header */
#logo {
    grid-column    : 1/2;
    grid-row       : 1/2;
    display        : flex;
    justify-content: center;
    align-items    : center;
    padding        : 1em;
}
img {
    max-width  : 150px;
    max-height : 90px;
}
#header {
    grid-column    : 2/4;
    grid-row       : 1/2;
    display        : flex;
    justify-content: center;
    align-items    : center;
}
h1 {
    text-align    : center;
}

/* content */
#left_aside { 
    background : yellow;
    grid-column: 1/2;
    grid-row   : 2/3;
}
#right_content { 
    background : black;
    color      : white;
    grid-column: 2/4;
    grid-row   : 2/3;
}

/* footer */
footer { 
    background : orange;
    grid-column: 1/4;
    grid-row   : 3/4;
}
    <!-- Begin Wrapper -->
    <!-- I tried to put a <main> tag instead of a div here, but as a result I
         got a blank page showing nothing else but the logo top left 
         of the page -->       
    <div id="wrapper">

     
      <!-- header, semantic grouping #1 that I would like to make -->
      <section>
        <div id="logo" ><img src="../assets/images/some_image.png"/></div>
        <div id="header"><h1>header h1</h1></div>
      </section>

      <!-- content, semantic grouping #2 that I would like to make -->
      <section>
        <div id="left_aside">
          <nav>
            <ul>
              <li><a href="#">link 1</a></li>
              <li><a href="#">link 2</a></li>
              <li><a href="#">link 3</a></li>
              <li><a href="#">link 4</a></li>
              <li><a href="#">link 5</a></li>
            </ul>
          </nav>
        </div>
        <div id="right_content">test right content</div>
      </section>
      <!-- footer, semantic grouping #3 that I would like to make -->
      <footer>test footer</footer>

    </div>
    <!-- End Wrapper -->

Answer №2

Expanding on the points made by @Alochi and the ongoing conversation in the comments, I recommend revising the HTML tags for better semantic structure. By utilizing markup tags that reflect the intended content, you can utilize CSS display: contents to selectively hide semantic containers and achieve your desired layout.

      <!-- showcasing semantic grouping #2 using topical HTML entities -->
      <!-- employ distinctive tags like aside, nav, section, article for clarity -->
      <article>
        <aside id="left_aside">
          <nav>
            <ul>
              <li><a href="#">link 1</a></li>
              <li><a href="#">link 2</a></li>
              <li><a href="#">link 3</a></li>
              <li><a href="#">link 4</a></li>
              <li><a href="#">link 5</a></li>
            </ul>
          </nav>
        </aside>
        <section id="right_content">test right content</section>
      </article>
    </div>
    <!-- End Wrapper -->

For a comprehensive understanding of "semantic" HTML5 tags, refer to this informative guide. Additionally, Rachel Andrew provides a clear explanation of the functionality of display: contents in this insightful article.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Switching the image backdrop with an assortment of pictures

I am working with a structure that looks like this: <div class="container"> <ul> <li> <img> </li> </ul> </div> Currently, there is a background image set for the main container. My go ...

Getting the outer span text with Selenium WebDriver and Java: A quick guide

Having an issue where I'm having difficulty extracting the text from a span tag that contains another nested span tag: <span class="abc"> <span class="def">Inner Span</span> Outer Span </span> Currently, when ...

Searching for a method to locate and substitute a specific HTML string using the `sed` command in the macOS Catalina terminal

I am attempting to modify the following html code: <svg xmlns="http://www.w3.org/2000/svg" width="20" viewBox="0 0 256 256"> to this updated version: <svg xmlns="http://www.w3.org/2000/svg" width="30&q ...

What is the best way to showcase a div on top of all other elements in an HTML page?

As a newcomer to html and css, I have successfully created a div that contains city names. The issue I am currently facing is when I click on a button to display the div, it gets hidden behind the next section of my page. Take a look at the image below for ...

Implementing CSS counter-increment with jQuery

Is there a way to use jQuery to set the CSS counter-increment attribute on ".demo:before" even though jQuery cannot access pseudo elements directly? I recall seeing a suggestion on Stack Overflow about using a data attribute and then referencing that value ...

Is there a way to adjust the position of ::before elements without affecting the border placement?

I'm struggling with an element that has a CSS style ::before to display an arrow-up inside a black circle. Check out the code here The issue I'm facing is that the character \25b2 isn't centered vertically within the circle. Adjustin ...

Executing functions and setting element values upon loading in Javascript

I am currently working on updating a small Javascript function that assigns an active class to the parent <div> of a group of radio buttons when the page loads and also when there's a change event. The existing function looks like this: functi ...

The Bootstrap 4 grid system does not automatically use floats

While using bootstrap 4 to develop a website, I encountered an issue where my columns were not floating automatically as expected. I'm not sure if this is a Bootstrap 4 problem or if I am missing something. I know custom CSS can solve the issue, but I ...

Tips for rotating a responsive table on a mobile device's screen

I am currently managing a blogger website that features an HTML table on the homepage. I am struggling with making this table responsive for mobile devices, and I would like it to look similar to the image provided below. Can someone guide me on how to ach ...

Issues with using a personalized font in a Stenciljs project

Looking for guidance on implementing a custom font in my Stenciljs app. I have the otf file, unsure if an npm package is necessary. Here's my code: filestructure: -src --components --assets ---Anurti-Regular.tiff ---Anurti-Regular.ttf friends-l ...

What could be causing the issue with my query that is preventing it from properly displaying information in

Even though I can successfully connect to my database, it keeps pointing out an issue with my query. However, I'm certain that the query is correct as I tested it in my phpMyAdmin console and got the desired results. It's been a while since I wor ...

Styling Angular2 Material Dialog: the perfect fit

The Angular2 material team recently unveiled the MDDialog module at https://github.com/angular/material2/blob/master/src/lib/dialog/README.md I am interested in customizing the appearance of Angular2 material's dialog. Specifically, I want to adjust ...

PHP infinite redirection loop

I have encountered an issue where a user can successfully submit an event to a calendar, but I am facing difficulty redirecting them back to the original page. I attempted to use the following code snippet: header("Location: http://localhost/Project/Vie ...

What is the process of importing a cascading style sheet into a C# object?

Currently, I am working on a web application and I would like to provide users with the flexibility to change the styling elements of CSS linked to a page through an admin screen. I have been exploring ways to load a CSS file into a C# object or convert ...

How can I make a div extend beyond the boundaries of a bootstrap card?

Working with bootstrap version 4 and devexpress. The dropdown section you see is a creation by devexpress. https://i.sstatic.net/8vdj6.png ...

"Struggling with a basic Javascript function to refresh parts of a web page by calling a .php

After spending several hours browsing the internet, I am still struggling to get this straightforward example to function properly. Could someone please lend me a hand? My goal is to use JavaScript to display the contents of a PHP file. The display should ...

As a useful tool in XML processing, xsl:value-of function effectively filters out any

When creating XML that contains HTML tags, the challenge arises when trying to convert all the XML data to HTML using XSLT. The issue lies in handling HTML tags included within the XML. Specifically, I need to retrieve the content within the summary tag wi ...

The A-frame inspector tool is not very useful within the Glitch platform

Just want to preface this by saying that I am completely new to both a-frame and Stack Overflow. Lately, I've been experimenting with A-frame templates similar to the one linked here: When I pull up the inspector for the example above (ctrl-alt-i), ...

Techniques for determining if a file is empty using jQuery

Just starting out with JS. I want to validate the file input element before form submission using jQuery/JavaScript. I've researched various solutions, but nothing seems to be working for me. I'm trying to avoid displaying "/c/fakepath" as the ...

Having trouble with adding a class on scroll?

My challenge is to extract the header from this website, by adding an additional class when the user scrolls at a position greater than 0. I thought it would be easy, but Java always presents problems for me. Here’s the code I came up with: <!DOCTY ...