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

Having trouble getting the on:dblclick event to trigger on a row within a list using S

I am currently using Sveltestrap and I am in need of a double click handler for a row: <ListGroupItem on:dblclick={handler(params)}> <Row> <Col><Icon name=........</Col> ... </Row> </ListGroupItem> Int ...

Angular UI grid: Arranging numbers in a straight line at the decimal point

I am interested in aligning decimal numbers in Angular UI Grid as shown below. 11.293 .89 233424 .34345 I have considered different approaches such as using a cell template with aligned divs or transparent 0s. Has anyone successfully imp ...

Ways to center a div with position:relative vertically on the page

What is the best way to center a position relative div vertically within its parent element? For example: <div class="parent"> <div style="position:relative;" class="child"> </div> </div> Any suggestions on how to vertic ...

Restrict the child's height to the remaining space within the parent container

I'm attempting to divide the viewport's height between two divs. In the top div, there is a small div followed by a larger div that should scroll if it doesn't fit within the remaining space of the parent div. Below is the structure of my HT ...

HTML5 for Bulk Video Upload

Is it possible to use the html5 api to record and upload video content directly from the browser? I am facing an issue in a project where the video recordings can be very long or large, causing extended upload times for users. Ideally, I would like the vi ...

There was a problem retrieving the product information from the API

I have been struggling to pinpoint the exact issue at hand. The foundation of HTML and CSS is pre-written, with JavaScript responsible for generating the core elements to populate the DOM. Within my script, I am attempting to retrieve product data in ord ...

Tip: Using jQuery to Wrap Characters Based on Count

Within my table, there is a specific cell in each row that contains exactly 13 characters. <td class="need-to-wrap">0123456789012</td> I am looking to wrap characters #10, 11 and 12 with a span element like so: <td class="need-to-wrap"> ...

How about determining the map size based on Divine proportions?

Is there a way to make the map at https://codepen.io/sinanelms/pen/MqdNNY responsive to screen size changes? I attempted the following code but was unable to achieve the desired outcome. <style type="text/css"> body{ background:#fff;} #map ...

The make-xx-column() mixins in Bootstrap are malfunctioning

After careful examination, it looks like some of the Bootstrap mixins are not functioning as expected. Specifically, the .make-md-column() and .make-sm-column() seem to have no effect at all. Initially, I suspected that WebEssentials was not compiling the ...

Alignment vertically in a Razor view

Here is a snippet of code I am working with: <p> @Html.LabelFor(m => m.address, new { style="vertical-align: middle;" }) @Html.TextAreaFor(m => m.address, new { @class = "addition ", value = "",rows="4", required = "required" }) </p> A ...

Show the HTML content fetched from the database using PHP

After pulling html source code from a database and storing it in a PHP variable, I noticed that the content consists of table rows structured like this: <tr><td>10:00 AM</td><td class="success">Available</td></tr> < ...

Unable to navigate to specific tab in Laravel 6 navigation tabs using URL parameter (e.g. myweb/pages#tab1)

I'm having trouble navigating to the tabs pages, they only show up in the URL (myweb/pages#tab1 .. myweb/pages/#tab2 ..). Can someone please help me out? View image <div class="card-body"> <ul class="nav nav-tabs" id="myTab" role="tabli ...

Triggering a JavaScript function when the mouse moves off an element

Recently, I created the following code snippet for marquee effect. The main functionality it is missing is triggering a function on mouse-over. <marquee class="smooth_m" behavior="scroll" direction="left" scrollamount="3"> <span style="float:le ...

Hover effect on two divs changing states

I was so close to solving this issue, but I'm stuck on the final part. Here's the code snippet I've been working on: /* valuecanvas */ #wrappercs { margin-top: 3px; width: auto; height: auto; display: block; float: right; bac ...

What's the best way to ensure an endless supply of copied elements with identical classes to the clipboard?

Here is a snippet of my HTML code: <Div Class='POSTS'> <Div Class='POST'> <Div Class='CONTENT'>Text1</Div> </Div> <Div Class='POST'> <Div Class=&apos ...

Looking for browser prefixes for the `text-align` property? We've got you covered! Use `-webkit-right` for Google Chrome and Safari, `-moz-right` for Firefox, and `-o-right` for Opera. But what about

When it comes to browser prefixes or hacks, we often think of: (for Google and Safari) text-align: -webkit-right; (for Firefox) text-align: -moz-right; (for Opera) text-align: -o-right; But what about Internet Explorer? I'v ...

Display all elements on a webpage using Javascript without the need for scrolling

I'm looking for a way to ensure all items on a webpage load immediately, without having to scroll down multiple times before running a script to find a specific item. Is there a method to have all items load at once without the need to manually scroll ...

Attempting to implement a disappearing effect upon submission with the use of JavaScript

I am currently working on implementing a disappearing form feature for a small web application. The idea is that once the initial form is submitted, it will be replaced by a new form. While my current code somewhat works, it only lasts for a brief moment b ...

What could be preventing this src tag from loading the image?

Here is the file structure of my react project Within navbar.js, I am encountering an issue where the brand image fails to load from the Assets/images directory. import '../../Assets/Css/Navbar.css'; import image from '../../Assets/Images/a ...

Is it possible to incorporate half an image into a slideshow using html and css?

Hello, I need help modifying this code: <div> <div style="width:100%"> <img src="images/o.jpg" alt="" style="width:100px;height:100px;justify-content: center;padding-left:50%" /> </div> <div id="flipbook"> ...