When the screen size changes, the centrally aligned position of sticky elements does not stay consistent

It has come to my attention that when centrally aligning an element with position sticky, the alignment can start to malfunction upon reducing the screen width past a certain point, depending on the width of the element. Unlike position: fixed, the sticky element eventually becomes stuck and loses its central alignment.

Do you have any insights as to why this occurs and perhaps a workaround?

div {
  height: 100px;
  width: 500px;
  background-color: red;
  position: sticky;
  left: 50%;
  transform: translateX(-50%);
}
<div>
</div>

Answer №1

If you want to center an element using flex, simply apply the flex property to the parent class and use justify-content

For example: https://jsfiddle.net/9cp6borj/

<body>
  <div class="div__item">
    <div class="div__sticky">
    </div>
  </div>
</body>
<style>
.div__item {
  display:flex;
  justify-content:center;
}
.div__sticky {
  height: 100px;
  width: 500px;
  background-color: red;
  position: -webkit-sticky;
  position: sticky;
  top: 20px;
}
</style>

Answer №2

Upon revisiting this question after some time, I found PIzmAR's workaround to be helpful. However, it lacks an explanation for the root cause, as pointed out by fy data, and there exists a more effective solution that doesn't involve wrapping HTML elements.

The issue at hand is quite straightforward. The 'sticky' property applies stickiness in both Y and X axes. As the sticky element maintains its position and size within the normal DOM flow, its untransformed state may reach the farthest right edge of the viewport (depending on its width), resulting in overflow. At this point, the sticky element behaves similarly to how it does on the Y axis and stays fixed in position. The culprit here is the 'left: 50%' attribute, much like how 'top:' functions with sticky in a vertical direction.

This outcome wasn't what I needed then, and it's likely not ideal for many others aiming to center-align a sticky element using 'left'. Therefore, the optimal approach is to treat the sticky element as relative and utilize margins for X-axis centering. Here's how:

#box-2 {
  top: 0;
  left: 0;
  transform: translate(0);
  margin-left: auto;
  margin-right: auto;
}

For demonstration purposes, run the code snippet in fullscreen to view examples:

        function updateBoxClasses(boxId, highlighted) {
            const box = document.getElementById(boxId);
            if (highlighted) {
                box.classList.add('highlighted');
                box.classList.remove('box');
            } else {
                box.classList.add('box');
                box.classList.remove('highlighted');
            }
        }

        function updateClasses() {
            const viewportWidth = window.innerWidth;
            if (viewportWidth <= 1000) {
                updateBoxClasses('box-ghost', true);
                updateBoxClasses('box', true);
                updateBoxClasses('box-2', true);
            } else {
                updateBoxClasses('box-ghost', false);
                updateBoxClasses('box', false);
                updateBoxClasses('box-2', false);
            }
        }

        updateClasses();
        window.addEventListener('resize', updateClasses);
* {
  box-sizing: border-box;
}

body {
  width: 100%;
  padding: 0;
  margin: 0;
}

div#container {
  height: 2000px;
  width: 100%;
  position: relative;
}

div#heading {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 64px;
  height: 200px;
  border: solid black 2px;
}

#box, #box-2 {
  position: sticky;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 32px;
  color: white;
  opacity: 50%;
  top: 100px;
}

#box-2 {
  top: 210px;
  margin-top: 10px;
  left: 0;
  transform: translate(0);
  margin-left: auto;
  margin-right: auto;
}



span {
  font-size: 12px;
  position: absolute;
  top: 5px;
  left: 10px;
}

#box-ghost {
  position: absolute;
  top: 0;
  left: 50%;
  background-color: transparent;
  border-style: dashed;
  border-width: 10px;
  opacity: 70%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 32px;
}

/* div#half-width {
  position: fixed;
  left: 0;
  right: 0;
  width: 250px;
  height: 100px;
  background-color: grey;
  display: block;
  color: lightgray;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 32px;
} */

.highlighted {
  border-color: blue;
  color: blue;
  background-color: blue;
   width: 500px;
  height: 100px;
}

.box {
  color: red;
  background-color: red;
  border-color: red;
   width: 500px;
  height: 100px;
}
<div id="heading"><span>position: relative</span>Resize viewport to see demo</div>
<div id="container">
  <div class="box" id="box"><span>position: sticky (left: 50%, transform: translateX(-50%))</span>500px</div>
  <div class="box-ghost" id="box-ghost"><span>position: absolute (left: 50%)</span>500px (not transformed)</div>
  <div class="box" id="box-2"><span>position: sticky (margin-left: auto, margin-right: auto)</span>500px (solution)</div>
</div>

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

Can you explain the mechanics behind Angular Component CSS encapsulation?

Is it possible to avoid CSS conflicts when using multiple style sheets? Consider Style 1: .heading { color: green; } And Style 2: .heading { color: blue; } If these two styles are applied in different views and rendered on a layout as a Partial Vi ...

Refreshing a HTML table by retrieving data from a session variable

