Is there a way to enable horizontal scrolling exclusively for a row of images, displaying overflow, without affecting the horizontal scrolling of the rest of the page?

I am working on a page layout where the body is 960px wide and I want to have a row of images overflow out of the right side of the page, with the ability to horizontally scroll to see the rest of the images. The challenge I'm facing is getting the overflowed images to display without increasing the width of the entire page and causing it to scroll horizontally.

My CSS setup:

.container{
  width:960px;
  margin: 0 auto;
}

.pic-container {
  white-space:nowrap; 
  overflow:scroll;
}

My HTML structure (simplified):

<body>
<container>
  <div class="pic-container">
    <div class="pic-row">
      <img src="1.jpg">
      <img src="2.jpg">
      <img src="3.jpg">
      <img src="4.jpg">
      <img src="5.jpg">
      <img src="6.jpg">
    </div>
  </div>
</container>
</body>

This is the desired layout:

Answer №1

If you're looking for a solution, this might be what you need (check it out on JSFiddle):

<section>
  <div class="pic-container">
    <div class="pic-row">
      <img src="1.jpg">
      <img src="2.jpg">
      <img src="3.jpg">
      <img src="4.jpg">
      <img src="5.jpg">
      <img src="6.jpg">
    </div>
  </div>
</section>​

CSS:

body {
   overflow: hidden;
}
section {
  /* Set the width of your document */
  width:600px;
  margin: 0 auto;
  white-space:nowrap; 
  overflow: scroll;
}
.pic-container {
 /* Adjust width as needed */
  width: 1500px;
}​

This approach hides the overflow of the body, preventing horizontal scrollbars even if the page isn't wide enough. It then enables scrollbars on the container div with a set width (likely the document width), making the content inside that div as wide as desired.

An alternate implementation, setting the width of the section instead of the body, can be seen here.

Answer №2

After struggling to find a solution using only HTML and CSS, I turned to JQuery for help. Surprisingly, the solution works flawlessly and gracefully degrades in the absence of JavaScript. Even without JavaScript, the images remain scrollable, but they just don't extend beyond the right margin (the '.pic-container' div is initially width-less until JavaScript assigns it).

// Adjust pic-container width
function adjustPicWidth() {
  let windowSize = $(document).width();
  let newWidth = String(Math.round(((windowSize - 960) / 2) + 960));

  $('.pic-container').css({'width': newWidth});
}
$(window).resize(adjustPicWidth);
adjustPicWidth();

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

The picture won't stay within the confines of the container

As I work on my fitness site, I wanted to incorporate a sponsor section. While exploring some code that matched the style of my website, I discovered this snippet below. However, being new to CSS, I struggled with fixing it to ensure that the images fit ...

How to extract hover CSS property from a Selenium WebElement using XPath in Python

Is there a way to detect if a button changes its background color on hover? I know how to get the cursor property: print webElement.value_of_css_property('cursor') But I am unsure how to capture the hover property. ...

Creating a shadow effect within a CSS mask: A simple guide

Is it possible to create a shadow within an SVG mask cutout? Here is the code snippet I am currently using index.html <div class="content"> <section id="home-section"> </section> ... </div> styles. ...

Linking two div elements together with a circular connector at the termination point of the line

I am currently working on designing a set of cards that will showcase a timeline. I envision these cards to be connected by lines with circles at each end, for a visually appealing effect. At the moment, I have created the cards themselves but I am struggl ...

Is it possible to launch an Electron application by clicking a run button in the VSCode

I'm new to using VSCode and am currently working on an HTML5 app with Electron. I'm finding it cumbersome to switch between windows and enter a command each time I want to test my application. Is there a way to configure VSCode to run my Electron ...

Displaying elements in a Django app using React JS is limited to rendering on a single HTML page within the app's DOM

I've encountered an issue with my Django app that utilizes ReactJS for the frontend. Within my project, I have two HTML files and one App.js file containing two classes. The problem I am facing is that React only renders the elements from the first ...

Optimal method for showcasing a multitude of columns on an HTML page

Seeking to showcase a vast array of data in HTML (over 10,000 columns with approximately 200-300 rows) to present a schedule. The information will be structured as movable blocks containing text and background colors. Is it feasible to exhibit such a mas ...

Updating the database with PHP and MYSQL to display a zero value in a specific field

I am facing a problem with updating a specific row in the database. The update process seems to be working, but there is an issue with one of the fields not updating properly. When checking the database after updating, it shows as 0, and I cannot pinpoint ...

Painting Magic: Exploring the World of Canvas Zoom and Moves

I'm having trouble implementing zoom and pan functionality for this particular canvas drawing. While there are examples available for images, my case is different since I am not working with images. Any tips or suggestions on which libraries to use wo ...

Responsive design with Semantic UI

Can you please provide an example of how to design for different screen sizes using Semantic UI instead of Bootstrap? I'm looking for something similar to Bootstrap's sm and lg classes. For instance, would it look like this: <div class="col s ...

What are the steps to incorporate an iframe containing uniquely designed XML content?

I'm currently working on a website project for a client who specifically wants to include news from the BBC. Despite spending 3 hours searching online, I haven't been able to successfully insert an XML file into an iframe and style it to the clie ...

Make sure to blur an editable p element using only vanilla JavaScript

Check out this code snippet: <p contenteditable>The most amazing p tag in the world of p tags</p> Here is the corresponding Javascript: var p = document.querySelector('p'); p.addEventListner('keypress', function (e) { ...

Personalize Autocomplete CSS based on the TextField input in React Material UI when a value is present

In my current project, I am utilizing React Material Autocomplete fields, which includes a nested TextField. I have successfully implemented standard styles for when the field is empty and only the label is visible, as well as different styles for hover ef ...

@media query not functioning properly with certain rules

I've hit a roadblock because my @media query is functioning properly for the initial rule, but fails to work for subsequent rules. For instance, I managed to make the "subButton" disappear when the screen size decreases below the specified width. How ...

Fluid Cursor Movement in the Atom Text Editor

I encountered an issue where adding a smooth caret animation in the atom editor caused the background to start blinking uncontrollably. Below is the code snippet I used to add the animation in my style.less file: atom-text-editor .cursor { transition: ...

Update the contents of a different element upon hover using dynamic PHP functionality

Often, solutions involving the use of java script/jquery to achieve a specific behavior require knowing the exact id of the element. However, in dynamic situations where the id is not always known, it can pose a challenge. In my case, I'm generating a ...

Webkit feature: Rounded border image overlay

One issue arises when applying a rounded border to an image in webkit browsers as the border ends up hidden behind the image. CSS img { border: 10px solid #000; border-radius: 100%; } HTML <img src="http://25.media.tumblr.com/tumblr_mbje ...

Create a scrollable div using the flex-grow-1 class in Bootstrap 5

I'm having trouble making the div below scrollable. Instead of producing a scrollbar, it keeps expanding the document size beyond the screen space. I know I could set a fixed pixel height for the div, but I want to make sure it scales well across diff ...

How can you enhance the visibility of AngularJS Materials dropdown menus?

While experimenting with AngularJS Materials, I noticed that the text in some elements like <md-input-container> and <md-select> may be too light for some people to read easily. I wanted to find a way to make the text more visible without chang ...

Creating manageable CSS styles for a wide variety of themes

I need to add themes to a web application that allows users to switch between them seamlessly. The designers want a variety of font colors and background colors, around 20 in total. Is there a way to achieve this without creating multiple .css files, which ...