Adjust the inner div's width within the scrollable element to match the full width of the scrollable area

How can I ensure the inner content div's width matches that of the scrollable area?

<div class="scrollable">
    <div class="content">short</div>
    <div class="content">very looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong text</div>
</div>

Use this CSS:

.scrollable {
    width: 300px;
    height: 300px;
    overflow: auto;
}

.content {
    background-color: lightblue;
    white-space: nowrap;
}

View the example on jsFiddle: http://jsfiddle.net/XBVsR/12/

ISSUE: The background does not extend all the way horizontally when scrolling.

I have tried setting width: 100%, overflow: visible, but haven't been successful.

UPDATE: Clarification added - I do not want the text to wrap, instead, I want horizontal scrolling for the entire content.

Answer №1

If you want to style nested divs, try using the CSS property display: table-row;. Check out this example on jsfiddle

Answer №2

simply provide this for your content

 overflow: auto;

Answer №3

If you want the outer div to have a width of 300px, you will not be able to break the text properly within that box. To enable word wrapping, you can simply include the following code in your content CSS:

word-wrap: break-word;

Alternatively, if the width of 300px is not important to you, you can transfer the overflow: auto from the scrollable class to the content class, which should resolve the background issue as well.

Answer №4

Include in the .content style

overflow: auto;

Check out the updated fiddle at: http://jsfiddle.net/XBVsR/11/

Keep in mind that this will impact all elements with the .content class. Consider assigning a different class name as well.

Answer №5

To optimize the layout, consider using a structure with two nested inner div elements rather than just one.

The outer nested div should be styled with display: table, min-width: 100%, and min-height: 100%. The innermost nested div should have the property display: table-cell.

This setup enables the table to expand horizontally and vertically within the scrollable container's viewport based on the content size. Additional padding can also be applied to the innermost div.

.outer {
  width: 200px;
  height: 100px;
  overflow: auto;
}

.middle {
  background: lightpink;
  display: table;
  min-width: 100%;
  min-height: 100%;
}

.inner {
  display: table-cell;
  white-space: pre-line;
  padding: 10px;
}
<div class="outer">
  <div class="middle">
    <div class="inner">loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
and another text here
...
    </div>
  </div>
</div>

<hr>

<div class="outer">
  <div class="middle">
    <div class="inner">short
    short
    ...
    </div>
  </div>
</div>

<hr>

<div class="outer">
  <div class="middle">
    <div class="inner">short</div>
  </div>
</div>

View the example on JSFiddle

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

How can I trim a value using one-way data binding in Angular?

Is there a way to trim values in Angular using one-way data binding? Can this be done directly in the component.html file rather than in typescript? I tried the following but it didn't work: <P>{{country.trim()}}</P> Thanks ...

Is it possible to turn off the fallback color for bourbon radial gradients?

Is there a way to prevent the fallback from being applied when using the radial gradient mixin in Bourbon? I've consulted the documentation at , which states that the fallback is optional. However, no matter what I attempt, it seems that the primary ...

A checkbox placed within an anchor tag

Here is some code that I have: <a href class="dropdown-toggle"> <input type="checkbox" ng-model="test.checked"> <span class="fa fa-angle-down"></span> </a> When I click on the above code, I want the chec ...

Building a geometric slider using HTML5, CSS3 and jQuery

Currently, I am attempting to construct a triangular slider that resembles the one shown in the example: While I can generate the triangle visually, the technical aspects like enabling node dragging, restricting its movement within the triangle boundaries ...

The mobile menu functions correctly on Jfiddle but is not functioning on the local server

I was working on a responsive mobile menu and used a toggleClass function to open the menu. It's functioning correctly in Jfiddle and below, but every time I click the nav icon in the live preview within brackets, nothing happens. I even tried it in a ...

The height of scrollbars in the HTML section does not extend to full coverage