I'm still learning the ropes when it comes to JSP, jQuery, and AJAX. I have a JSP page that utilizes a table populated with values from a List variable stored in session. Below is the code snippet: <table id="someData" class="display" width="95%"& ...

Why does the hashtag keep popping up every time I launch the Bootstrap Modal?

I can't figure out why this keeps happening. I researched how to eliminate hashtags from the URL and found multiple solutions. However, none of them proved to be helpful as they only removed the hashtag, requiring a page refresh which still didn' ...

ensure that the element matches the height of its parent element

Allow me to illustrate the issue with a visual aid. In the provided image, you can observe that the checkmark is perfectly centered vertically in the one-line p element. However, as the text extends to two or three lines within the p element, the checkmar ...

Shifting static information both above and below a 100vh element

At the moment, I have a stationary image in the center of my screen that moves horizontally when scrolling with the mouse. Now, I want to include a screen above and below this element, each with a height of 100vh. However, when I attempt to do so, the fixe ...

Firefox changes the sequence of form input attributes

Is anyone familiar with this issue and how to prevent it? I am using AJAX to populate a div with a form from a PHP file. The input element is generated in PHP as shown below: echo ('<input type="text" name="court_type_name" id="court_type_name_in ...

How to retrieve the time duration in minutes between a start and end time in JavaScript to

I have conducted a thorough search on Stack Overflow and other platforms but could not find an answer to my specific question. While I did come across posts related to this topic, they did not address the issue I am facing. Problem: My challenge involves ...

Guide to verifying a value within a JSON object in Ionic 2

Is there a way to check the value of "no_cover" in thumbnail[0] and replace it with asset/sss.jpg in order to display on the listpage? I have attempted to include <img src="{{item.LINKS.thumbnail[0]}}"> in Listpage.html, but it only shows the thumbna ...

Deactivate numerous buttons with the help of jQuery

Within my MVC view, I have a Razor foreach loop that generates table rows with buttons: @foreach (var item in Model) { <tr> <td>@item.Id</td> <td> <button id="btn" class="button btn-primary" type= ...

Checkbox: Activate button upon checkbox being selected

On my webpage, I have a modal window with 2 checkboxes. I want to enable the send button and change the background color (gray if disabled, red if enabled) when both checkboxes are selected. How can I achieve this effectively? HTML: <form action="" me ...

Is there a way to make the hoverzoom effect only apply to a specific object instead of all objects?

I'm currently troubleshooting my code in order to implement a hover effect on Mui <cards> when a user hovers over the object. I have managed to make it work partially, but the issue is that the effect gets applied to all objects instead of just ...

Unable to dial phone numbers when clicking 'Click to call' link on Android devices

Within my Cordova android application, I have a link set up like this: <a href = "tel:011123456789">Click to Call</a> While this click-to-call functionality works smoothly in IOS, it seems to be hindered in Android by an issue such as: 11- ...

What is the best way to conceal all lists except for the one that has been chosen?

Looking to enhance my sortable list with jQuery UI - currently it's functional, but I'd like to incorporate a dropdown menu for each item to designate the active one when clicked. Is there a way to modify the code so that upon selection, only th ...

Phonegap application functioning smoothly on computer, encountering issues on mobile device

Hey there! I recently developed a phonegap app that retrieves JSON data from a YQL link and presents it to the user. It works perfectly on Google Chrome desktop, but my client mentioned that it doesn't work on his Android 2.3 device. Could you help me ...

Having trouble with the toggle button on the Bootstrap 5 navbar?

I am facing an issue with my HTML code where the toggle button does not display properly when I resize the browser screen. Upon clicking on it, the navigation bar items do not show at all. Here is a screenshot of my website <html> <head> ...

The Bootstrap Jumbotron section stubbornly stays above the footer

I'm currently working on a Bootstrap 3 page and facing an issue where the yellow area does not seamlessly transition into the green footer. Instead, there is a white space between the end of the yellow area and the start of the footer. How can I resol ...

Retrieve information from a MySQL database and integrate it into a different application

This php script is used to generate a table with a "book" button next to each row. The goal is to extract the values of "phase" and "site" from the specific row where the "book" button is clicked, and transfer them to another form (in "restricted.php") fo ...

Having Trouble With Your Font Display?

I am confident that my code is correct. Here's a snippet from what I've done: <!DOCTYPE html> <html> <head> <link rel="stylesheet" href="homepage.css"/> </head> <body> <div id="menu"> &l ...

Python Automation with Selenium (Locating elements using CSS selectors)

Is it possible to select and access the first element of an array using a CSS selector in Python? For instance: css=('.od-FieldEditor-fieldTitle.ms-Label.is-required') generates an array of 6 labels. I am interested in accessing the innerText a ...

What do you think about removing the Bootstrap styling from the design?

As a newcomer to Bootstrap and currently working on my second project using this framework, I find myself struggling with overriding CSS styles. It can be time-consuming to locate the specific style that needs to be changed rather than starting from scratc ...