Using a percentage width on a <div> element with the "overflow-x: scroll" property does not seem to be functioning properly, even when encapsulated within

My code includes a div within another div, taking up 50% of the page's width:

<div id="mainContainer" class="mainContainer">
    <div id="scroller" class="scrolling">
        <!-- items here should make the div really long -->
    </div>
</div>

The classes are configured as follows:

div.mainContainer{
    width:  50%;
    height: 50%;
}

div.scrolling{
    width:  50%;
    height: 10%;
    overflow-x: scroll;
}

This setup should display a horizontal scrollbar when the number of items in the 'scroller' div exceeds its width. However, I have encountered an issue where items wrap onto the next line and the div scrolls vertically instead.

When the 'scrolling' div contains <div> elements rather than <p> or text, all elements are displayed on one line in other scenarios. So, it seems that applying overflow: scroll to divs with percentage widths doesn't work as expected. I attempted wrapping the <div> inside a <span> element with a width of 100%, but this approach also failed.

I experimented with calculating and setting the width using JavaScript in pixels, yet this method did not resolve the issue either. Additionally, changing width to max-width did not yield any results.

If you have any suggestions or insights on how to achieve horizontal scrolling for this div, please let me know!

Answer №1

To achieve this, you may need to use the no-wrap feature and add borders for a clearer visual representation of the size.

div.mainContainer{
    width:  50%;
    height: 50%;
    border: 1px solid;
}

div.scrolling{
    width:  50%;
    height: 10%;
    overflow-x: scroll;
    border: 1px solid;
    white-space: nowrap;
}
<div id="mainContainer" class="mainContainer">
    <div id="scroller" class="scrolling">
        <div>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</div>
    </div>
</div>

Answer №2

Instead of using a percentage for the width, consider utilizing vw (viewport width) as a more versatile alternative. Take a look at this code snippet:

body {margin: 0;}
div {
    width: 100vw;
    height: 30px;
    background-color: red;
}
<div></div>

Answer №3

Here's a demonstration for you to check out. You'll notice that there is a paragraph element with horizontal scrolling, along with other elements. Edit I've added another element with text and an image that are also scrollable

div.mainContainer{
    width:  100%;
    height: 100%;
}

div.scrolling{
    max-width:  50%;
    height: 10%;
   overflow: auto;
}
p.text{
white-space: nowrap;
 overflow: auto;
}

#scroller>div{
overflow: auto;
  border: 6px solid red;
white-space: nowrap;
  overflow: auto;
}
<div id="mainContainer" class="mainContainer">
    <div id="scroller" class="scrolling">
        <p class="text">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In condimentum nisi nec diam convallis lacinia. Suspendisse feugiat tincidunt felis, eleifend finibus odio sodales tempor. Donec tempor diam sem, ac hendrerit orci ullamcorper quis. Ut tempus nec nunc at bibendum. Nulla vulputate nisl sit amet dapibus scelerisque. Nulla facilisi. Nam bibendum nec felis quis lobortis. Sed porta convallis sapien, ac fringilla quam maximus efficitur. Sed et mi ut lorem iaculis consectetur et et dui. Cras sit amet mi non augue viverra facilisis. Aenean pretium cursus consequat. Quisque cursus fringilla facilisis. Donec vel eros tellus. Nunc non nibh nibh. Sed pretium laoreet lectus eget blandit.
</p>
<div>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In condimentum nisi nec diam convallis lacinia. Suspendisse feugiat tincidunt felis, eleifend finibus odio sodales tempor. Donec tempor diam sem, ac hendrerit orci ullamcorper quis. Ut tempus nec nunc at bibendum. Nulla vulputate nisl sit amet dapibus scelerisque. Nulla facilisi. Nam bibendum nec felis quis lobortis. Sed porta convallis sapien, ac fringilla quam maximus efficitur. Sed et mi ut lorem iaculis consectetur et et dui. Cras sit amet mi non augue viverra facilisis. Aenean pretium cursus consequat. Quisque cursus fringilla facilisis. Donec vel eros tellus. Nunc non nibh nibh. Sed pretium laoreet lectus eget blandit.<br>
<img src="https://cdn.vox-cdn.com/thumbor/SOE2_mCo4BiW9ENumqhU220AEMk=/0x330:1577x1381/1200x800/filters:focal(0x330:1577x1381)/cdn.vox-cdn.com/uploads/chorus_image/image/33197419/122047293.0.jpg">
</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

React useEffect appended script not activating

I'm having trouble loading a script that should generate an iframe and display a weather widget upon trigger. I am able to append the script correctly, but it doesn't seem to be called and does not create the iframe. Even when I directly add the ...

Tips for saving information in an array with Vue.js?