On my HTML webpage, I am facing an issue with the "left-section" scrollbar. It does not cover the entire height of the section. The scrollbar only appears when the window is resized small enough to reach "Link 18". Even then, the scrollbar does not scroll ...

Issues with HTML5 file uploads causing delays in iOS Safari mobile browsers

I developed a simple HTML5 file upload script using $.ajax(). I also attempted to use xhr, but encountered the same issue. The script successfully uploads an image to imgur.com and works flawlessly on desktop browsers as well as iOS Safari. However, in iOS ...

The Bootstrap select feature does not override the original dropdown menu; instead, it adds its own functionality alongside it

I have implemented a select combo using this library. After adding the necessary CSS and JS files, I encountered the following result. Here is what I have attempted so far: <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.mi ...

How come the words are clear, but the background remains unseen?

My markup looks like this: <div class="one">Content</div> <div class="two"></div> I have the following CSS styles: .one, .two { height: 20px; } .one { background-color: #f00; } .two { background-color: #0f0; margi ...

Customize Your Chartjs Background Color Settings

I'm working on an angular application that includes a chartjs line graph, and I'm trying to get the line chart to shade the background red for negative numbers. The issue I'm facing is figuring out how to calculate the bottom of the chart. ...

How to use jQuery to target every anchor element within an unordered list

I am trying to manipulate an unordered list structure by changing the class of the anchor tags when a specific link is clicked. For example, when the link in List Item 1 is clicked, I want to change the class of all links underneath List Item 1 to "yes". ...

What are the steps to remove the border from this table?

The alignment of the First Name and Textfield in the image above appears to be off from the left. It's unclear if it's due to a border or padding issue. The conditionally visible image inside complicates things. Is there a way to resolve this usi ...

What is the best way to close a popup window?

Is there a way to close my popup? function hidePopup(){ document.getElementById("popup-1").classList.remove("active"); } @import url('https://fonts.googleapis.com/css2?family=Source+Sans+Pro&display=swap'); .popup .overlay{ positio ...

Transforming images with Imagick

I've been trying to generate thumbnails from PDF uploads using Imagick. I have a script that is supposed to handle this task, but unfortunately, it only uploads the file without creating a thumbnail. I know some of you may find this basic, but PHP is ...

Incorporate CSS and JavaScript styling into your Android application

I'm currently working on developing an Android app that will display a countdown for an upcoming event. I have utilized HTML, CSS, and JavaScript to create the functionality, which works perfectly fine when tested in a regular HTML environment. Howeve ...

What is the reason behind the change in size and shape of the text when using "overflow: hidden"?

Check out this interactive demo using the provided HTML and CSS: You can view the demo by clicking on this link. /*CSS*/ .header { margin: 0px; padding: 0px; width: 100%; } .headertable { padding: 0px; margin: 0px; width: 100%; border-sp ...

Having issues with React Nivo tooltip functionality not functioning properly on specific graphs

While using Nivo ResponsivePie to visualize some data, everything is functioning properly except for the tooltip. For some reason, the tooltip isn't appearing as it should. Interestingly, I have a heatmap and a bar graph with tooltips that are working ...

Thumbnail Centering Issue in Swiper Plugin by iDangero.us

Currently, I am utilizing the Swiper plugin created by iDangero.us. Within my layout design, there are 6 slider images accompanied by 6 thumbnails. Surprisingly, when I attempt to center align the thumbnails, it appears to cause issues with the plugin. Th ...

JavaScript can be used to emphasize text that a user has selected on a

One challenge I am currently facing is the highlighting of selected text. We are attempting to extract the DOM elements of the selected text using window.getselction and enclose them within a <span> with styling to apply a background color. While thi ...

Utilizing Ant Design Icons without an Internet Connection

In my current project, a reactJS application utilizing ant design for the UI was recently deployed to production on locked down computers. These computers lack internet access, causing ant design icons on modals to appear as empty boxes. Upon investigation ...