The flex-box has been elongated

There is an issue with the image gallery in this example where boxes 1 to 9 are stretching downward, as shown in the snippet.

I have attempted to set them with a fixed size using align-content and align-items, but I have not had any success.

If I adjust the browser size, the boxes return to their normal layout, similar to the image link here: https://i.sstatic.net/f3Xjk.png

const menu = document.querySelectorAll('.inner .box');
const cursors = document.querySelectorAll('.cursor')

cursors.forEach( c => {
  c.addEventListener('click', () =>{
    for(let i = 0; i < menu.length; i++){
      if(menu[i].classList.contains('scale')){
        if(c.innerText == "<"){
          if(i == 0){
            menu[0].classList.remove('scale')
            menu[menu.length - 1].classList.add('scale')
            break
          }
          menu[i].classList.remove('scale')
          menu[i-1].classList.add('scale')
          break
        }
        else if(c.innerText == ">"){
          if(i == menu.length - 1){
            menu[i].classList.remove('scale')
            menu[0].classList.add('scale')
            break
          }
          menu[i].classList.remove('scale')
          menu[i+1].classList.add('scale')
          break
        }
      }
    }
  });
});
body{
  height:90vh;
  background-image: radial-gradient( circle farthest-corner at 10% 20%,  rgba(0,93,133,1) 0%, rgba(0,181,149,1) 90% );
  background-repeat: no-repeat;
  animation: GradientAnimated 59s ease infinite;
}

.container{
  margin-top: 10vh;
  display: flex;
  padding:-1px;
  width: 100vw;
  align-items: center;
  justify-content: space-around;  
}
.container.inner{
  margin-top:0;
  width: 50vw;
  height: 40vh;
  border: 2px solid black;
  align-items: center;
  justify-content: center;

}
.box{
  width: 15vw;
  height: 40vh;
  color: white;
  text-align: center;
  font-size: 4vw;
}

