Expand a sticky element by hiding the overflow

How can I create a sticky element with overflow hidden that expands to fill a parent element with scroll on overflow?

For example:

.wrap {
  position: absolute;
  width: 250px;
  height: 80px;
  overflow: scroll;
  background: #844;
}
.stick {
  position: sticky;
  white-space: nowrap;
  overflow: hidden;
  background: #484;
  top: 0;
}
.text {
  color: #ccc;
}
<div class="wrap">
  <div class="stick">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 10 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40</div>
  <pre class="text">Hello and good bye to the sticky scroll once the wrap area scrolls past the initial width
a
b
c
d
e
f
g
h
</pre>
</div>

The sticky green line does not fill the width of the scroll element in this scenario. The desired effect is visible when scrolling. While it could be achieved using JavaScript, my aim is to accomplish this with CSS.

A further requirement is for the sticky element to have overflow hidden because in practical use, it contains a wide canvas as a sub-element. This canvas exceeds the content width, such as the "text" mentioned above, and should not be visible beyond the size of "text".

An illustration:

https://i.sstatic.net/Z57fa.png

  • The yellow portion remains sticky at the top - It stays in place when scrolling up or down.
  • The yellow part scrolls horizontally along with the blue content.
  • The yellow element does not expand the blue content; it adheres to the overflow hidden property.
  • The yellow stick has the full width of the blue section.

Therefore, the sticky yellow component should:

  1. Have the same width as the blue content, hiding anything exceeding it
  2. Stay at the top of the viewport while scrolling up or down
  3. Follow the content's horizontal movement

In a real project, I utilize this feature in an MDI layout with multiple absolutely positioned "windows," each containing a single sticky element on the top and left of the content area, similar to how pixel bars function in GIMP when an image is opened. Although too intricate to display here, a basic representation is provided below:

The resizable handle allows adjusting the window size by dragging the bottom-right corner of the element.

(function() {
  "use strict";

  const spacer = {
    el: null,
    sz: {
      small: [100, 100],
      wide: [1000, 100],
      high: [100, 1000],
      big: [1000, 1000]
    },
    change: function (ev) {
      let z = spacer.sz[ev.target.value];
      spacer.el.style.width = z[0] + 'px';
      spacer.el.style.height = z[1] + 'px';
    },
    init: function () {
      spacer.el = document.querySelector('.content-spacer');
      spacer.el.addEventListener('change', spacer.change);
    }
  };

  ... <!-- JavaScript code continues -->
  
})();
... <!-- CSS code continues --> 
<div id="overlay"></div>
<div class="wrap">
    <div class="window">
        <div class="content-outer">
            <div class="header"><span>Header</span></div>
            <div class="content-inner">
                <div class="corner">C</div>
                <div class="top-line">
                  <div class="top-line-c"></div>
                </div>
                <div class="left-line">
                  <div class="left-line-c"></div>
                </div>
                <div class="content-text">
                  <div class="content-spacer">
                    <ul> 
                      <!-- Radio buttons for resizing options -->
                    </ul>
                  </div>
                </div>
            </div>
        </div>
        <div class="sizer" id="sizer" tabindex="0"></div>
    </div>
</div>

Answer №1

Check to see if this meets your requirements :

.wrap {
  position: absolute;
  width: 250px;
  height: 80px;
  overflow: scroll;
  background: #844;
}

.stick {
  position: sticky;
  display: table;
  white-space: nowrap;
  overflow: hidden;
  background: #484;
}

.text {
  color: #ccc;
}
<div class="wrap">
  <div class="stick">1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 10 21 22 23 24 25 26 27 28 29 30 31 32 33</div>
  <pre class="text">Hello and good bye to the sticky scroll once the wrap area scrolls past the initial width</pre>
</div>

This will cause the child <div> to fill the entire content of the parent <div>.

Here is the same example with an additional snippet included:

