Creating a scrollable HTML5 div container with fixed divs positioned within it

I am attempting to develop an app container that mimics the functionality of Microsoft Excel. The container should scroll both horizontally and vertically, with fixed headers on the left and top that move along with the content. Here is a rough representation:

+---ScrollableContainer---------+
|+-a-++----b--------------------|-------------------+
||   ||                         |                   |
||   |+-------------------------|-------------------|
||   |+----Content--------------|-------------------+
||   ||                         |                   |
||   ||                         |                   |
+-------------------------------+                   |
 |   ||                                             | 
 |   ||                                             |
 +---++---------------------------------------------+

The ScrollableContainer serves as the main container, offering horizontal and vertical scrolling capabilities.

Within the ScrollableContainer are 3 divs: a, b, and Content. The challenge I am facing is ensuring that the div a remains fixed on the left side while scrolling up and down, and that the div b stays fixed on top while scrolling left and right with ScrollableContainer. The Content div should be free to scroll in any direction. Think of it like an "Agenda". The a div represents the "HOUR" pivot, the b div represents the "DAY" pivot, and the Content acts as the agenda, organized by hours and days. The pivots should move along with the scroll.

EDIT:

Here is a partially functional example: - AndyM

.rows {
    height:200px;
    width:400px;
    background-color:rgb(240,240,240);
    position:relative;
}
.rows > header {
    position:absolute;
    top:0;
    left:0;
    bottom:0;
    width:40px;
    background-color:blue;
}
.rows > article {
    position:absolute;
    top:0;
    left:40px;
    bottom:0;
    right:0;
    overflow-x:scroll;    
}
.cols {
    position:relative;
    height:100%;
}
.cols header {
    height:40px;
    position:absolute;
    top:0;
    left:0;
    background-color:green;
}
.cols article {
    position:absolute;
    top:40px;
    bottom:0;
    left:0;
    overflow-y:scroll;
}

row {
    display:block;
    height:40px;
    white-space:nowrap;
}
cell {
    display:inline-block;
    height:100%;
    width:70px;
}
<section class="rows">
    <header>
        <row>
            <cell>Text</cell>
        </row>
        <row>
            <cell>Text</cell>
        </row>
        <row>
            <cell>Text</cell>
        </row>
        <row>
            <cell>Text</cell>
        </row>
        <row>
            <cell>Text</cell>
        </row>
        <row>
            <cell>Text</cell>
        </row>
    </header>
    <article>
        <section class="cols">
            <header>
                <row>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                </row>
            </header>
            <article>
                <row>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                </row>
                <row>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                </row>
                <row>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                </row>
                <row>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                </row>
                <row>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                </row>
                <row>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                    <cell>Text</cell>
                </row>
            </article>
        </section>
    </article>
</section>

The issue with this setup is that the vertical scrollbar is not visible, and the .col header does not scroll properly. Nonetheless, this example should provide a glimpse of the desired outcome.

Answer №1

If you desire a scrolling effect like this, you have two options: utilizing an iframe or implementing customized scrolling using JavaScript. When using an iframe, you can simply fix the positions of divs 'a' and 'b', but there may be some complexities involved. On the other hand, with JavaScript, you can create a solution similar to the following:

var x=0, y=0;
ScrollableContainer.onwheel = function(e) {
  if(firefox) {
    x+=e.deltaX*16;
    y+=e.deltaY*16;
  } else {
    x+=e.deltaX;
    y+=e.deltaY;
  }
  a.style.marginTop = y + 'px';
  b.style.marginLeft = x + 'px';
  content.style.marginTop = y + 'px';
  content.style.marginLeft = y + 'px';
};

Although untested, in order for this code to function properly, divs 'a', 'b', and 'content' must each reside within their own container. However, you are free to arrange these containers to position your divs as needed.

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

Consistently directing to a single page on the Node Js web server

I'm in the process of creating a website with multiple HTML pages. Currently, I have code that successfully links to the login page, but unfortunately, the localstores page also directs to the login page. const express = require('express') ...

What is the process for applying cdkDropList to the tbody when using mat-table instead of a traditional HTML table?

I have been experimenting with the mat-table component in my Angular project, following a simple example from the documentation: <table mat-table [dataSource]="dataSource" class="mat-elevation-z8"> <!--- These columns can be ...

Placing a div with absolute positioning inside another div with absolute positioning raises the question of how it functions when the outer div is not set to position: relative

Trying to grasp the concept, I experimented with the following setup: <div id="Outer"> <div id="Inner"></div> </div> In this scenario, the outer div has a relative position and the inner div has an absolute position. This allo ...

