Overlapping dynamic content causing CSS nested scrollbars to clash at full width of 100%

Displayed below is a layout with two nested divs, both featuring automatic vertical scrolling.

Is there a CSS technique available to show the inner scrollbar while maintaining the inner div at 100% width?

https://i.stack.imgur.com/HSKHH.png

div {
  background: rgba(0, 0, 0, 0.2);
  width: 200px;
  height: 400px;
  overflow-y: auto
}
#outer { height: 400px; }
#inner { height: 200px; }
button { float: right; color: #F60}
<button onclick="document.getElementById('outerContent').innerHTML='smaller content does not need to scroll #inner should still be 100% width of #outer'">TRY WITH SMALLER CONTENT</button>

<div id='outer'>
  <span id='outerContent'>Some long content which causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which
  causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which
  causes scroll</span>

  <div id='inner'>INNER DIV ALSO NEEDS TO SCROLL BUT I CAN'T SEE THE SCROLLBAR INNER DIV ALSO NEEDS TO SCROLL BUT I CAN'T SEE THE SCROLLBAR INNER DIV ALSO NEEDS TO SCROLL BUT I CAN'T SEE THE SCROLLBAR INNER DIV ALSO NEEDS TO SCROLL BUT I CAN'T SEE THE SCROLLBAR INNER DIV
    ALSO NEEDS TO SCROLL BUT I CAN'T SEE THE SCROLLBAR INNER DIV ALSO NEEDS TO SCROLL BUT I CAN'T SEE THE SCROLLBAR INNER DIV ALSO NEEDS TO SCROLL BUT I CAN'T SEE THE SCROLLBAR
  </div>
</div>

Answer №1

Absolutely, it is entirely feasible.

To achieve this effect, you must implement width:auto and display:block on the #inner element. Though these styles are typically applied automatically by most (if not all) browsers, they may need to be explicitly set in your case due to the fixed width specified in line 3 of your CSS (div { width: 200px }), which overrides default behavior where #inner adjusts to its parent's width.


This issue can be resolved by only setting width:200px on #outer, without applying it to both divs:

/* Remove fixed width from div... */
div {
  background: rgba(0, 0, 0, 0.2);
  height: 400px;
  overflow-y: auto
}

/* ...and apply it solely to #outer */
#outer { height: 400px; width: 200px; }
#inner { height: 200px; }

See the solution in action on this fiddle


Alternatively, you can maintain width:200px for both divs (as currently done) and then revert it for #inner using width:auto.

/* Keep fixed width on both divs... */
div {
  background: rgba(0, 0, 0, 0.2);
  height: 400px;
  width: 200px;
  overflow-y: auto
}

/* ...and reset width to default for #inner */
#outer { height: 400px; }
#inner { height: 200px; width: auto; }

View the alternate approach in this fiddle

Answer №2

To achieve the desired effect, simply set the #inner to have a style of width:auto.

This adjustment will allow the block element to expand as much as needed while still displaying a scrollbar when necessary.

div {
  background: rgba(0, 0, 0, 0.2);
  width: 200px;
  height: 400px;
  overflow-y: auto
}
#outer { height: 400px; }
#inner { height: 200px; width:auto;}
button { float: right; color: #F60}
<button onclick="document.getElementById('outerContent').innerHTML='smaller content does not need to scroll #inner should should still be 100% width of #outer'">TRY WITH SMALLER CONTENT</button>

<div id='outer'>
  <span id='outerContent'>Some long content which causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which
  causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which causes scroll Some long content which
  causes scroll</span>

  <div id='inner'>INNER DIV ALSO NEEDS TO SCROLL BUT I CANT SEE THE SCROLLBAR INNER DIV ALSO NEEDS TO SCROLL BUT I CANT SEE THE SCROLLBAR INNER DIV ALSO NEEDS TO SCROLL BUT I CANT SEE THE SCROLLBAR INNER DIV ALSO NEEDS TO SCROLL BUT I CANT SEE THE SCROLLBAR INNER DIV
    ALSO NEEDS TO SCROLL BUT I CANT SEE THE SCROLLBAR INNER DIV ALSO NEEDS TO SCROLL BUT I CANT SEE THE SCROLLBAR INNER DIV ALSO NEEDS TO SCROLL BUT I CANT SEE THE SCROLLBAR
  </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

Issues have been reported with Angular 10's router and anchorScrolling feature when used within a div that is positioned absolutely and has overflow set

I feel like I may be doing something incorrectly, but I can't quite pinpoint the issue. Any help or pointers would be greatly appreciated. My current setup involves Angular 10 and I have activated anchorScrolling in the app-routing.module file. const ...

How can I include line breaks using HTML `<br>` tags in a textarea field that is filled with data from a MySQL

Within a modal, I am showcasing a log inside a read-only <textarea> field that contains data fetched from my MySQL database. Below this, there is a writable <textarea> field where users can input updates to the log, which are then added to the ...

What is the best way to convert JavaScript to JSON in Python programming?

I encountered a situation where I have an HTML page that contains a lengthy product list, making it too large for me to upload. The products are stored within the script section of the page, specifically in one variable. Initially, I mistook this data for ...

Tips for creating responsive scrollable columns in an HTML table with Bootstrap

I'm not a professional web designer. I attempted to create a web layout with scrollable columns containing data in anchor tags that are loaded dynamically. To achieve this, I created an HTML table structure along with a stylesheet. Here is the source ...

Scroll to the end of the main div's horizontal position instead of using offset().left

I'm struggling with an issue in my code. <script type="text/javascript"> $(function() { $('ul.nav a').bind('click',function(event){ var $anchor = $(this); /* ...

Having trouble with the initial tap not being recognized on your mobile browser?

When using mobile web browsers (specifically chrome and firefox on iOS), I am experiencing an issue where the hamburger menu does not trigger when tapped for the first time. For a simplified version of the HTML/CSS/JS code, you can check it out at: https ...

Initiate the countdown when the button is pushed

Recently ran into an issue where a button triggers a command to a Perl script, causing the page to continuously load for 60 seconds. To provide users with transparency on when the Perl script will be finished running, I implemented a JavaScript countdown t ...

Implementing a sorting mechanism for ajax data retrieval

Currently, I am using the code below to save HTML created with jQuery in a database and retrieve it later: $('div[read]').each(function(){ var kelas = $(this).attr('kelas'); $.post('admin.php',{kelas:kelas,id:id},func ...

How to ensure text wraps when resizing window in CSS? The importance of responsive design

UPDATE: I finally fixed my scaling issues by setting the max-width to 760px. Thank you for the tip! However, a new problem has emerged - now my navigation scales in the small window and I want it to remain a fixed size. Hello everyone! I am currently lear ...

What is the best method for showcasing two images side by side in a column layout?

I need to showcase 2 images in a row using AngularJS, with the next two images in the following row and so forth. Img1 Img2 Img3 Img4 This is my code: <section id="content"> <div class="content-wrap"> <div class="container clearfix "& ...

Tips for employing 'live CSS' in Rails

As someone who is new to Ruby on Rails, I am currently facing a challenge. I want to extract data from specific fields in my database and utilize them for styling purposes. For instance, within my 'Student' database, there are height and width f ...

The vertical menu was added, but the text did not align properly

I pasted a sidebar from a different source https://i.stack.imgur.com/jkYWz.png My goal is to have the text appear at the top of the page, similar to this example https://i.stack.imgur.com/1aR5s.png After adding this line, my text is displaying at the b ...

Is there a way to make a Bootstrap grid with four columns span across two rows in the second column?

How can I create a Bootstrap grid with four columns where the second column spans two rows? Hello, I am looking for help to achieve a portfolio grid layout with 4 columns (refer image here) and have the second column span 2 rows. I plan to include images ...

What is the best way to ensure the width of the div is adjusted to fit the table it contains?

What is the best way to adjust the width of a div containing a potentially wider-than-screen table? Despite setting padding: 10px for the div, the right padding disappears when the table exceeds the screen width. Below is an example code snippet: <div ...

Navigation is failing to float in the correct position

Having issues with my navigation positioning, specifically when it reaches the breakpoint. There's a strange gap on the right side that I can't seem to fix by adjusting padding or margin settings. I'm relatively new to this so please bear wi ...

Master the art of displaying complete text when zooming in and elegantly truncating it when zooming out

I am currently working on a data visualization project using d3.js. The tree chart that I have created is functioning well, but I would like the text to react dynamically when zooming in and out. You can find the code for my project on this JSFiddle page. ...

What is causing all webkit browsers to overlook the z-index in my code?

I have removed all unnecessary elements from my webpage to focus on reproducing a specific issue. HTML: <html lang="en-us"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initi ...

Tips for adjusting column sizes in react-mui's DataGrid based on screen size

I would like the title column to occupy 3/4 of the full row width and the amount column to take up 1/4 of the full row width on all screens sizes (md, sx...). Here is the updated code snippet: import React from 'react' const MyComponent = () =&g ...

What causes the child positioning to break when a CSS-Filter is applied to the parent element?

One of my projects includes a title-screen "animation" where the title is initially centered on a fullscreen page and then shrinks and sticks to the top as you scroll down. I have provided a simplified working example below: $(window).scroll( () => ...

JavaScript progress bar with extended duration, inspired by the success-oriented approach

Currently, I am in search of a unique solution or plugin that can replicate the same effect as Kickstarter's crowd fund long term progression bar. It seems like existing solutions only offer short term dynamic progress bars that indicate the percentag ...