Adjust the container width to match the width of the visible thumbnails?

Take a look at my website over at: . If you want to see more thumbnails, consider switching to a different album. The album titled Paris contains the most images.

If you press the "Show Photo Grid" button located in the bottom-right corner, you'll be able to view the photo/thumbnail grid that I've created.

Currently, the grid resizes based on the viewport size. I would like it to still resize according to the viewport, but instead of smoothly getting bigger, I'd like it to only be as wide as the number of thumbnails that can fit in the viewport. This way, there won't be empty white space to the right of the displayed thumbnails. I want to maintain a 20px distance from the right side of the viewport, with any extra space displayed on the left side.

How can I achieve this?

Here is the current code:

HTML

<div id="grid_outer">
  <div id="grid_inner">
    <div class="grid_thumb button">
      <span class="thumb_title">TITLE</span>
      <img class="thumb_image" src="image.jpg" alt="TITLE">
    </div>
    <div class="grid_thumb button">
      <span class="thumb_title">TITLE2</span>
      <img class="thumb_image" src="image2.jpg" alt="TITLE2">
    </div>
  </div>
</div>

CSS

#grid_outer {display:none; position:absolute; background-color:rgba(0,0,0,0.75); right:20px; left:20px; bottom:60px; padding:20px 10px 10px 20px; border-radius:20px; max-height:480px; overflow:hidden;}
.grid_thumb {position:relative; float:left; margin:0 10px 10px 0; width:150px; height:150px;}
.thumb_image {position:absolute; top:0; left:0; border-radius:5px;}
.thumb_title {position:absolute; bottom:0px; left:0px; z-index:100; padding:8px; background:#000; border-radius:0 5px 0 5px;}
.button {cursor:pointer;}

After some trial and error, I found that setting the width of #grid_outer to auto works well when there's less than one row of thumbnails, but it stretches to the far left side of the viewport if there's more than one row. Can anyone provide guidance on this issue?

Answer №1

A Pure CSS Approach Using CSS3 Selectors and Media Queries

View the CSS-only solution on jsFiddle

div.grid {
    font-size: 170px;
    position: fixed;
    max-width: 1em;
    height: 3em;
    bottom: 20px;
    right: 20px;
    padding: 5px;
    background-color: rgba(0, 0, 0, .75);
    border-radius: 20px;
    overflow: auto;
}

div.grid:after {
    display: block;
    content: "";
    clear: both;
}

div.grid > div {
    width: 160px;
    height: 160px;
    float: left;
    margin: 5px;
    border-radius: 10px;
    background-color: #009dff;
}

@media screen and (min-width: 410px) {
    div.grid {
        max-width: 2em;
    }

    div.grid > nth-child(2n) {
        clear: right;
    }
}

@media screen and (min-width: 550px) {
    div.grid {
        max-width: 3em;
    }

    div.grid > nth-child(3n) {
        clear: right;
    }
}

@media screen and (min-width: 720px) {
    div.grid {
        max-width: 4em;
    }

    div.grid > nth-child(4n) {
        clear: right;
    }
}

@media screen and (min-width: 890px) {
    div.grid {
        max-width: 5em;
    }

    div.grid > nth-child(5n) {
        clear: right;
    }
}

@media screen and (min-width: 1060px) {
    div.grid {
        max-width: 6em;
    }

    div.grid > nth-child(6n) {
        clear: right;
    }
}

@media screen and (min-width: 1230px) {
    div.grid {
        max-width: 7em;
    }

    div.grid > nth-child(7n) {
        clear: right;
    }
}

@media screen and (min-width: 1400px) {
    div.grid {
        max-width: 8em;
    }

    div.grid > nth-child(8n) {
        clear: right;
    }
}

@media screen and (min-width: 1570px) {
    div.grid {
        max-width: 9em;
    }

    div.grid > nth-child(9n) {
        clear: right;
    }
}

@media screen and (min-width: 1740px) {
    div.grid {
        max-width: 10em;
    }

    div.grid > nth-child(10n) {
        clear: right;
    }
}

The jQuery Solution

View the jQuery solution on jsFiddle

var size = 170;
var margin = 40;
var win = $(window);
var adjust = function() {

    $('div.grid').each(function() {

        var width = win.width() - margin;
        width = Math.floor(width / size) * size;

        $(this).width(width);
    });
};

win.resize(adjust);
adjust();

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

Unable to display information from a repeated `textarea` in ngRepeat

Check out my code on Plunker: https://plnkr.co/edit/rBGQyOpi9lS0QtnCUq4L I'm trying to log the content of each textarea when typing, using the printStuff() function: $scope.printStuff = function(customize, item) { console.log(customize[item.inde ...

Enhancing an image with zoom effect in a 2-column layout on hover

Could someone help me achieve a 2-column layout where the left column contains text and the right column an image? I want the image in the right column to zoom when the user hovers over either the left or right column. The issue is that when the user hove ...

Having trouble with the preview feature when resizing images

Before trying to implement the JSFiddle was working correctly: http://jsfiddle.net/qa9m7t33/ However, after attempting to implement it, I encountered some issues: http://jsfiddle.net/z1k538sm/ While trying to resize an image, I realized that this example ...

Adding an HTML string to the head in Next.js

Every time I fetch data from an API, it returns an object with the property head: '<title>dummy title<title>' Despite trying different methods, I am unable to display this HTML string in the head tag. This is my code: <Head>{da ...

Why isn't my API's JSON Response showing up in PHP?

Having trouble displaying the JSON response from an API call in PHP. Despite confirming that the server, IP settings, and API status are all fine on the provider's end, I am unable to identify the problem. This is how I'm handling the PHP part: ...

Contact form in PHP fails to send upon submission

An issue I am currently facing involves a simple php mail server setup in my nodeJS site for managing contact messages. Strangely, when testing it from my local machine, the message does not get routed to the designated email address. I suspect that this c ...

Resolving VSCode WebViewPanel access issues through a corporate proxy

I am currently utilizing the "C4 DSL Extension" in VSCode to display previews of my architecture diagrams. These previews are generated through the WebviewPanel functionality. If you're interested, you can access the source code here: While everythin ...

Exploring the wonders of CSS with Socialchef

Hey, I recently came across a solution on Stack Overflow where they provided code for SocialChef to convert decimal inputs into fractions. I'm wondering how I can write or incorporate some code to input fractions and mantissa as a fraction... I ...

Displaying an interactive 2D floorplan in a web browser with the use of html5 and javascript

In the process of updating my old Flash viewer, I am looking to display interactive 2D floorplans exported from AutoCAD. Currently, I convert the AutoCAD files into XML files containing the X and Y coordinates of the various elements on the floorplan such ...

NgFor is failing to display data from a fully populated array of objects

CLICK HERE FOR THE IMAGE So, let's tackle the issue at hand. I have an array that contains data fetched from an API. These data points are essentially messages. Now, below is the TypeScript file for a component where I aim to display all these messag ...

Unable to remove the initial item from my shopping cart

Currently working on my computing science assignment and decided to create an online store. I followed a tutorial to implement the shopping cart functionality, but I'm facing an issue. I can delete every item in the cart except for the first one that ...

Reveal Password Feature in Angular After User Click

I am working on an inventory page that includes a password field. My goal is to initially hide the password and display it as points (****) instead. I would like the password to be revealed either when clicked or through a pop-up. JS var retrieveCert ...

Learn how to extract JSON information from a URL and integrate it into either a table or dropdown in Vue.js

Looking to retrieve a JSON data array from a URL and utilize it to populate either a table or dropdown with Vue.js and Axios. Here is the link where I intend to fetch the data. Any advice on how to accomplish this? https://jsonplaceholder.typicode.com/us ...

What is the best way to clear the selected option in a dropdown menu when choosing a new field element?

html <div class="row-fluid together"> <div class="span3"> <p> <label for="typeofmailerradio1" class="radio"><input type="radio" id="typeofmailerradio1" name="typeofmailerradio" value="Postcards" />Postcards& ...

The color of the Angular md-switch .md-thumb is not showing up properly when switching the toggle

In Safari, the md-switch displays a yellow section on the md-thumb when toggled. However, other browsers handle the switch without any issues. The md-bar appears fine, but the md-thumb is yellow. I have tried setting everything to the blue color I am using ...

Change the background color of a Bootstrap dropdown when toggled

I recently created this code snippet: <div class="dropdown"> <button class="btn btn-secondary dropdown-toggle buttonForProfile" type="button" id="dropdownMenuButton" data-bs-toggle='dropdo ...

Do AngularJS routes allow the use of special characters in URLs?

Issue at hand: Every time I enter http://localhost:53379 in the browser, it redirects me to http://localhost:53379/#/. Why is the /#/ being added? angular .module('app', ['ngRoute', 'ngStorage']) .config([&apo ...

Using Chrome or Firefox's inspector tool to make changes to HTML code

Is it possible to save changes made to HTML elements in Chrome/Firefox inspector directly to a local file on my desktop? ...

The collapse of HTML elements converging inwards within the Bulma Framework and customized with SASS

Messy Screen Recently delving into the world of Bulma, I decided to take a shot at customizing my CSS. Unfortunately, it seems that after some tweaking, my screen layout has gone haywire. Can anyone shed light on why two elements might refuse to maintain ...

Utilize the data-toggle attribute to retrieve a unique variable name, which can then be integrated into a function for added

I'm in the process of replacing an iframe with an image, and I want to accomplish this with minimal code. The container div I'm working with has a data-toggle attribute: <div id="my-div" data-toggle="video2"> <div id="videocontaine ...