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

Passing a JavaScript variable to a URL parameter can be achieved by

Can anyone advise me on passing JavaScript string variables and displaying them in the URL? For instance, let's say the URL is "xyc.com". Upon populating a variable (a), I wish for that variable to appear in the address bar as URL = "xyc.com/?a=". Cur ...

Responsive HTML grid with a distinct line separating each row

My challenge is to create a layout that features a grid of floating divs with horizontal lines under intermediate rows. The catch is that there should not be a line under the last row, and if there is only one row of elements, no line should be displayed. ...

Implementing asynchronous image loading with Angular 4 using bearer headers

Currently, I am working on making asynchronous image requests with authentication headers. The image paths in the code look like this: <img src="{{file.src}}"/> I need to include a Bearer Token in the header for these requests, but since there are ...

Updating an HTML element by using the input value in a text field with JavaScript/jQuery

Although it may seem straightforward, I am new to Javascript and struggling to find or create the script I need. I have a list of BIN numbers for credit cards and want to extract the first 6 digits of the card number field to determine the type of card, an ...

Tips for creating a "sticky" button in Angular that reveals a menu when clicked

Is there a way to incorporate a feature similar to the "Get help" button on this website: The button, located in the lower right corner, opens a sticky chat menu. I am interested in adding dynamic information to the button (such as the number of tasks a u ...

An angular component that is functioning properly when connected to a live server, however, it is experiencing issues when trying to run `

I tried integrating versitka into my Angular component by placing the version HTML file and all necessary assets in the appropriate directories. However, when I run ng serve, only the HTML file seems to be working, while the CSS and images fail to load. I ...

What is the best method for incorporating a unique style to a v-dialog component in Vuetify following the most recent update?

I'm currently working on a project using vuetify, and I am facing an issue while trying to add a custom class to a v-dialog component. I have tried searching for information on how to do this, but it seems that the prop "content-class" has been deprec ...

What is the best way to utilize overflow with TailwindCSS?

1. The Home Component: import React from "react"; import Title from "./Title"; import TaskInput from "./TaskInput"; import TaskList from "./TaskList"; function Home() { return ( <div className="h-scree ...

The functionality of Jquery AJAX is operational, however, the message being displayed appears to

Inside the file changename.php, there is a DIV element: <div id="resultDiv"></div> Within the same file, PHP code can be found: <?php include_once ('connect.php'); if(isset($_POST['name'])) { $postedname = $_POST[&ap ...

Troubleshooting jQuery Dropdown Menu Animation Bugs

Take a look at this interesting fiddle: https://jsfiddle.net/willbeeler/tfm8ohmw/ HTML: <a href="#" class="roll-btn">Press me! Roll me down and up again!</a> <ul class="roll-btns"> <li><a href="#" class="control animated noshow ...

Including new styles in CKEDITOR.stylesSet that pertain to multiple elements

My idea involves creating a unique bullet list style with two specific features: a. A blue arrow image replacing the standard list icon b. Very subtle dotted borders above and below each list item. I am looking to incorporate this design into CKEditor t ...

Removing cookies after sending a beacon during the window unload event in Chrome

Here's the situation: I need to make sure that when the browser is closed or the tab is closed, the following steps are taken: Send a reliable post request to my server every time. After sending the request, delete the cookies using my synchronous fu ...

Unable to apply margin right to React Material-UI table

I am struggling to add margin to the right of a mui table with no success. Below is the code I have used: <TableContainer> <Table sx={{ mr: 100 }} aria-label="customized table"> <TableHead> <Tabl ...

Containers do not line up properly with each other. Adjusting the width leads to unexpected consequences

As someone who is new to HTML and CSS, I am facing a challenge with aligning the items within a navigation bar on my website. The list serves as a navigation bar and is contained inside the "inner header" div. While I was able to align the entire "nav" con ...

Having trouble with the flexbox flex-direction: column property not functioning properly with the textarea element in Firefox?

Inconsistencies between Chrome and Firefox have been noticed in the following example. While the footer height is set to height:40px and appears fine in Chrome, it seems smaller in Firefox, around 35px. Any specific reasons for this difference? Link to t ...

What is the best way to display form input fields depending on the selected value?

I've been struggling with a seemingly simple issue that I can't seem to find the right solution for, even after scouring Google. I have a form field for input and a select field with various values, including an "Other" option. Here's what ...

Unable to choose from the dropdown menu

I'm having trouble with selecting options on a selectbox when the select option is overlapping my menu. The menu I'm using is a jQuery Plugin. When the select box overlaps the menu, the options are hidden. I tried adjusting the z-index of my div ...

Combine collapse and popover in Bootstrap 3 for enhanced functionality

I'm facing some issues trying to use the badge separately from the collapse feature. $(function (e) { $('[data-toggle="popover"]').popover({html: true}) }) $('.badge').click($(function (e) { e.stopPropagation(); }) ) Check o ...

The grid is evenly populated with icons

Is it possible to evenly distribute the icons by only using CSS? I'm aiming to achieve a result similar to this (unfortunately, I can't post images due to my lack of reputation). You can find an example solution that uses JavaScript here: _htt ...

Displaying the division of shares in an HTML table using jQuery to add a new row

I recently worked on transposing a table, adding classes and ids to specific HTML tags, and converting numbers with commas into integers. Now, I am attempting to calculate the percentage share and display it in a new row. Here is the original table for re ...