.box1{background-color:#f00;}
.box2{background-color:#fa0;flex-grow:1;}
.box3{background-color:#ff0;}
.box4{background-color:#080;}
.box5{background-color:#00f;}
.box6{background-color:#6ae;}
.box7{background-color:#90d;}
.box8{background-color:#408;}

.box.scale:hover{
  cursor: pointer;
}

.cursor{
  color: #222;
}

.cursor:hover{
  cursor: pointer;
  animation: shake 1s cubic-bezier(.36,.07,.19,.97) both infinite;
}

.scale{
  box-sizing: content-box;
  border: 2px solid black;
  animation-name: Scale;
  animation-timing-function:ease;
  animation-duration: 1s;
  animation-iteration-count:1;
  animation-fill-mode: forwards;
}

@keyframes Scale{
  from{transform: scale(1);}
  to{transform: scale(1.5);}
}

@keyframes shake {
  10%, 90% {
    transform: translate3d(-1px, 0, 0);

  }

  20%, 80% {
    transform: translate3d(2px, 0, 0);
  }

  30%, 50%, 70% {
    transform: translate3d(-4px, 0, 0);
  }

  40%, 60% {
    transform: translate3d(4px, 0, 0);
  }
}

@keyframes GradientAnimated {
    0%{background-position:0% 10%}
    50%{background-position:100% 91%}
    100%{background-position:0% 10%}
}
<div class="container">
  <div class="box cursor">
    <</div>
      <div class="container inner">
        <div class="box box1 scale">1</div>
        <div class="box box2">2</div>
        <div class="box box3">3</div>
        <div class="box box4">4</div>
        <div class="box box5">5</div>
        <div class="box box6">6</div>
        <div class="box box7">7</div>
        <div class="box box8">8</div>
      </div>
      <div class="box cursor">></div>
  </div>

Answer №1

If you want to specify the height of an element, make sure to use max-height instead of height.

const menu = document.querySelectorAll('.inner .box');
const cursors = document.querySelectorAll('.cursor')

cursors.forEach(c => {
  c.addEventListener('click', () => {
    for (let i = 0; i < menu.length; i++) {
      if (menu[i].classList.contains('scale')) {
        if (c.innerText == "<") {
          if (i == 0) {
            menu[0].classList.remove('scale')
            menu[menu.length - 1].classList.add('scale')
            break
          }
          menu[i].classList.remove('scale')
          menu[i - 1].classList.add('scale')
          break
        } else if (c.innerText == ">") {
          if (i == menu.length - 1) {
            menu[i].classList.remove('scale')
            menu[0].classList.add('scale')
            break
          }
          menu[i].classList.remove('scale')
          menu[i + 1].classList.add('scale')
          break
        }
      }
    }
  });
});
body {
  height: 90vh;
  background-image: radial-gradient( circle farthest-corner at 10% 20%, rgba(0, 93, 133, 1) 0%, rgba(0, 181, 149, 1) 90%);
  background-repeat: no-repeat;
  animation: GradientAnimated 59s ease infinite;
}

.container {
  margin-top: 10vh;
  display: flex;
  padding: -1px;
  width: 100vw;
  align-items: center;
  justify-content: space-around;
}

.container.inner {
  margin-top: 0;
  width: 50vw;
  height: 40vh;
  border: 2px solid black;
  align-items: center;
  justify-content: center;
}

.box {
  width: 15vw;
  max-height: 40vh; /*  remember to use max-height instead of height */
  color: white;
  text-align: center;
  font-size: 4vw;
}

.box1 {
  background-color: #f00;
}

.box2 {
  background-color: #fa0;
  flex-grow: 1;
}

.box3 {
  background-color: #ff0;
}

.box4 {
  background-color: #080;
}

.box5 {
  background-color: #00f;
}

.box6 {
  background-color: #6ae;
}

.box7 {
  background-color: #90d;
}

.box8 {
  background-color: #408;
}

.box.scale:hover {
  cursor: pointer;
}

.cursor {
  color: #222;
}

.cursor:hover {
  cursor: pointer;
  animation: shake 1s cubic-bezier(.36, .07, .19, .97) both infinite;
}

.scale {
  box-sizing: content-box;
  border: 2px solid black;
  animation-name: Scale;
  animation-timing-function: ease;
  animation-duration: 1s;
  animation-iteration-count: 1;
  animation-fill-mode: forwards;
}

@keyframes Scale {
  from {
    transform: scale(1);
  }
  to {
    transform: scale(1.5);
  }
}

@keyframes shake {
  10%,
  90% {
    transform: translate3d(-1px, 0, 0);
  }
  20%,
  80% {
    transform: translate3d(2px, 0, 0);
  }
  30%,
  50%,
  70% {
    transform: translate3d(-4px, 0, 0);
  }
  40%,
  60% {
    transform: translate3d(4px, 0, 0);
  }
}

@keyframes GradientAnimated {
  0% {
    background-position: 0% 10%
  }
  50% {
    background-position: 100% 91%
  }
  100% {
    background-position: 0% 10%
  }
}
<div class="container">
  <div class="box cursor">
    <</div>
      <div class="container inner">
        <div class="box box1 scale">1</div>
        <div class="box box2">2</div>
        <div class="box box3">3</div>
        <div class="box box4">4</div>
        <div class="box box5">5</div>
        <div class="box box6">6</div>
        <div class="box box7">7</div>
        <div class="box box8">8</div>
      </div>
      <div class="box cursor">></div>
  </div>

Answer №2

When styling the boxes, it is recommended to either use fixed units for height or opt for max-height instead of height.

You can choose to style it like this:-

.box{
height: 300px;
}

Alternatively, you can go with this approach:-

.box{
max-height: 40vh;
}

Opting for the max-height method is more preferred as it eliminates the need to define other flex properties such as Shrink And Grow.

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

Ways to showcase tooltip text for an unordered list item?

When creating an unordered list, each element's text corresponds to a chapter name. I also want to include the description of each chapter as tooltip text. The Javascript code I currently have for generating a list item is: var list_item = document.c ...

Ways to disable mousewheel function in jQuery

I am trying to deactivate the mouse scroll in jQuery and successfully did so, however, I encountered this error message jquery.min.js:2 [Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See Thi ...

Issue with background image not covering entire div

I am experiencing an issue where the background image of a div element is not occupying the full image. I want to display images side by side with text underneath them. Below is the CSS code: .img-w { position: relative; background-size: cover; ba ...

Discrepancy in div height between Chrome/Edge and Firefox browsers

I am currently in the process of designing a straightforward website layout that includes a navigation bar at the top. The main focus is on an image that needs to stretch from the bottom of the navbar to the bottom of the window, filling up the entire scre ...

When the height exceeds the window in IE8, the -ms-filter background vanishes as the scrollbar appears

When the height of a div is greater than the window in IE8, the background specified with -ms-filter disappears. However, this issue does not occur in IE7 and earlier versions. (One workaround could be using a 1px transparent image, but what if I want to u ...

Adjusting the space between div elements when resizing the window

I am seeking guidance on how to ensure that the text boxes (div) on my page adjust themselves properly when the size of the page is shrunk. Currently, the text boxes tend to overlap when the screen size is smaller, which is not desirable. I need the boxes ...

The division element shifts to the left upon invoking the slideUp() function

I am currently working on a page that contains a form with two different sections - one is visible and the other is hidden. When the user clicks a button in the first section, it slides up using slideUp() while the hidden section slides down using slideDow ...

In mobile view, the grid text should be positioned on top of the input field

Created a basic grid layout with input fields. Everything looks fine on the PC, but on mobile devices, long text causes the input field to be positioned below the text. However, when the text is short like Lor, it appears next to the input field... I want ...

Is there a way to position text to the right of an image?

Looking for some help with aligning text to the right of an image within a hyperlink. I want the hyperlink to fill the rectangle and achieve a specific layout similar to attachment1, but currently getting something different like attachment2. If anyone ha ...

the frame on the page is distorting

I am trying to implement a frame around my website that overlaps elements. desired appearance Although I created a styled div for the frame, it seems to have a bug and looks like this: current appearance Below is the React code I am using: class About ...

The last value label in Google Charts bar chart getting cut off

I've searched extensively for a solution to this issue regarding the haxis labels, but I have not been able to find one that addresses it... In the image provided below, you'll observe that the last percentage label on the horizontal axis is mis ...

Issue with Email Signature on replies

Recently, I had some email signatures designed and they appear great when sent out, but encounter issues upon receipt or reply: The logo gets distorted and occasionally enlarges significantly during replies. The font color switches to black when receivin ...

What is the best way to implement a loading cursor when the Submit button is clicked?

Is there a way to incorporate a progress cursor into my code in order to notify the user to wait when they click the Submit button or the Upload Button while uploading multiple files? Below is an example of my form: <form action="" method="post" enct ...

Utilizing unique background images tailored to different screen resolutions

In order to enhance the user experience on my website, I am looking to implement a feature that dynamically changes the background images based on the user's screen resolution. My plan is to use a small snippet of JavaScript within the <head> s ...

Bootstrap3: Issue with Firefox - Unable to define height for div

Firstly, I'd like to share the link with you: While everything looks good in Chrome, the slider in Firefox seems to be pushed to the right side. This issue arises because the height of 'nav.navbar.navigation-9' is set to 50px. Even though I ...

HTML element that does not contain any image attributes

Currently facing two issues, with the second one dependent on the resolution of the first. Everything was functioning correctly and looking fine until I came in to work on it today, only to find that the page is now broken. Interestingly enough, I am using ...

Close button for colorbox modal window containing an iframe

I'm currently utilizing colorbox for a modal popup, and the content of the popup is being sourced from a URL. Since it's displayed within an iFrame, I'm wondering how I can incorporate a close button to the modal popup. Thank you The follo ...

Creating a drop-down menu within an HTML table along with a D3 bar chart

How can I implement a drop-down menu allowing the user to choose a time interval for this HTML table and d3 bar chart? The time intervals needed are: Now, 24 hours, 48 hours, 72 hours, 1 week, and 1 month. I am relatively new to creating dynamic tables and ...

Error in jQuery submenu positioning issue

I am attempting to design a menu that opens when the parent element is clicked and closes when the mouse leaves the previously opened one. It should operate from left to right. Here is an example: <ul class="menuFirst"> <li><im ...

Strategies for avoiding text wrapping in Bootstrap labels on Firefox

Within my panel and panel-body, there are multiple span elements that display dynamically. These spans behave perfectly in Safari and Chrome, wrapping to the next line when needed. However, in Firefox, they overflow strangely. Here are the comparisons: Ch ...