How can I adjust the scale of the Flex Grid to fit within

I'm currently working on setting up a dynamic gallery. The number of items in the gallery will vary, and I want them to adjust to fit the browser viewport. It seems like it should be a straightforward task?

My current DOM structure is as follows:

<div class="parent">
  <div class="child"></div>
  <div class="child"></div>
  <div class="child"></div>
</div>

Here's the accompanying CSS:

.parent {
  height: 100vh;
  display: flex;
  flex-wrap: wrap;
}

.child {
  flex: 1 1 auto;
}

However, when there are too many children, they overflow the parent container. I want them to scale their dimensions to fit within the parent. Even with just a few (1-3) children, I want them to adjust accordingly. Shouldn't flex: 1 1 auto be sufficient for this?

Am I approaching this issue incorrectly with CSS grid? Would using Grid be more suitable for my requirements?

Answer №1

Did you attempt using the width property to set the width? For example, width:33.33%. It could create an intriguing visual effect.

Answer №2

After some experimenting, I finally achieved the desired layout using CSS grid in my project. To adapt to the varying number of items in the gallery, I dynamically adjust the number of grid columns using JavaScript, which I didn't include in the demo.

With CSS grid, I utilized

grid-template-columns: repeat($num, 1fr);
to define column sizes and
grid-auto-rows: minmax(auto, 100px);
to ensure rows adjust appropriately to screen size.

To see a clearer demonstration, you can check out this pen: https://codepen.io/hjfitz/pen/LYxJWQe

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

Center a sans-serif font vertically with exact precision while adjusting the font size

My issue is an expansion of a previous problem I mentioned in another question, which can be found here Vertically align sans-serif font precisely using jquery/css. To summarize: I am aiming to align two divs containing text, with one positioned above the ...

JavaScript - Unable to unselect a button without triggering a page refresh

I have a series of buttons in a row that I can select individually. However, I only want to be able to choose one button at a time. Every time I deselect the previously selected button, it causes the page to reload when all I really want is for all the but ...

What is the best way to alter the BackColor of a dropdown list item depending on the variance between the date on the list and the current

I have a dropdown list in (MVC View file & using jQuery); that contains text along with a date substring in the format "yyyy-MM-dd". I am able to extract the date, compare it with the current date, and calculate the difference. Based on this difference ...

The progress bar is visually enhanced with a border style that doubles as a background color

While experimenting with a jsfiddle to create an animated progress bar, I stumbled upon something peculiar. I had a single CSS rule applied to the progress tag: progress { border:2px solid #ccc; } Prior to adding this CSS rule, the progress bar looke ...

Having trouble with a static CSS file not loading correctly in the Django admin interface

Currently, I am attempting to replace the base.css file with my own custom CSS file in order to personalize the appearance of Django Admin. {% extends "admin/base.html" %} {% load static %} {% block title %}Custom Django Admin{% endblock %} {% block bran ...

How to activate the horizontal scrollbar in a jqgrid table

How can I enable horizontal scroll in jqgrid when the grouped text is longer than the table width? The table width is set to approximately 50% of the screen, but when the grouping text exceeds 300 characters, adding overflow property results in the text g ...

Category-Specific Page Display

Can anyone help with CSS coding using Wordpress Elementor to make a page only display posts from the coaching category? I attempted to use this selector article.type-post:not(.category-coaching) { display: none; }, but it doesn't seem to be functi ...

jQuery: Function is not running as expected

I'm facing a challenge in executing a function twice, and I'm having trouble identifying the issue. Check out my JSFiddle at the following link: http://jsfiddle.net/g6PLu/3/ Javascript function truncate() { $(this).addClass('closed&apo ...

Is there a way to extend this section to fill the entire screen height without any white space at the bottom?

We've been working on extending the content to the full height of the screen, but no matter what we try, there's always some white space lingering at the bottom. Any suggestions on how to fix this issue? section { height: 100vh; background ...

Enhance your website's animations using CSS3

Is there a possibility that jQuery will utilize the enhanced performance capabilities of CSS3 animations for functions like .animate(), .slideDown(), .fadeIn(), and others in the future? Seeing this integration would be greatly appreciated, either through ...

The hamburger icon seems to be frozen and unresponsive

I'm struggling to get my hamburger icon to reveal the navigation bar when clicked. The icon itself functions properly and adapts to different screen sizes, but the overlay remains stationary. Check out my code on JSFiddle! <html> <head> ...

Steps for modifying the CSS class of an <a> tag with Jquery/Javascript

I am attempting to dynamically change the class of a tab on the dashboard based on the selected page. In the dashboard, there are 3 tabs: <div> <ul class="menu"> <li class="MenuDashboard"><a href="#" >Dashboard</a&g ...

Issues with Css custom properties on certain webhosting platforms

Seeking assistance with a CSS custom properties issue. Recently purchased a web hosting service from Hostinger and using the default Wordpress template. Our web designer created a website in teleporthq.io, exported the files as HTML and CSS. Deleted the ...

Utilizing the classList property with elements that are hidden from view

How can I use JavaScript to toggle between classes? I can't seem to understand why an element that is set to display: none; is unable to receive a class using classList. In simpler terms, if I define #div1{ width: 25%; background-color: #000000; ...

Creating a dynamic dropdown menu using jQuery animation

I am looking to add animation to my top navigation bar. Currently, the dropdown menus just appear suddenly when hovering over the links in the top nav. Development site: I have attempted the following: <script> jQuery(document).ready(function() { ...

What is the best way to adjust image size according to the dimensions of the parent container using CSS

I am working on a card game using Bootstrap styling and I have a container with a background image of a card table (1000x1000). As I resize the browser window, the card table image shrinks to 800x800, then to 500x500, and finally to 200x200. My issue is th ...

Require assistance in getting a slider operational

Hello there! I could really use your assistance with this code. I have been trying to make it work using JavaScript, but I'm determined not to incorporate any external JS files. Here is the snippet of JavaScript code I am working with: "use strict"; ...

Struggling to keep up with responsive behavior on a page featuring vertically and horizontally centered content that spans the full height

I'm working on a full-height website and the first section needs to have both vertical and horizontal centered content. The issue I'm facing is that there's an image included, which doesn't scale responsively when the screen size change ...

Utilize Custom Field to incorporate background videos from websites like Youtube or Vimeo into your content

I've been working on implementing a video background for my website. I came up with a custom code to embed URL links from Youtube and Vimeo using "oEmbed". The process is functioning well, but I'm facing some challenges in setting the background ...

Tips for directing attention to a specific row with an input field in JavaScript

I duplicated a table and added an input field for users to search or focus on a specific row. However, there are two issues: When a user enters a row number, the table displays the wrong row - for example, if the user enters 15, the table shows row number ...