Tips for utilizing CSS flexbox to create a row of squares that can scale while maintaining their aspect ratios

Struggling with this issue has me on the verge of pulling my hair out. It seems like a simple task, yet I can't seem to achieve perfection. My goal is to create a row of squares that maintain their aspect ratio but scale down in size as their container shrinks. I've come close by setting the square's height to 100% with an aspect-ratio of 1/1; and using a flex display for its container. While this approach works for the height, it falls short when scaling the width because the flex container and its content do not scale accordingly. I've simplified the code to make it easier for replication and have incorporated one of the suggested solutions which almost achieves what I'm aiming for.

* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  width: 100%;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.game {
  width: 100%;
  max-width: 500px;
  height: 100%;
  display: flex;
  flex-direction: column;
}

.header-container {
  background-color: grey;
  width: 100%;
  height: 15vh;
  max-height: 100px;
}

.row-container {
  background-color: green;
  width: 100%;
  height: 20vh;
  max-height: 150px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.row {
  display: flex;
  height: 100%;
  width: 100%;
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.square {
  aspect-ratio: 1/1;
  flex: 0 1 100px;
  background-color: blue;
  margin: 5px;
}

.board-container {
  background-color: aquamarine;
  width: 100%;
  height: 45vh;
}

.lower-container {
  background-color: black;
  width: 100%;
  height: 30vh;
  max-height: 250px;
}
<div class="game">
  <div class="header-container">

  </div>

  <div class="row-container">
    <div class="row">
      <div class="square"></div>
      <div class="square"></div>
      <div class="square"></div>
      <div class="square"></div>
      <div class="square"></div>
      <div class="square"></div>
      <div class="square"></div>
    </div>
  </div>

  <div class="board-container">

  </div>

  <div class="lower-container">

  </div>
</div>

The provided CSS covers all aspects, but my focus for improvement is within .square, .row, and .row-container classes.

Answer №1

#container {
 display: flex;
 height: 100%;
 width: 100%;
 flex-direction: row;
 justify-content: center;
 align-items: center;
}

.square {
 aspect-ratio: 1/1;
 flex: 0 1 55px;
 background-color: blue;
 margin: 10px;
}
<div id="container">
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
  <div class="square"></div>
</div>

Uncertain if this is aligned with your requirements. Can you please provide a concise example of how you intend to use it?

Answer №2

It appears that there may have been some confusion with your inquiry, but are you certain it does not resize accordingly? Take a look at the rendering when I adjust the width to 100%:

#container {
 display: flex;
 height: 100%;
 width: 100%;
 flex-direction: row;
 justify-content: center;
 align-items: center;
}

.square {
 background-color: orange;
 width: 100%;
 height: 100%;
 max-height: 55px;
 aspect-ratio: 1/1;
}
<div id="container">
  <div class="square">a</div>
  <div class="square">a</div>
  <div class="square">a</div>
  <div class="square">a</div>
  <div class="square">a</div>
  <div class="square">a</div>
</div>

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 it possible that a link with a negative z-index is unclickable in IE11 but functions properly in Chrome, Firefox, and Edge?

I am currently facing an issue with a menu and div content. The problem is that the link in the menu is not clickable on IE 11. What could be causing this problem? Visit this link for more details #menu { position: absolute; background: red; wid ...

How to locate the position of a specific word on a webpage using jQuery

In my div, I am struggling to search like an editor. Despite multiple attempts, I have not found a solution yet. Can someone please advise me on how to resolve this issue? <div id="content"> <p> Lorem Ipsum is simply dumm ...

Div content with a unique angle

I need to create a website with slanted div elements that must meet the following criteria: The sections should have a height of approximately 400px when displayed side by side, and about 800px when they stack vertically. It would be ideal if this could ...

Verify the dropdown selection when moving focus away from it

I need help validating my DropDownlist on tabout. Here is the code for the dropdownlist: <div class="form-group"> <label class="col-sm-2 control-label labelfont">Select a Provider Type:</label> <div class="col ...

Extracting Data from Imgur Videos: Measuring their Duration

