Dynamic image gallery with a flexible layout and interactive scrollbar using Flexbox

I'm looking to incorporate a scrolling image gallery into a flexbox layout so that as the browser page size changes, the gallery will scale according to the flex-grow index. My goal is to ensure all images remain accessible via a scrollbar without using absolute units. Despite attempting various methods recommended on forums and websites, I have been unsuccessful in getting the gallery to function within my flexbox layout. Any suggestions would be greatly appreciated.

Example1: The desired behavior of the image gallery when the browser page size is smaller than the displayed images:

<!DOCTYPE html>
<html>
    <head>
        <style>
            html, body {
                background-color: dimgray;
                height: 100%;
                overflow: hidden;
                margin: 0;
                padding: 0;
            }
            .gallery {
                flex: 1 1 50%;
                white-space: nowrap;
                overflow: scroll;
            }
        </style>
    </head>
    <body>
        <div class="gallery">
            <img src="https://via.placeholder.com/100x100" alt="Tile1">
            <img src="https://via.placeholder.com/100x100" alt="Tile2">
            <img src="https://via.placeholder.com/100x100" alt="Tile3">
            <img src="https://via.placeholder.com/100x100" alt="Tile4">
                                ...
        </div>
    </body>
</html>

Example2: Flexbox layout where the image gallery should be integrated:

<!DOCTYPE html>
<html>
    <head>
        <style>
            html, body {
                background-color: dimgray;
                height: 100%;
                overflow: hidden;
                margin: 0;
                padding: 0;
            }
            .container {
                display: flex;
                flex-direction: column;
                height: 100%;
            }
            /************************/
            .column {
                display: flex;
                flex-flow: column nowrap;
                border: 1px solid black;
                padding: 10px;
            }
            .column:nth-child(1) {
                flex: 4 0 40%;
            }
            .column:nth-child(2) {
                flex: 3 0 30%;
            }
            /************************/
            .row {
                display: flex;
                flex-direction: row;
                flex-flow: row nowrap;
            }
            .row:nth-child(1) {
                flex: 1;
            }
            /************************/
            .sub-row {
                border-bottom: 1px solid black;
                padding: 5px 0;
                color: white;
            }
            .sub-row:last-child {
                border-bottom: none;
            }
            /************************/
            #row-middle-col-left1,
            #row-middle-col-left2 {
                flex-grow: 4;
            }
            #row-middle-col-right1 {
                flex-grow: 10;
            }
            #row-middle-col-right2 {
                /*max-width: 800px;*/
            }
            /************************/
            .gallery {
                flex: 1 1 50%;
                white-space: nowrap;
                overflow-x: scroll;
                min-width: 0;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="row" id="row-middle">
                <div class="column" id="row-middle-col-middle">
                    <div class="sub-row" id="row-middle-col-left1">M1</div>
                    <div class="sub-row" id="row-middle-col-left2">M2</div>
                </div>
                <div class="column" id="row-middle-col-right">
                    <div class="sub-row" id="row-middle-col-right1">R1</div>
                    <div class="sub-row" id="row-middle-col-right2">R2
                        <div class="gallery">
                            <img src="https://via.placeholder.com/100x100" alt="Image1">
                            <img src="https://via.placeholder.com/100x100" alt="Image2">
                            <img src="https://via.placeholder.com/100x100" alt="Image3">
                            <img src="https://via.placeholder.com/100x100" alt="Image4">
                                            ...
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

Answer №1

When looking for a solution, I came up with this approach: I fixed the size of the gallery and then deducted it from the flex-basis:

#row-middle-col-right2 {
    display: inline-flex;
    flex-flow: row nowrap;
    flex: 1 1 (10% - 500px);
    overflow-x: scroll;
}
.gallery {
    display: flex;
    flex-flow: row nowrap;
    width: 500px;
}

The complete code is as follows:

<!DOCTYPE html>
<html>
    <head>
        <style>
            HTML, body {
                background-color: dimgray;
                overflow: hidden;
                height: 100%;
                width: 100%;
                margin: 0px;
                padding: 0px;
            }
            .container {
                display: inline-flex;
                height: 100%;
                width: 100%;
            }
            .column {
                display: inline-flex;
                flex-flow: column nowrap;
                border: 1px solid black;
                padding: 10px;
            }
            .column:nth-child(1) {
                flex: 2 2 20%;
            }
            .column:nth-child(2) {
                flex: 3 3 30%;
            }
            .row {
                display: inline-flex;
                flex-flow: row nowrap;
            }
            .row:nth-child(1) {
                flex: 1 1 100%;
            }
            .sub-row {
                display: inline-flex;
                flex-flow: row nowrap;
                border-bottom: 1px solid black;
            }
            .sub-row:last-child {
                border-bottom: none;
            }
            #row-middle-col-left1,
            #row-middle-col-left2 {
                display: flex;
                flex: 2 2 20%;
                flex-flow: row nowrap;
            }
            #row-middle-col-right1 {
                display: flex;
                flex-flow: row nowrap;
                flex: 6 6 60%;
            }
            #row-middle-col-right2 {
                display: inline-flex;
                flex-flow: row nowrap;
                flex: 1 1 (10% - 500px);
                overflow-x: scroll;
            }
            .gallery {
                display: flex;
                flex-flow: row nowrap;
                width: 500px;
            }

            .gallery img {
                white-space: nowrap;
                padding: 10px;
            }
        </style>
    </head>
    <body>
        <!-- Not working like expected-->               
        <div class="container">
            <div class="row" id="row-middle">
                <div class="column" id="row-middle-col-middle">
                    <div class="sub-row" id="row-middle-col-left1">M1</div>
                    <div class="sub-row" id="row-middle-col-left2">M2</div>
                </div>
                <div class="column" id="row-middle-col-right">
                    <div class="sub-row" id="row-middle-col-right1">R1</div>
                    <div class="sub-row" id="row-middle-col-right2">R2
                        <div class="gallery">
                            <img src="https://via.placeholder.com/100x100" alt="Image1">
                            <img src="https://via.placeholder.com/100x100" alt="Image2">
                            <img src="https://via.placeholder.com/100x100" alt="Image3">
                            <img src="https://via.placeholder.com/100x100" alt="Image4">
                            <img src="https://via.placeholder.com/100x100" alt="Image5">
                            <img src="https://via.placeholder.com/100x100" alt="Image6">
                            <img src="https://via.placeholder.com/100x100" alt="Image7">
                            <img src="https://via.placeholder.com/100x100" alt="Image8">
                            <img src="https://via.placeholder.com/100x100" alt="Image9">
                            <img src="https://via.placeholder.com/100x100" alt="Image10">
                            <img src="https://via.placeholder.com/100x100" alt="Image11">
                            <img src="https://via.placeholder.com/100x100" alt="Image12">
                            <img src="https://via.placeholder.com/100x100" alt="Image13">
                            <img src="https://via.placeholder.com/100x100" alt="Image14">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </body>
</html>

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

Tips for displaying only one image when clicked and hiding other divs:

Currently tackling a project that involves JavaScript and I've hit a roadblock. Before you dive into the text, take a look at the image. Check out the picture here. I have successfully created a slider, but now I want to implement a feature where cli ...

Menu bar placed atop a carousel

I am currently in the process of developing a website and I have encountered an issue with the navigation bar. I have a slider that I would like to appear below the navbar but the navbar is pushing it down instead. Ideally, I want the navbar to be transpar ...

Customize the design of a React Material-UI select dropdown by overriding

I am currently utilizing the React MUI Select Component for my forms, specifically for language selection. My issue arises when I need a different style for the dropdown menu of the language Select component. Overriding the relevant class would apply this ...

Utilize orientation detection technology to ascertain the exact placement of an image

On my HTML page, I have a centered header (h1) with a title that displays in portrait mode above an image. When switching to landscape orientation, I want the image to move to the left side of the title. I've experimented with using <br> along ...

The essential criteria for script tag and page validation requirements

