overlaying a div on top of an HTML5 canvas

I'm currently attempting to position a div element on top of a canvas on the center of my webpage in order to create a start screen for a game. This start screen will display options like "Play game" and "Settings". Despite conducting thorough research online and reviewing various examples, I have been unable to achieve the desired result. Here is the code I am working with:

<div id="gamecontainer">
            <canvas id="myCanvas" width="800" height="600">
                Sorry, <strong>CANVAS</strong> is not supported by your browser. Get a more recent one to play my game!
            </canvas>

            <div id="gamestartscreen" class="gamelayer">
                <img src="images/icons/play.png" alt="Play Game" onclick="alert('ceve');"><br>
                <img src="images/icons/settings.png" alt="Settings">
            </div>
</div>

Here is the associated CSS:

#gamecontainer {
    width: 800px;
    height: 600px;
    background-color: beige;
    border: 1px solid black;
    position: relative;
    margin: 0 auto;
}

.gamelayer {
    width: 800px;
    height: 600px;
    position: absolute;
    display: none;
    z-index: 0;
}


/* Screen for the main menu (Like Play, Settings, etc.) */
#gamestartscreen {
    position: relative;
    margin: 0 auto;
}

#gamestartscreen img {
    margin: 10px;
    cursor: pointer;
}

I would greatly appreciate any guidance on what may be incorrect in my implementation.

Answer №1

The issue arose from the differing positioning and z-index values between the canvas layer and the gamestart div. To resolve this, you simply need to adjust the canvas element's position to absolute or relative, while ensuring that the startstreen div has a z-index greater than 1. This JSFiddle example should provide clarity on the matter. Cheers!

#gamecontainer {
  width: 800px;
  height: 600px;
  background-color: beige;
  border: 1px solid black;
  position: relative;
  margin: 0 auto;
}

canvas {
  position: absolute;
  z-index: 1;
}

#gamestartscreen {
  position: absolute;
  margin: 0 auto;
  z-index: 2;
  background: red;
}
<div id="gamecontainer">
  <canvas id="myCanvas" width="800" height="600">
    Sorry, <strong>CANVAS</strong> is not supported by your browser. Get a more recent one to play my game!
  </canvas>

  <div id="gamestartscreen" class="gamelayer">
    Hello world!
  </div>
</div>

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

Steer clear of using HTML tags in search keywords for indexing

When it comes to indexing keywords in an HTML document, I want to exclude the HTML tags from being indexed. Take this for instance: <div> <!-- this is html code --> <span>You are welcome</span> <!-- simple message for searchi ...

You can generate a TBODY element by utilizing an Array

I have an array with the following structure. https://i.sstatic.net/5AdO9.png below is the code I am using success: function(data) { data = $.parseJSON(data); for (i = 0; i < data.length; i++) { con ...

The PHP mail() function seems to be having trouble delivering emails to Hotmail users, although it works fine with all

Utilizing the php mail() function on my dedicated server via a php script, I am able to send emails to all recipients except for those using Hotmail accounts. Below is the code snippet for the mail() function: $hyperlink = 'http://test.guru99.com/&ap ...

Troubleshooting Problem with Web Forms and Bootstrap Form Design

Currently, I am utilizing a simplified version of the Admin LTE Bootstrap Theme in an ASP.NET Web Forms Project. In this project type, only one form tag can be used per page. The form tag must encapsulate both the Top (Nav) and Bottom (Side Bar Left & Mai ...

Scroll horizontally in a div with a minimum width while it is contained within a column using Bootstrap

I am seeking to create a bootstrap v4 grid with 3 columns, where certain content within the columns require a minimum width. This necessitates a horizontal scroll (overflow-x), but I am encountering some overlap where the scrollbar and content extend beyon ...

"Discover the art of arranging div elements into a grid format with multiple rows and two columns

<div class="parent"> <div class="col">1</div> <div class="col">2</div> <div class="col">3</div> <div class="col">4</div> <div class="col">5</div> <div class="col">6</div ...

starter tablets

Currently, I am using Bootstrap pills for my navigation bar. However, I am experiencing an issue where the active pill is not displaying with a blue background color as it should according to the default Bootstrap styling. Here is my code: <div style=" ...

Clear backdrop with solid objects

I am trying to achieve a unique design where the body has an image background, and the main div has a semitransparent background. I want the image to be visible through the main div, but in a shaded manner. However, the traditional approach may cause every ...

When you hover over the image, a thin white line materializes along the bottom edge

Can anyone help me troubleshoot an issue I am facing with a white pixel line that appears at the bottom of every image within the "div class gallery" element? The white line seems to only show up when the mouse hovers over the images, especially since I ha ...

What could be causing the variation in appearance between my website on PC and Android compared to iOS when viewing a ReactJS page?

My reactjs web page is fully responsive and displays consistently on Google Chrome for PC and Android. However, I have noticed that there are some differences when viewing it on Google Chrome for iOS. In particular, the loading animation and green line gen ...

I am utilizing Python Django to display a message from a Python file in HTML. While the current code is functional, I would like the message to be showcased as a pop-up or alert notification

Using Python Django. The message is coming from a Python file and I would like to display it in HTML. The current code is functioning, but I would like the message to appear as a pop-up or alert. Register {% if messages %} {% for result in messages %} < ...

Submitting POST data from a form to the current page

I am facing a rather complex issue. Currently, on a page, I have a form that collects information from my visitors. With the help of a script, I can grab this information and add it to a specific link. Initially, I used redirect.php to attach the data to ...

How come Vue.js is not showing the image I uploaded?

Even though I can successfully print the image URL, I'm facing an issue where the img tag is not displaying it, despite my belief that I've bound it correctly. <html> <head> <title>VueJS Split Demo</title> <script t ...

Extending the length of the list items and their contents

Take a look at this website. I'm struggling with expanding the submenu list items in IE7. It seems that when you hover over one of the LIs under Restaurants, the green does not fill the entire line. I attempted using {width: 100%}, but it did not sol ...

Delete a particular division within a row based on its unique identifier

I am currently working with Django and have integrated multiple Bootstrap cards into my page structure as shown below. I am attempting to delete a specific div element when clicking the 'X' button within an anchor tag. <row> <col-sm-3 ...

Comparing Yii's CHtml::link() function with the use of regular <a href=''></a> HTML tags

When using elements like yii CHtml::link, what are the recommended practices? I'm working on a button that needs to include an icon, text, respond to hover events, and be wide. Are there any benefits to using CHtml instead of a regular tag that can e ...

Guide on Creating HTML Embeds for Multiple Selection Code Blocks

I need some help creating a feature similar to this: View Image Unfortunately, I haven't been able to locate any tutorials or code samples on how to implement multi-selectable language code blocks using HTML/CSS. My CSS knowledge is limited, and I r ...

Utilize jQuery to encase the final word of a paragraph within span tags

Greetings! Consider the following HTML snippet: <p>Phasellus sit amet auctor velit, ac egestas augue.</p> I am interested in enclosing the last word within span tags to achieve the following result: <p>Phasellus sit amet auctor velit, ...

javascript box is hidden to prevent displaying of values

I need the negative character to disappear when the user clicks the minus (-) button. The count should stop at 0. Currently, it is displaying characters like -1, -2, -3. I only want to show 0 or numbers greater than 0. <script type="text/javascript" ...

How do I ensure that the active item in the sidebar remains in view as I scroll?

After trying various methods found on Stack Overflow and jQuery examples, I wasn't able to successfully implement it in my grav theme. Every time I click on an item in my sidebar, the page refreshes and the sidebar scroll position resets to the top. W ...