What steps can be taken to stop the grandchild with an absolute position from increasing the scrollHeight or scrollWidth of containers?

Is it possible to make a container scroll size dependent on its child only?

The issue arises when the child element (.scrollable) contains absolutely positioned elements that may exceed the boundaries of the child. How can I make sure that the containers scroll size ignores these overflows?

Any suggestions on how to achieve this?

.container {
  width: 150px;
  height: 150px;
  border: 1px #ddd solid;
  overflow: auto;
}

.scrollable {
  width: 100px;
  margin: 5px;
  background: linear-gradient(#f79862, #fd6a02)
}

.row {
  width: 100%;
  position: relative;
  height: 30px;
  background: linear-gradient(#f79862, #fd6a02)
}

.overflow {
  position: absolute;
  top: 0;
  left: 20px;
  height: 400px;
  width: 30px;
  background: #09c;
  z-index: 2;
}
<div class="container">
  <div class="scrollable">
    <div class="row">
    </div>
    <div class="row">
    </div>
    <div class="row">
    </div>
    <div class="row">
      <div class="overflow">
      </div>
    </div>
    <div class="row">
    </div>
    <div class="row">
    </div>
    <div class="row">
    </div>
  </div>
</div>

In this scenario, I am looking to adjust the scrolling behavior so that it stops as soon as the last orange line becomes visible, rather than showing the entire blue bar.

Answer №1

Make sure to apply overflow:hidden to the .scrollable div in order to prevent it from extending beyond its parent container

Here is a suggested solution:

.container {
  width: 150px;
  height: 150px;
  border: 1px #ddd solid;
  overflow: auto;
}

.scrollable {
  width: 100px;
  margin: 5px;
  background: linear-gradient(#f79862, #fd6a02);
  overflow: hidden; /*added here*/
}

.row {
  width: 100%;
  position: relative;
  height: 30px;
  background: linear-gradient(#f79862, #fd6a02)
}

.overflow {
  position: absolute;
  top: 0;
  left: 20px;
  height: 400px;
  width: 30px;
  background: #09c;
  z-index: 2;
}
<div class="container">
  <div class="scrollable">
    <div class="row">
    </div>
    <div class="row">
    </div>
    <div class="row">
    </div>
    <div class="row">
      <div class="overflow">
      </div>
    </div>
    <div class="row">
    </div>
    <div class="row">
    </div>
    <div class="row">
    </div>
  </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

Conceal a section of a container with the click of a button within a WordPress Plugin

I am currently utilizing a Wordpress plugin known as contact form 7 to construct an email list for an upcoming website project. The client has specifically requested that we avoid using services like mailchimp due to their preference of not sending ANY ema ...

Is it necessary to scroll when all elements on the page are in absolute position?

I have all elements positioned absolutely on my page. Some are at the top and some are at the bottom. However, when I add an additional element to display news text in the center of the page, it gets hidden under the element aligned at the bottom. When I s ...

Tips on implementing CSS to the subpar class in Vuejs

I am working on an HTML file that operates with button in Vue.js. The v-bind:class can be utilized for a single tag as shown below. It disappears based on the boolean value of bool data. <h3 v-bind:class="{active: bool}">{{counter.document}}&l ...

Creating a circular price display using CSS:

Below is the code snippet that I am working with: .special-price { text-align: center; background-color: #2e566e; } .special-price .offer { border-radius: 50%; background-color: #56c243; color: #fff; font-weight: 700; font-size: 18px; h ...

What is the quickest method to forward a page to a different web address?

I am looking to direct traffic from my Blogger website to an external URL and have found two possible methods to do so: One option is to use a meta tag: <meta http-equiv="Refresh" content="0; url='https://google.com' Alternativ ...

Align Text Center inside a Magical H1 Heading Tag

I'm having a bit of trouble with something that should be simple. I want to center the text inside my h1 tag on a wizard, and I've added this CSS to my stylesheet.css: .h1textalign { text-align:center; } Here's how I'm trying to apply ...

What is the best way to merge two JSON values with the help of jQuery?

Two JSON Objects are provided below: The first one is: [{ "id": 5001, "count": "0", "type": "None" }, { "id": 5002, "count": "0", "type": "Glazed" }, { "id": 5005, "count": "0", "type": "Sugar" }, { "id": 5003, ...

Several submission choices available within a single form

My form is set up like this: <form action='../performSQL.php' method='post'> <input type='hidden' name='hidden_id' value='$id'> <input type='date' name='porteringsdato' va ...

What is the best way to retrieve the height of a div that includes an image that has been

I have a script that inserts images into different columns. I want the script to insert images into the column with the smallest height. However, every time I try to check the height of the columns, it always returns 30px (likely because the images are not ...

HTML- Any suggestions on how to troubleshoot my sticky navbar not functioning properly?

I'm having trouble creating a sticky navbar. I've attempted to use z-index: 999 but it's not behaving as expected. * { margin: 0; padding: 0; } .navbar { display: flex; align-items: center; justify-items: center; position: ...

Displaying both input fields and buttons side by side on mobile devices using Bootstrap

I am in the process of creating a navbar that includes select, input, and button elements. Below is the code I have written: <nav class="navbar sticky-top navbar-light p-0"> <div class="collapse navbar-collapse search-bar show p-4" id="navbarSupp ...

Ensure that the method is passed a negative number -1 instead of the literal number 1 from an HTML error

This is an example of my HTML code: <button (mousedown)="onMouseDown($event, -1)"></button> Here is the TypeScript code for handling the mouse down event: onMouseDown(e: MouseEvent, direction: 1|-1) { this.compute.emit(direction); ...

Aligning text to the left center

I am looking to center left-aligned text in the yellow box (txtbox). The current look is like this: and I want it to appear like this: HTML: <div class="content container small"> <div class="txtbox"> ...

When there are multiple elements inside a bootstrap button, it causes the button to lose its

I've encountered an issue with the bootstrap button. Whenever I insert 2 elements like div or span inside it, the little arrow within the button ends up below the button text. It seems to be caused by a float attribute which then arranges the elements ...

Generating Dynamic DIV Elements in Angular 2

I am looking to dynamically generate divs in Angular 2 with Bootstrap classes based on data retrieved from a database. For instance, if I receive 6 categories from the database, I would like to display this as 3 rows with 2 columns each. How can I implemen ...

What are the steps to enable CSS code completion for JavaFX again?

Hello everyone, Recently, I disabled the CSS code completion feature for JavaFX-Tags such as '-fx-...'. Unfortunately, I'm not sure how or why it happened but now I would like to enable the code suggestions again. I have been unable to loca ...

I am experiencing issues with the functionality of the jQuery function

I am currently diving into the world of jquery. I put together a script, but unfortunately it's not functioning as expected. 1 Here is a snippet of my HTML code <ul id="recur" class="elasticstack"> <li id=<?=$item['id']?> ...

Activate the Dart checkbox to ensure it is checked

Is there a way to programmatically check a CheckBox element? I've attempted using Click();, but it hasn't worked. I've also looked for a method like .setChecked(true); without any luck. Could it be because I'm utilizing the checkbox as ...

How to Correctly Align Links in Bootstrap Navbar

I am currently utilizing Bootstrap 5 and I am looking to perfectly center some links within the navigation bar. <header> <nav class="navbar navbar-expand-lg py-5"> <div class="container"> ...

Can a relative link with parameters but no path be used?

Can I omit the path from an href tag and begin with a question mark instead? For instance, if my website is https://example.com/mycgi, would it be acceptable to have a link like this: <a href="?foo=bar">bar</a>? I tested this on Fir ...