There are instances where I have pages that contain only a script without any content (such as sending data through postMessage and then closing itself). In these cases, is the page considered valid with just <script>doSomeStuff</script> or do ...

Is the parameter retrieval method correct or erroneous?

I am looking to retrieve data by clicking a link. Here are the links available: <a class="note" id="' . $data["p_id"] . '" value="1" href="javascript:void(0)">+1</a> <a class="note" id="' . $data["p_id"] . '" value="-1" ...

Why is there an excessive amount of white space on my Reactjs page?

This webpage (page URL -) displays well without any white space between two images when viewed on a laptop. However, when accessed on a mobile device, it appears to be taking up too much space as shown in the image below. Image of webpage on laptop- lapto ...

having trouble with changing the button's background color via toggle

I've been experimenting with toggling the background color of a button, similar to how changing the margin works. For some reason, the margin toggles correctly but the button's color doesn't. <script> let myBtn = document.querySele ...

The issue of background and footer distortion arises in PhoneGap when the keyboard is opened

Currently, I am experiencing screen problems with phonegap. The issue arises when a keyboard is opened, causing the back button at the bottom of the page to move above the keyboard and resulting in the background image appearing shorter. How can this be re ...

Removing the currently selected item from the OwlCarousel

I am trying to enable users to delete the item that is currently active or centered in an OwlCarousel. I have come across some code related to deleting items, but it seems to be a rare question: $(".owl-carousel").trigger('remove.owl.carous ...

Tips for extracting and utilizing a JSON object provided by the parent page:

Sorry in advance if this question has already been addressed. I've spent hours searching on Google, but haven't found a satisfactory answer yet. Below is the code snippet I'm working with: <ion-content> <div class="list"> ...

Which method is better for HTML5/JavaScript GeoLocation: Passing data to a callback function or suspending an async call using promises?

Trying to figure out how to utilize HTML5/Javascript Geolocations effectively for passing location data to an ajax call and storing it in a database. The main challenges I'm facing are the asynchronous nature of the Geolocation call and issues with va ...

What causes two identical pages to display fonts in different ways?

Two identical pages, hosted on the same server with the exact same Apache configuration and home directory. Each page contains only one sentence of text, unstyled. The strange thing is, when viewed in Chrome and Safari, the fonts render differently, while ...

implement css property solely when device is in landscape orientation

I am facing an issue with applying CSS to a mobile device, specifically for horizontal orientation. When the device is in a vertical position, the page appears blank, and different CSS loads for desktop view. Although I have managed to make the desktop CS ...

toggle the outer div along with its corresponding inner div simultaneously

On one of my pages (let's call it "page1"), I have multiple divs, some nested within others. These divs are initially hidden and toggle on when a specific link is clicked (and off when clicked again). To view a nested div, the outer div must be opened ...

Removing the 'div' tag using jQuery in CodeIgniter

I need assistance in removing the added item. $(document).ready(function() { $("#appendex0").click(function() { $(".divcls0").append('<div class="col-sm-10 col-sm-offset-1"><div class="col-sm-3 col-sm-offset-1"><div class="form ...

What is the best way to eliminate the right margin from my div element?

On my contact page, there's a div with the ID of contactdetails that appears to have a margin on the right side when viewed in Chrome's developer tools. This margin is causing some layout issues, specifically preventing me from placing the Google ...

Deliver real-time notifications to linked users by leveraging socket.io and node.js

Is there a solution for sending real-time updates to connected clients every second without using the setInterval() function in nodejs and socket.io? Please provide an alternative method that fits my specific scenario. ...

What steps can I take to make sure a jQuery Mobile table does not become responsive?

My goal is to make a table responsive by using media queries to hide optional columns on smaller devices. I need to hide two columns based on different screen sizes: @media screen and (max-width: 33em) { th.optional-1, td.optional-1 { display: no ...

When utilizing the --watch flag, Node-sass will fail to function properly

Encountering an issue with the "--watch" or "-w" flag when running node-sass. After installing node-sass as a devDependency and setting up a script to compile SCSS code, everything runs smoothly without any flags. However, adding the "--watch" flag causes ...