I am working on a task where I need to fetch data from an API and store only the values that are marked as "true" in a variable. The API response is listed below: API Output Data { "BNG-JAY-137-003": false, "BNG-JAY-137-004": true, ...

Determine the number of input files in HTML5 when there is a change

I am working on a project that requires using the HTML5 File API to determine the number of files selected in an input file element and then displaying this count in another element. Here is the HTML code: <div class="form-row"> <label>Ma ...

Developing a versatile table component for integration

My frontend app heavily utilizes tables in its components, so I decided to create a generic component for tables. Initially, I defined a model for each cell within the table: export class MemberTable { public content: string; public type: string; // ...

Troubles with retrieving API search results using Vue computed properties

I've recently developed an Anime Search App using Vue.js and the new script setup. I'm currently struggling to access the search results in order to display the number of titles found. The app is fetching data from an external API, but it's ...

Resolving the Enigma: Querying jQuery for Real-Time Validation and

I'm fairly new to jQuery and I'm facing a challenge in my registration form script. Specifically, I want to check if the entered username or email is already taken while the user is typing. Currently, this functionality works by making a json req ...

The CSS styles are not recognized by the Windows 2003 server

After deploying my webapp, which was created using asp.net, to a testing server (Windows 2003 SP2, IIS6), I encountered an issue. When I accessed the default page, none of the CSS styles were applied. Only plain text or formatting from the .aspx file was v ...

Exploring the process of retrieving data from localStorage in Next.js 13

Having recently delved into the realm of Next JS, I've encountered a hurdle when it comes to creating middleware within Next. My aim is to retrieve data from local storage, but I keep hitting roadblocks. middleware.ts import { key, timeEncryptKey, to ...

Guide to building a seamless page without refreshes with the help of jquery, express, and handlebars

I need assistance with my express JS project. I am trying to set up two pages using NodeJS, incorporating handlebars as the template engine. My objective is to have the first page rendered using res.render('home'), and for the second page to be l ...

What impact does reselect have on the visual presentation of components?

I'm struggling to grasp how reselect can decrease a component's rendering. Let me illustrate an example without reselect: const getListOfSomething = (state) => ( state.first.list[state.second.activeRecord] ); const mapStateToProps = (state ...

Utilizing a class instance as a static property - a step-by-step guide

In my code, I am trying to establish a static property for a class called OuterClass. This static property should hold an instance of another class named InnerClass. The InnerClass definition consists of a property and a function as shown below: // InnerC ...

Cloning Div repeatedly instead of the specified quantity

My JSON data contains four sections and I want to clone my html div based on the number of sections. For example, if there are 100 sections in my JSON, I would like the div to be cloned 100 times. Currently, my div is getting cloned with the JSON data app ...

Header and Footer Components in ReactJS

My goal is to design a unique Layout component that will display the Header and Footer. This way, I can later use the Layout like <Layout> ... </Layout> In order to achieve this, I have utilized Routing in both the Header and Footer. To imple ...

Draggable resizing of the Accordion component in React.js using Material-UI

In the visual representation, there are two Accordions—one positioned on the left (30%) and the other on the right (70%). Upon clicking the button, the right accordion disappears, while the one on the left expands to cover the full width (100%). A featu ...

Configuring cloud code on Back4App to automatically trigger a POST API request to update the ESP

I am a beginner when it comes to developing APIs and cloud code, and I need help figuring out how to create an API that can add or update users in my back4app database table to my sendinblue (ESP) contact list. Could someone provide guidance on what shoul ...

Retrieving chosen items from NextUI-Table

I am completely new to JavaScript and NextUI. My usual work involves C# and DotNET. I have a requirement to create a table with selectable items, and when a button is clicked, all the selected items should be passed to a function on the server that accepts ...

Troubles encountered when cascading val(), text(), and data()

Here is my JavaScript/jQuery code: $select.append($("<option />") .val(this.id) .text(this.text) .data('name', this.name) .data('isstorage', this.isstorage)); Although it successfully assigns values to t ...

Guide for integrating the shadcn/ui Range Date Picker within a Form

Encountering an issue with using The Range Date Picker within the Form component. Specifically, I am looking to store {from, to} values of the range in an object, however, utilizing an object as a Form field value results in error messages not functioning ...

Tips for telling the difference between typescript Index signatures and JavaScript computed property names

ngOnChanges(changes: {[paramName: string]: SimpleChange}): void { console.log('Any modifications involved', changes); } I'm scratching my head over the purpose of 'changes: {[propName: string]: SimpleChange}'. Can someone cl ...

Troubleshooting: Why isn't my jQuery Click Event functioning in Mozilla but working perfectly fine in

While I know this question has been asked before, I can't seem to locate the posts I remember reading about it. I apologize for any duplication, but I am trying to implement a "click anywhere BUT here to close element overlay" effect on my personal we ...