Ensure that the header stays centered on the page as you scroll horizontally

Below is a given code snippet:

header {
  text-align: center;
}
/* This section is just for simulation purposes */
p.text {
  width: 20rem;
  height: 50rem;
}
<html>

<body>
  <header>
    <h1>Page Title</h1>
    <details>
      <summary>Click Me</summary>
      <p>More information</p>
      <p>Even more stuff</p>
    </details>
  </header>
  <table>
    <tr>
      <td><p class="text">Lorem</p></td>
      <td><p class="text">ipsum</p></td>
      <td><p class="text">dolors</p></td>
      <td><p class="text">itamet</p></td>
      <td><p class="text">consectetura</p></td>
      <td><p class="text">dipiscinge</p></td>
      <td><p class="text">litsed</p></td>
      <td><p class="text">doeiusmod</p></td>
    </tr>
    <tr>
      <td><p class="text">Lorem</p></td>
      <td><p class="text">ipsum</p></td>
      <td><p class="text">dolors</p></td>
      <td><p class="text">itamet</p></td>
      <td><p class="text">consectetura</p></td>
      <td><p class="text">dipiscinge</p></td>
      <td><p class="text">litsed</p></td>
      <td><p class="text">doeiusmod</p></td>
    </tr>
  </table>
</body>

</html>

The goal is to have the content of <header> centered when horizontally scrolling, but disappear when vertically scrolling.

I've experimented with different CSS properties like position: sticky; left: 0;, but have not achieved the desired result. The closest I got was with this code:

    header {
      text-align: center;
      position: fixed;
      top: 0;
      left: 0;
      width: 100vw;
      z-index: -1;
    }
    table {
      margin-top: 7rem;
    }

However, this solution fails when expanding the <details> element and doesn't function properly on Firefox Mobile.

Looking for a pure, semantic approach without relying on frameworks. Open to some use of JavaScript if necessary.

Thank you for your assistance.

Answer №1

To enable horizontal stickiness with position: sticky (which appears to be the desired effect here), the containing element must be larger than the element you want to stick.

Additionally, it's important to consider the overflow settings of ancestor elements. Refer to position:sticky is not working for more insights on this topic.

<!doctype html>
<html>
<head>
<title>sticking</title>
<style>
body {
  margin: 0;
  overflow: visible;
  width: fit-content;
}
header {
  width: 100vw;
  text-align: center;
  position: sticky;
  left: 0px;
}
/* not essential, just using it to simulate a large table */
p.text {
  width: 20rem;
  height: 50rem;
}

</style>
</head>
<body>
  <header>
    <h1>Page Title</h1>
    <details>
      <summary>Click Me</summary>
      <p>More information</p>
      <p>Even more stuff</p>
    </details>
  </header>
  <table>
    <tr>
      <td><p class="text">Lorem</p></td>
      <td><p class="text">ipsum</p></td>
      <td><p class="text">dolors</p></td>
      <td><p class="text">itamet</p></td>
      <td><p class="text">consectetura</p></td>
      <td><p class="text">dipiscinge</p></td>
      <td><p class="text">litsed</p></td>
      <td><p class="text">doeiusmod</p></td>
    </tr>
    <tr>
      <td><p class="text">Lorem</p></td>
      <td><p class="text">ipsum</p></td>
      <td><p class="text">dolors</p></td>
      <td><p class="text">itamet</p></td>
      <td><p class="text">consectetura</p></td>
      <td><p class="text">dipiscinge</p></td>
      <td><p class="text">litsed</p></td>
      <td><p class="text">doeiusmod</p></td>
    </tr>
  </table>
</body>

Please note: I don't have access to mobile Firefox for testing purposes. Kindly confirm if it works properly on that platform.

Answer №2

One solution could be to add a scroll element around the table, but this might cause the scroll bar to appear at the bottom.

header {
    text-align: center;
  }
  /* This is optional, just for simulating a large table */
  p.text {
    width: 20rem;
    height: 50rem;
  }
  .scroll {
    overflow-x: scroll;
  }
<header>
            <h1>Page Title</h1>
            <details>
                <summary>Click Me</summary>
                <p>More information</p>
                <p>Even more stuff</p>
            </details>
        </header>
        <div class="scroll">
            <table>
                <tr>
                    <td><p class="text">Lorem</p></td>
                    <td><p class="text">ipsum</p></td>
                    <td><p class="text">dolors</p></td>
                    <td><p class="text">itamet</p></td>
                    <td><p class="text">consectetura</p></td>
                    <td><p class="text">dipiscinge</p></td>
                    <td><p class="text">litsed</p></td>
                    <td><p class="text">doeiusmod</p></td>
                </tr>
                <tr>
                    <td><p class="text">Lorem</p></td>
                    <td><p class="text">ipsum</p></td>
                    <td><p class="text">dolors</p></td>
                    <td><p class="text">itamet</p></td>
                    <td><p class="text">consectetura</p></td>
                    <td><p class="text">dipiscinge</p></td>
                    <td><p class="text">litsed</p></td>
                    <td><p class="text">doeiusmod</p></td>
                </tr>
            </table>
        </div>

Answer №3

To address horizontal scrolling, it is recommended to place the header element outside of the container you want to horizontally scroll.

<body>
  <header class="fixed"></header>
  <table class="scroll"></table>
