你說:
Use pure JavaScript code that creates a <main> element at the center of the visible screen. The <main> element always stays over other elements and occupies 1/4 area of the visible screen.
ChatGPT 說:

Here's a pure JavaScript solution to create a <main> element that stays centered in the visible screen and occupies 1/4th of the area. The <main> element will always stay over other elements as required.

html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Centered Main Element</title> <style> body { margin: 0; padding: 0; height: 100vh; overflow: hidden; } </style> </head> <body> <script> // Create the <main> element const mainElement = document.createElement('main'); // Style the <main> element to stay centered and occupy 1/4 of the screen mainElement.style.position = 'fixed'; mainElement.style.top = '50%'; mainElement.style.left = '50%'; mainElement.style.transform = 'translate(-50%, -50%)'; mainElement.style.width = '50vw'; // 50% of the viewport width mainElement.style.height = '50vh'; // 50% of the viewport height mainElement.style.zIndex = '9999'; // Ensure it stays over other elements mainElement.style.backgroundColor = 'rgba(0, 0, 0, 0.5)'; // Just to make it visible // Append the <main> element to the body document.body.appendChild(mainElement); // Update the position of <main> element when window is resized window.addEventListener('resize', () => { mainElement.style.top = '50%'; mainElement.style.left = '50%'; mainElement.style.transform = 'translate(-50%, -50%)'; }); </script> </body> </html>

Key Points:

  1. The <main> element is positioned using position: fixed, which keeps it at the center of the screen at all times.
  2. The top: 50% and left: 50% positions place the element's top-left corner at the center of the viewport, and transform: translate(-50%, -50%) shifts it back to the center.
  3. The width and height are set to 50vw and 50vh respectively, making the element occupy 1/4 of the screen.
  4. z-index: 9999 ensures the <main> element is on top of other content.
  5. The resize event listener ensures the <main> element remains properly positioned even when the window is resized.

This will make the <main> element remain at the center, occupying a quarter of the visible screen and stay above other content.


ChatGPT 可能會發生錯誤。請查核重要資訊。