(function() {
  "use strict";

  const spacer = {
    el: null,
    sz: {
      small: [100, 100],
      wide: [1000, 100],
      high: [100, 1000],
      big: [1000, 1000]
    },
    change: function (ev) {
      let z = spacer.sz[ev.target.value];
      spacer.el.style.width = z[0] + 'px';
      spacer.el.style.height = z[1] + 'px';
    }
    
    // more code here...
})();
// CSS code snippets
// more code here...
// HTML code snippets
// more code here...

I have made a minor adjustment by adding display: table; to the .top-line class.

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

PHP script not running as intended

I created an HTML form where users can input values and select options. Once the user has made their selections, these values are then sent to a PHP script on the server. The PHP script retrieves data from a MySQL database based on the user-selected values ...

Running Perl CGI script within an HTML document

I'm currently facing an issue while attempting to execute a perl script within an html context. Upon execution, I am encountering an error labeled as "Internal Server Error," which does not provide much insight. Additionally, I am seeking guidance on ...

Creating a form element in JavaScript and submitting it - step by step tutorial

Is there a way to redirect to a different page once a JavaScript process running on the current page is finished? The issue here is that only JavaScript knows when it's complete, and I'm using Python (Flask) as my web framework. One solution I th ...

Positioning a DIV in the center within Bootstrap columns

I have been attempting to solve the challenge of vertically aligning content within my bootstrap grid. Within my 3 col-md-4 columns, I am looking to center another div along with text inside each of them. This is the current appearance: https://i.sstati ...

What is the best way to include numerous triangles in a single element?

I have designed a timeline featuring milestones with circular images using the CSS property (border-radius: 100%). I am attempting to create arrows at both the top and bottom of each .milestone-image-holder element by utilizing the :before and :after pseud ...

Clicking on a link with JQuery does not fire the action when the href attribute is set to "#open-site"

My navigation setup is as follows: <ul> <li><a href="index.html#open-site">Test</a></li> <li><a href="#open-site">Test</a></li> <li><a href="test.html#open-site"> ...

Focusing on the alignment of an article tag

I am currently facing a challenge in centering the content within an HTML article element. As of now, the content is aligned to the left margin of the page. Below is the snippet of HTML I am working with: <article> <h1>Sign In</h1> ...

When applying a vertical-align property to both an anchor tag and a button with the same class, why is the span also aligned to the top?

Why is the span tag aligning to the top when I have only applied properties to the .btn class elements, and not targeted it directly? .btns { text-align: center; } .btn { text-decoration: none; border: 1px solid black; display: inl ...

Is it acceptable to add customized (personalized) properties to DOM objects?

<div id="customDiv"></div> document.getElementByid('customDiv').myAttribute='customValue'; if('undefined'!==typeof document.getElementById('customDiv').myAttribute){ Would it be acceptable and compatib ...

Having difficulty viewing the images clearly

My JavaScript codes for scrolling images are not displaying all the images when viewed on Google Chrome. Is there a solution available? Below is the included JS code and CSS. data=[ [" images/img1.jpg"," Image1","pic01.jpg"], [" images/img2.jpg","Image2 " ...

What is the best way to control the overflow length and display only a specific amount of content at once?

I am currently designing the homepage for a social media platform. I have around 45 dummy posts and I am looking for a way to limit the overflow in CSS so that only 5 posts are displayed at a time. Once the user reaches the bottom of those 5 posts, another ...

drawing tool with JavaScript and HTML5

Can you help me figure out why my sketchpad isn't working in Processing.js? I've checked my code and there are no errors, but the canvas is not showing up. Any suggestions? Take a look at the code below: function createSketchPad(processing) { ...

Hiding components in Angular 4/5 through routing decisions

Just getting started with routing in Angular 4/5, I am currently following the tutorial provided on the official Angular website. I have an Angular application and I want to create two separate pages. Currently, the main page is located at localhost:8870/d ...

Implement jQuery to close multiple div elements

My dilemma involves a series of divs with different manufacturer IDs listed as follows: manufacturer[1], manufacturer[2], manufacturer[3], ... etc ... In attempting to create a JavaScript for loop to hide each div, I discovered that it was not achievab ...

Locate the Element containing specific text, and then navigate up several levels to select a different Element

I am in the process of developing a chrome extension that will remove a specific section from a website. In order to accomplish this, I must locate a <span> element that contains particular text and then identify the encompassing <div> tag. How ...

Mastering the Cache-Manifest file configuration for optimal performance

I am experiencing a problem where even though I have my cache manifest, my website is not functioning correctly. Currently, I am working with Visio 2012, using asp.net MVC. Here are the steps I have taken so far: I created a file called Manifest.manifes ...

The custom pagination feature in Addsearch UI always default to displaying the first page of search

I am currently using addsearch-ui version 0.7.11 for my project. I have been attempting to integrate a customized pagination function based on this example template. However, I have encountered an issue where the arrow buttons always navigate to the first ...

Hugo/Docsy encountered an error while executing the template at <.url>: unable to assess the url field in type bool

Currently, I'm in the process of creating a static web application using Hugo in combination with the Docsy theme. In my code for the Docsy Partials, I'm attempting to incorporate a conditional statement that appends the word mailTo: to my .url i ...

Hide image URL on mobile devices with CSS and JavaScript

Exploring the realm of cross-device magic, I have a challenge on my hands. My goal is to eliminate a slider image for screen widths smaller than 622px. Through experimentation, I have discovered that removing the image URL in CSS achieves the desired effec ...

What is the best way to eliminate YouTube branding from a video embedded in a website?

I am currently utilizing an <iframe width="550" height="314" src="https://www.youtube.com/embed/vidid?modestbranding=1&amp;rel=0&amp;showinfo=0" frameborder="0" allowfullscreen></iframe> This setup removes the "YouTube" logo from the ...