The horizontal scrolling with overflow-x is not functioning correctly as it is displaying the scrollbar for the vertical

I am perplexed as to why I am unable to scroll horizontally to view the other pink thumbs. Despite adding an overflow of scroll-x to the thumbs container, which should theoretically allow horizontal scrolling, it only seems to scroll vertically.

Could someone shed some light on why horizontal scrolling is not functioning in this case? Many thanks in advance!

#content-wrap {
  background: lightgreen;
  width: 300px;
  height: 300px;
}
#main-image {
  background: cyan;
  width: 300px;
  height: 250px;
  float: left;
}
#thumbs-wrap {
  background: orange;
  width: 300px;
  height: 50px;
  float: left;
  overflow-x: scroll;
}
.thumb {
  background: pink;
  box-sizing: border-box;
  width: 75px;
  height: 50px;
  border: 2px solid grey;
  float: left;
}
<div id="content-wrap">
  <div id="main-image"></div>
  <div id="thumbs-wrap">
    <div class="thumb"></div>
    <div class="thumb"></div>
    <div class="thumb"></div>
    <div class="thumb"></div>
    <div class="thumb"></div>
    <div class="thumb"></div>
  </div>
</div>

Answer №1

It appears that the issue is due to the fixed width of your #thumbs-wrap, which is not sufficient to accommodate all child elements in a single row or enable horizontal scrolling. One simple solution is to wrap all child elements within another div and assign it additional width. Here's an example:

#content-wrap {
  background: lightgreen;
  width: 300px;
  height: 300px;
}
#main-image {
  background: cyan;
  width: 300px;
  height: 250px;
  float: left;
}
#thumbs-wrap {
  background: orange;
  width: 300px;
  height: 50px;
  float: left;
  overflow-x: scroll;
}
#thumbs-inner-wrap {
  width: 1000px;
}
.thumb {
  background: pink;
  box-sizing: border-box;
  width: 75px;
  height: 50px;
  border: 2px solid grey;
  float: left;
}
<div id="content-wrap">
  <div id="main-image"></div>
  <div id="thumbs-wrap">
    <div id="thumbs-inner-wrap">
      <div class="thumb"></div>
      <div class="thumb"></div>
      <div class="thumb"></div>
      <div class="thumb"></div>
      <div class="thumb"></div>
      <div class="thumb"></div>
    </div>
  </div>
</div>

Answer №2

When working with HTML elements, they will naturally wrap to new lines if there is no more horizontal space available. This can result in a vertical scrollbar appearing even when setting overflow-x: auto. However, using CSS flexbox allows you to override this behavior without the need for additional elements. Simply add the following snippet to your CSS:

#thumbs-wrap {
    display: flex;
    flex-wrap: nowrap;
}
.thumb {
    flex: 0 0 auto;
}

But how does this actually work?

The flex-wrap:nowrap property ensures that child elements remain on a single line and do not wrap to multiple lines.

In standard situations, flex items may resize to fit their parent container's width. By setting flex-grow and flex-shrink to 0, we prevent them from expanding or shrinking based on available space.

The flex property's shorthand values dictate the item's growth, shrinkage, and basis. Setting it to 0 0 auto enforces the original sizing specified in our CSS.

To summarize, these rules ensure that the flex items (.thumb) align horizontally, avoid wrapping, and maintain their initial size. As a result, a horizontal scrollbar will be visible on the #thumbs-wrap element.


You can observe this behavior directly alongside your code:

#content-wrap {
  background: lightgreen;
  width: 300px;
  height: 300px;
}
#main-image {
  background: cyan;
  width: 300px;
  height: 250px;
  float: left;
}
#thumbs-wrap {
  background: orange;
  width: 300px;
  height: 50px;
  float: left;
  overflow-x: scroll;
  display: flex;
  flex-wrap: nowrap;
}
.thumb {
  flex: 0 0 auto;
  background: pink;
  box-sizing: border-box;
  width: 75px;
  height: 50px;
  border: 2px solid grey;
  float: left;
}
<div id="content-wrap">
  <div id="main-image"></div>
  <div id="thumbs-wrap">
    <div class="thumb"></div>
    <div class="thumb"></div>
    <div class="thumb"></div>
    <div class="thumb"></div>
    <div class="thumb"></div>
    <div class="thumb"></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

Carousel Image Alignment Issue on iPhone SE Portrait Mode: All Images Centered Except One

Check out this website: It functions perfectly on Desktop browsers at all scales and works on Mobile devices in Landscape mode. However, I noticed that when using Portrait mode on my iPhone SE, the first image in the carousel appears off center in Chrome ...

Textbox textchange event triggers Javascript function when Checked=True