Is there a child missing? If so, add a class

I need to add the class span.toggle if the parent element of li does not have a child ul element. click here to view on codepen Snippet of HTML: <html lang="en"> <head> <meta charset="UTF-8> <title>No Title</title>& ...

What is causing the input tag and button tag to appear on separate lines instead of together?

Here is my HTML and CSS code: <!DOCTYPE html> <html> <head> <title>assignment1</title> <meta charset="utf-8"> <meta name = "description" content="assignment"> <meta name = "keywords" content="php, assignm ...

What is the best way to convert a dynamic HTML table with input fields into an Excel spreadsheet?

I have developed a JavaScript function to convert an HTML table into an Excel sheet. However, I am facing an issue where some fields in the table are enclosed in input tags instead of td tags, causing them to not appear in the Excel sheet. function expo ...

Reply to changes in the window size *prior to* adjusting the layout

Take a look at the "pixel pipeline" concept illustrated with a vibrant diagram on this page. I am currently working on resizing an element (let's say, a span) dynamically when the browser window is resized. I have implemented this using window.onresi ...

Having trouble with AngularJS not rendering on your HTML page?

This question has probably been asked countless times, but I'm genuinely unsure about what I'm doing wrong. The solutions I've come across so far haven't fixed the issue for me. Just to provide some context, I'm currently followin ...

creating an interactive element that seamlessly integrates with a dynamic background image slideshow

Struggling to make this work correctly as a newbie in javascript. I need the background image to slide upon hover and stay active on its selected div when clicked. The html, css, and javascript I have currently work fine - when the user clicks on a div, a ...

Creating a visual feast: A guide to crafting a stunning image gallery

Is there a way to ensure all the pictures in a table are the same size, orientation, and inline? Currently, my images vary in size and rotation. The CSS I have applied is styling only the first image differently from the rest. Any help is appreciated! CSS ...

The toggling feature seems to be malfunctioning as the div section fails to display

I'm facing an issue with my Django project while working on a template. I want to toggle the visibility of a div element between hiding and showing, but the function I used isn't working for some reason. I borrowed the function from a different t ...

"Implement a feature that allows for infinite scrolling triggered by the height of

Looking for a unique solution to implement a load more or infinite scroll button that adjusts based on the height of a div. Imagine having a div with a height of 500px and content inside totaling 1000px. How can we display only the initial 500px of the div ...

<p> The box is massive, and its size is a mystery to me

Why is the box around my paragraph tag so large? I suspect this could be why I am unable to move the <p> tag down with margin-top: 50px; .train p { margin: 0; font-size: 2.5vw; } <div class="train"> <p class="train">In Training& ...

Bullet-point Progress Indicator in Bootstrap

Seeking guidance on how to create a Bootstrap progress bar with dots incorporated, similar to the image linked below. Any tips or resources where I can learn more about this technique? https://i.stack.imgur.com/BF8RH.jpg .progress { width: 278px; he ...

Incorporating a PHP script into an HTML document for display

I have a PHP file that retrieves line items from a database and displays them in a table. On an HTML page, I have a form for adding items to the database that houses a restaurant menu. I want to embed the PHP script responsible for showing the line items i ...

Looking for Protractor CSS functionalities nodes

I tried the code below but am having trouble locating the "Login" text using Protractor: <div _ngcontent-c2="" class="align-center"> <img _ngcontent-c2="" alt="Autoprax" class="ap-logo" src="/images/apLogoSmall.svg" style="width: 100%"> ...

Adding a hyperlink to each table cell (td) in HTML and CSS that is visible outside of the table

In my table, there is a link on every td. I want these links to appear outside the table. Although I have achieved this, the first cell that was created needs to be hidden. When I try to hide the td using CSS, the link also disappears. Here is the HTML: ...

Send properties to the makeStyles function and apply them in the CSS shorthand property of Material UI

When working with a button component, I pass props to customize its appearance: const StoreButton = ({ storeColor }) => { const borderBottom = `solid 3px ${storeColor}`; const classes = useStyles({ borderBottom }); return ( <Button varian ...

Can object methods be called using inline event attributes in an HTML tag?

Exploring the depths of core JavaScript has been my latest obsession. It's fascinating how we can easily trigger functions within the global context using events like this. HTML <span class="name" onclick="clicked()">Name</span> JavaSc ...

SVGs do not display on iPhones

Having some issues with my code - specifically, I have an SVG animation that seems to work on all devices except iPhones. I've been struggling to find a solution. Here is the code snippet for the SVG: <div class="contenuto-svg"> <svg vi ...