Here’s a solution in pure JavaScript that satisfies your requirements:
const mainElement = document.createElement('main');
Object.assign(mainElement.style, {
position: 'fixed',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: '25vw',
height: '25vh',
backgroundColor: 'rgba(255, 255, 255, 0.9)',
boxShadow: '0 4px 8px rgba(0, 0, 0, 0.2)',
overflowY: 'auto',
zIndex: '1000',
borderRadius: '8px',
padding: '10px',
boxSizing: 'border-box'
});
document.body.appendChild(mainElement);
function appendContent() {
const newElement = document.createElement('p');
newElement.textContent = `Dynamic content added at ${new Date().toLocaleTimeString()}`;
mainElement.appendChild(newElement);
}
for (let i = 0; i < 5; i++) {
appendContent();
}
setInterval(appendContent, 2000);
Explanation:
CSS Styling: The <main>
element is styled using position: fixed
, transform
, and top
/left
values to center it and make it stay visible over other elements.
Dynamic Content: The appendContent
function demonstrates how elements can be added dynamically into the <main>
element.
Scrollability: The overflowY: auto
ensures the <main>
element becomes scrollable when content overflows.
Feel free to tweak the styles or behavior as needed to fit your project! Let me know if you'd like further modifications or explanations.