Adjust the content size to 100% based on the quantity of items in a row

I've been having a hard time creating an image gallery that adjusts its size automatically (width: 100%) based on the number of items it contains.

For example, take a look at this gallery: http://codepen.io/anon/pen/eNMOEz

.item {
  width: 75px;
  height: 75px;
  border-width: 1px;
  float: left;
}

As you resize the browser window, more items are added to each row due to float: left property.

However, the gallery ends up with empty space on the right side of the page because each .item is fixed at 75px.

What I am aiming for is something like this: http://codepen.io/anon/pen/xGWKaP

.item {
  width: 5%;
  float: left;
}

In this version, as you can see, the items in each row adjust perfectly to the width of the page since their width is defined as a percentage rather than a fixed pixel value. However, there are two issues that need to be addressed:

  1. The number of items per row is fixed (20 in the example). I want the item count to be calculated dynamically based on how many can fit within the available width. For instance, if each item is 75 pixels wide and the page width is 800 pixels, then the item count should be 10 (as 750 pixels is the maximum width that can be covered by 800 pixels.) So it should be width: 10%

  2. There is extra space at the bottom of each row, and I'm not sure what is causing this.

I prefer a CSS-only solution but would also consider a clever JavaScript solution that doesn't cause any glitches.

I know this is a lengthy post, but I hope everything is clear now.

Answer №1

If you're looking for a solution, check out this potential answer here: Removing gaps in Flexbox layouts

Regarding your second query:

2) I'm noticing a gap at the bottom of each row and I can't figure out why.

This issue usually arises due to an image within a block element. To get rid of the gap, try adding 'vertical-align' property with values like top, middle, or bottom.

Answer №3

Give it a shot:

.element {
    width: 8vw;
    height: 8vh;
    border-width: 2px;
    float: left;
} 

Alternatively, if the scrollbar appears:

body {
  margin: 0;
  padding: 0;
  font-size:0px;
}
.element {
  width: 6%;
  height: auto;
  /*border-width: 1px;*/
  float: left;
}
img {
  width: 100%;
}

Consider this alternative approach to display all images in one row (I may not fully understand due to English not being my first language, please bear with any mistakes and provide further explanation ¬¬)

html,body {
  margin: 0;
  padding: 0;
  font-size:0px;
  width:100%;
}
#gallery {
  display:inline-table;
  overflow:hidden;
  width:100%;
  height:auto;
}
.element {
  display:table-cell;
  height:10px;
}
img {
  width: 100%;
}

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

Is the hover effect malfunctioning, even though the correct style has been applied?

I'm currently working on a simple design, but I've encountered an issue. When I hover over the number blocks (1, 2, etc.), my hover effect doesn't seem to work. My intention is to hover over the list item and change the entire digit text fro ...

Using JavaScript and jQuery, apply a class when a specific radio button is checked and the data attribute meets certain criteria

Need help with changing explanation color based on correct answer selection in multiple choice question. Currently, all choices turn green instead of just the selected correct one. Any assistance is welcome, thank you. html <li class='test-questi ...

Populating hidden fields in Javascript with mouseover event trigger - Pre-click action

Seeking a solution for another issue, I stumbled upon the capability to execute JavaScript on mouseover. Here is what I want to execute: $(document).ready(function(){ function myPayment() { var value = document.mortgagecalc.value_input.value; var rate ...

Tips for preloading icon font in Angular server-side rendering (SSR)

Is it possible to preload icons and fonts before the HTML content is rendered? I have been using a preload strategy to try and achieve this. index.html <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

Is it possible for ng-change to invoke another controller?

Can someone help me with the following HTML code? <div ng-controller="select"> <select ng-options="n for n in names" ng-model="selected.item" ng-change="change()"> </select> </div> <div ng-co ...

What is the best way to transfer a JavaScript variable through a query string from one HTML page to another?

Is there a way to pass a JavaScript variable called username from one HTML page, example1.html, to another HTML page, example2.html, using query strings? <script type="text/javascript" > $(document).ready(function() { $('#SubmitForm ...

What steps should I take to ensure that the login page functions properly?

Below is the HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Login Pag ...

How can we use the jQuery toggle function to hide and show elements based on

When using the toggle function, I have noticed that it takes a considerable amount of time to load. As a solution, I attempted to include a loading image while the content loads; however, the image does not appear when the .showall is activated. This iss ...

What is the best method for sending a JSON array to the server from the client side?

Utilizing jQuery, I have created a JSON array and now I am looking to send this data from the client side to the server/backend through a modal window. Upon clicking "Save Changes" on the modal window, my goal is to transmit the var data = $(this).serial ...

Preventing Bull Queue from automatically re-starting jobs upon server restart

Currently, I am utilizing the bull queue system for processing jobs. Imagine a scenario where a job is in progress with an active status and I restart my development server. Upon restarting the worker script, the job remains in the active state within the ...

Ways to retrieve JSON data using Angular JS

I'm currently working on populating my table with data. The URL returns JSON data, but I'm struggling with how to retrieve it using AngularJS. Here is my services.js: angular.module('OrganisatieApp.services', []) .factory('organi ...

Analyze HTML while maintaining the integrity of the initial content

I am facing a challenge with my HTML files. I need to replace certain elements while leaving the rest of the content untouched. Specifically, I am looking to run this jQuery expression (or something similar to it): $('.header .title').text(&apos ...

Having trouble concealing the logout button even after signing out with ng-show in angularjs

The code for displaying the logout button is as follows: <li class="dropdown" data-ng-if="userName"> <a href class="dropdown-toggle clear" data-toggle="dropdown" data-ng-show="userName"> </a> <!-- dropdown --> <u ...

The information sent via POST (via fetch JavaScript with PHP8) is not being received by PHP8

My PHP8 server is not receiving the data I am sending. When trying to insert a new song into my API, an error occurs and in the console, I see an object with POST as an empty array. fetch("http://localhost/api.audio-player/",{ method: 'POST&apos ...

Shopping Dialog with Bootstrap (nakupanda) captures form input [JSFiddle]

Having difficulty extracting data from my form. Currently utilizing the bootstrap dialog from nakupanda () The dialog (located within a fullcalendar select function) var form = $('#createEventForm').html(); BootstrapDialog.show({ mes ...

Having trouble with proper routing using node.js, socket.io, and the __dirname variable

As a newcomer to node.js, I am struggling with creating a basic route to read a file using node.js and socket.io. The solution may be straightforward, but I feel like I'm missing some fundamental concepts here. var http = require('http' ...

Using a static function within a library's state function in NextJS is throwing an error: "not a function"

Inside a library, there's a special class known as MyClass. This class contains a static method named setData. The contents of the MyClass.js file are shown below: class MyClass { static DATA = ''; static setData(data) { MyClass ...

Using JQuery, identify cells located in the first column of a table excluding those in the header section

In the past, I had code that looked like this: $(elem).parents('li').find(...) I used this code when elem was an item in a list, making it easy to reference all items in the list. But now, I've made some changes and decided to use a table ...

Using HTML5 and CSS transitions to scale an image within a 960 grid layout

Having some trouble achieving a smooth transition for the height and width of images on my webpage. I believe if you take a look at my page, not much explanation is needed: Click here to see the image transition The goal is to enlarge the image to double ...

When hovering over it, an element within an li tag appears larger than its parent element

I'm currently working on a project for my school classes and running into an issue with CSS. I'm trying to enclose everything in boxes, but it's proving to be quite challenging. The main problem I'm facing is that when I hover over the ...