$('.LblQTY').on('input', function () { var currentRow = $(this).closest('tr'); var rate = parseFloat(currentRow.find('.LblRate').text()); var quantity = parseFloat($(this).val() == '' ? '0& ...

Cross-Origin Resource Sharing (CORS) for HTML

Here is a code snippet I found on http://jakearchibald.com/scratch/alphavid/ $("#video").append('<video id="movie" style="display:none" autobuffer><source id="srcMp4" src="https://host-away-from-host.com/file.mp4" type=\'video/mp4; ...

minimize the size of the image within a popup window

I encountered an issue with the react-popup component I am using. When I add an image to the popup, it stretches to full width and length. How can I resize the image? export default () => ( <Popup trigger={<Button className="button" ...

Utilizing Jquery's Selectable IDs in a repetitive manner

I'm a newbie to jQuery and I'm attempting to create two lists where each item can be selected. Check out my code: <!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>jQuery UI s ...

Dynamic content displayed within adjacent div elements

I am currently working on a project where I am dynamically generating an outline by creating panels and labels in ASP.NET. Each node in the outline is defined by an outline number and outline text, which are obtained from a database that defines the relati ...

Applying Tailwind styles to dynamically inserted child nodes within a parent div

Currently, I am in the process of transitioning my website stacktips.com from using Bootstrap to Tailwind CSS. While initially, it seemed like a simple task and I was able to replace all the static HTML with tailwind classes successfully. However, now I ha ...

Does the padding and margin of an element alter when the position is set to relative?

By utilizing relative positioning, an element can be moved in relation to its original position in normal flow. - Citation from the book "HTML&CSS: design and build websites" by John Duckett If an element has a relative position property, it opens up ...

The page remains static even as the function is executed

Having trouble with creating a sliding form using jQuery. The issue is that instead of sliding to the desired page, it slides to a blank one. The website where this problem can be seen is www.entrilab.com. Upon inspecting the element, there are no errors ...

Transfer data from href link to specific detail page

I am currently working on a table that displays all the users. My goal is to turn the usernames into clickable links that direct you to their profile page. What steps should I take to convert the username into an href link and send it to the detail page? ...

Open the navigation menu by clicking on either the navigation links or anywhere outside of it

Seeking a solution for my mobile navigation menu that closes when clicking outside the links or on one of them, without using jQuery. How can I achieve this with JavaScript ES6? Current code snippet: const navSlide = () => { const burger = docum ...

Is there a way to show uppercase values in Angular's tooltip as capital letters?

Here is my HTML code: <i [class]="item.icon" [pTooltip]="item.item" tooltipPosition="bottom" ></i> The value inside item.item is 'ATTACHMENT' and I am unable to modify it from the ts file. Is there a way ...

Update the contents of a div element with JavaScript and PHP combo

When I click on the following div, I want to replace it with a similar one from file.php. The div tag in question is: <div style="display: none;" id="extra" class="container2" xml:id="page"> <div class="title"> <h2>Stuff to check out< ...

What is the best way to align a form in the center of a page both horizontally and vertically

Currently, this is the HTML code I am working with: <body> <form id="form_login"> <p> <input type="text" id="username" placeholder="username" /> </p> ...

Error loading custom Javascript in MVC 4 view during the first page load

I'm working on an MVC 4 application that utilizes jQuery Mobile. I have my own .JS file where all the functionality is stored. However, when I navigate to a specific view and check the page source, I notice that all scripts files are loaded except fo ...

How to place a circle below text using CSS

I'm attempting to center a 5px x 5px circle under the links in a navigation bar to indicate the current page, but I'm uncertain about how to achieve this effect. Here is the current outcome: Link to Image This is what I aspire to accomplish: Li ...

Optimized layout for screen resolutions exceeding 1000 pixels wide

As I work on making my website responsive using Dreamweaver CC, I'm encountering a problem with screen sizes larger than 1000px. In Dreamweaver, there are three options for responsive web design at the bottom: desktop 1000px and less, tablet 768px an ...

Menu Overlay (jQuery/CSS)

I am in the process of developing a website that is optimized for both mobile and desktop devices. I am currently brainstorming ideas for the main navigation, which I envision as an overlay that slides down similar to . However, I am struggling to create ...

Animating components when they first mount in React

I'm attempting to implement an onMount animation in React (and Tailwind if relevant). The code I currently have is as follows: const Component = () => { const [mounted, setMounted] = useState(false) useEffect(() => { setTimeout(( ...

tips for passing value to the date field using proctractor

This is the HTML code I am working with: <input id="filter-datepicker" type="daterange" ranges="ranges" class="form-control date-picker" placeholder="Select Date Range" name="sensorDetails.date" ng-model="myDateRange" ranges="ranges" requi ...