Hey there! Lately, I've been faced with the challenge of extracting specific information from videos hosted on the imgur website. Specifically, I'm interested in retrieving the video length indicated in the attached image. Despite my best efforts ...

Improve the loading speed of large datasets into HTML tables

I have a problem where I generate an HTML table from code behind to display data like a grid (Note: I am not using GridView). When the data is large, around 2000 rows, it causes the application to display a white page as if it has hung. What is the best w ...

What is the best way to position a small image within a menu?

Is there a way to adjust the position of the arrow image within the <li> element without affecting the layout of the unordered list? Below is the code snippet for reference: body { margin: 0; padding: 0; } nav { width: 100%; background: # ...

Automatically submitting the form

Is there a way to automatically submit a form once 4 numbers are inputted into the field without having to press enter or click submit? <form action="send.php" method="POST"> <input type="number" maxlength="4"> </form> I have a basic ...

Using Javascript to iterate through and increase HTML code with values all the way up to 55

I am looking for a way to automatically generate a list of links in an HTML page using JavaScript. I have tried a few scripts, but none have worked for me. This is the current structure of my HTML... <a href="1.html"><img src="images/1.jpg" widt ...

Is it possible to maintain child divs in a single line?

Despite trying numerous recommendations (many involving white-space:nowrap and display:inline-block), I have been unsuccessful in keeping these child divs on a single line with horizontal scrolling. This is my code: <div id="list"> < ...

Using various colors to highlight specific sections of a letter or number

I am striving to recreate the unique image shown below, particularly interested in achieving the multi-colored effect on the numbers. This aspect of having different colors for parts of the number is intriguing and I would love to learn how it's done. ...

Stop the container from wrapping and allow its children to wrap instead

Here is a snapshot of my current layout: <div class="left"> ... </div> <div class="right"> <div class="inner" /> ... several more of these ... <div class="inner" /> </div> My CSS code looks like this: ...

Difficulty aligning headings in HTML

I'm puzzled by a strange 1px gap on the left of my h2 heading, which sits atop an h3. The font sizes are set to 40px for h2 and 12px for h3. Can anyone help me solve this mystery? Any assistance would be greatly appreciated! Thank you body { pa ...

Using FlexBox for Vertical Alignment of Elements

Experimenting with FlexBox (for demonstration purposes only). I am facing a challenge in vertically centering text within the parent .top-nav-bar. While using justify-content: space-between, I have successfully aligned two elements horizontally, but I am s ...

The arrows on the Slick Slider appear cropped on ios devices like the iphone 8, however, they display perfectly when tested on Chrome and Firefox

I am currently facing an issue with my slick slider setup at https://www.labtag.com/shop/product/clear-cryogenic-labels-for-frozen-vials-1-75-x-1-aha-224/. It is configured for 4 slides on desktop and 2 slides on mobile devices (768px breakpoint). The pr ...

The CSS outline has a soft, rounded appearance rather than a sharp

Is there a way to prevent my outline from having rounded corners as it expands during the animation? Note: The issue seems to be present on Linux and MS Edge, but works fine on Mozilla Firefox. Any solutions for MS Edge users? Check out the code on Codep ...

Tips for creating spacing between an image and a button when utilizing the routerLink feature in CSS

To enhance the user interface, I've utilized a routerLink with an image to navigate back to the home page, and a button to direct users to add a new customer. Now, I am aiming to create some space between these elements. Previously, I used "& ...

Shadow effect is present under every cell in the table row

I am struggling to create an HTML table with proper vertical spacing between rows and a shadow effect beneath each row. Unfortunately, the shadow always ends up overlapping other table content. I have tried adjusting the positioning of elements and setti ...

Interactive text animation triggered by a click

jQuery ( function ( $ ) { console.log(">>Testing animation"); $('a.loveit').on('click',function(event){ event.preventDefault(); var text = $(this).find('div.love-text'); ...

What is the best way to center a div horizontally on all screen resolutions using only CSS?

The margin: 0 auto declaration is not having the desired effect for some reason. I have attempted using text-align in the header, as well as exploring various other options, but none seem to be working. This is my current code: div.middle { font-fami ...