</body>

Note: Avoid scrolling the body element as it can cause unexpected behavior in child elements. Instead, utilize css properties like .fixed { position: fixed; } and .scroll { overflow-x: scroll }

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

Elevate the index of the name attribute for an input field within a dynamically generated form

In my scenario, I have created a form that includes indexed input fields for username and level: <form> <table> <tr class="trToClone"> <td> <label>Username:</label> <input type="text" name="usernam ...

HTML5, canvas covering every element

Below is the code that I am working with: <html> <head> <title>canvas</title> </head> <body> <canvas width="1745" height="569" style="width: 1730px; height: 1680px; position: absolute; z-index: 1"></canvas> ...

The process of making an image fill an entire div using Html and CSS

What is the best way to make an image fill an entire div using Html and CSS? ...

Trouble with scrolling horizontally on the website

After completing the coding for this website with bootstrap, I encountered an unexpected issue with a horizontal scroll appearing. This is my first project using bootstrap. I attempted to troubleshoot by using the 'divide et impera' method - rem ...

I am having trouble getting my divs to show up side by side in one line

I am struggling to get the second div (right-column) to move next to the first div (left-column) in order to create 2 columns of links. I would prefer not to create a separate stylesheet for this small page. <div class="container" style="height: 700px; ...

Utilizing the Bootstrap grid system for responsiveness is optimized for a first word that is precisely 15 characters

Trying to achieve responsiveness using basic Bootstrap. The layout becomes responsive only when the initial word in a sentence contains more than 15 characters, as illustrated below. https://i.stack.imgur.com/wyErA.png <!-- Headers --> <div clas ...

Is it possible to pass multiple IDs into document.getElementById?

I am working on a form that contains 45 dropdown lists and currently, I am using the following code for validation. Is there a way to modify this code so that I can use a single function to validate all 45 dropdown lists? Below is the Function: function ...

Issues detected between Angular and Express rendering operations

Having an issue with my angular code. It runs perfectly on its own, but when I try to access it on localhost with express, it only displays the HTML file. Here's my server code: var express = require('express'), app = express(); app ...

Why does Node.js prompt an error when using document.querySelector? Is there an alternative method to document when utilizing express in my project?

An issue arose when attempting to select the form element using const weatherForm = document.querySelector('form') ^ Unfortunately, a ReferenceError was encountered: document is not defined at Object. (/Users/sheebadola/development/quadacademy/w ...

Could you show me how the easyrtcid is generated in the demonstration of audio-only chat? I would appreciate a step-by

Currently, I am utilizing the easyrtc webpage to test out the audio only chat demo and everything seems to be running smoothly. However, when connecting, the easyrtcid variable is automatically assigned a random string. I was wondering if there is a way t ...

Tips for adjusting text to fit within a container without needing to use overflow:auto

Is there a way to prevent text overflow in a container without using the overflow:auto property? I've come across plugins that automatically reduce the text size, but I'm not keen on that solution. What I want is for long words or URLs to wrap on ...

One-time PHP form submission only

On my website, I offer a variety of PDF download options. To access these downloads, I encourage visitors to sign up using a form. After signing up, they receive a direct link to the chosen PDF in their email. My goal is to streamline the process so that ...

The scroll function remains unchanged when using overflow-y scroll and border radius

I need help with styling a div that will contain a large amount of text on my webpage. I want to use the overflow-y property for scroll, but the issue arises when trying to style the scroll bar with a radius. Check out this jsfiddle link for reference: ht ...

Tips for creating a dynamically responsive eye icon placement

When the screen width is less than 991px, the content is centered but the eye icon that is absolutely positioned goes off-center. This issue only occurs on devices with a small or medium screen size. The reason for using absolute positioning on the icon i ...

Modify request parameters in real-time using JavaScript

i am seeking assistance with a link request: <a href=index.jsp></> also, i have various divs located elsewhere on the page that contain changing values based on user input or specific conditions: <input id="var1" /> <input id="var2" ...

Issue with Next.js app styles persisting on pages after page refresh

I am currently working on my first next.js project, transitioning from create-react-app, and I've encountered an unusual issue. When I refresh the home page, the CSS styles persist without any problem. However, if I navigate to a different page like " ...

I'm looking for a solution to pass a PHP object as a parameter in JavaScript within an HTML environment,

I am currently working on a project using Laravel 5. I have a table in my view, and I want to pass all the data values to a JavaScript function when a link is clicked. I have tried several methods but have been unsuccessful so far. @foreach ($basl_offic ...

Creating a HTML element that functions as a text input using a div

I am encountering an issue with using a div as text input where the cursor flashes at the beginning of the string on a second attempt to edit the field. However, during the initial attempt, it does allow me to type from left to right. Another problem I am ...

Getting rid of the arrow icons in a Material UI TextField

I am facing a challenge with removing the up and down arrow icons from a Material UI TextField that I adjusted using the Material UI documentation (https://material-ui.com/components/autocomplete/#autocomplete) Highlights section. After trying various sol ...

Uploading images using the Drag and Drop feature in HTML

Hello, I'm having an issue with the drag and drop functionality. I want to expand the size of the input to cover the entire parent div, but for some reason, the input is appearing below the drag and drop div. Can anyone assist me with this? https://i. ...