Achieving consistent width for flex items in each row

Can I ensure that all flex items have the same width? Currently, my code looks like this:

.video-container {
  background-color: blue;
  min-height: 90vh;
  display: flex;
  flex-wrap: wrap;
}

.video {
  flex: 1 0 25%;
}
<div class="video-container">
  <video class="video" style="background: red;"></video>
  <video class="video" style="background: blue;"></video>
  <video class="video" style="background: yellow;"></video>
  <video class="video" style="background: green;"></video>
  <video class="video" style="background: orange;"></video>
  <video class="video" style="background: red;"></video>
</div>

Outcome:

This approach limits each row to a maximum of 4 items before moving to the next row.

Although effective for layouts with 4 or 8 items, it results in uneven widths when there are only 6 items present, with the last two expanding beyond the others.

How can I ensure uniform width for all items?

UPDATE

I realize now that using flex: 0 0 25% addresses the issue mentioned above. However, I also want the items to utilize available space effectively based on the number present:

If there is just 1 item, it should take up the full screen.
For 2 items, they should each take half the space,
And so on, up to 4 items.

In essence, similar to the layout seen in Google Meet.

Answer №1

To achieve the desired layout, simply adjust the flex-grow property of the child elements to be 0 instead of 1

.video-container {
  background-color: blue;
  min-height: 90vh;
  display: flex;
  flex-wrap: wrap;
}

.video {
  flex: 0 0 25%;
}
<div class="video-container">
  <video class="video" style="background: red;"></video>
  <video class="video" style="background: blue;"></video>
  <video class="video" style="background: yellow;"></video>
  <video class="video" style="background: green;"></video>
  <video class="video" style="background: orange;"></video>
  <video class="video" style="background: red;"></video>
</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

Show the Table and Conceal the Overflow

Currently, I am experimenting with creating an Img hover for entertainment purposes. However, I have added some text to my div. In order to center the text beautifully within my div, I utilized the following CSS code: display: table-cell; vertical-align: ...

Having trouble locating the nivoslider IE8 jQuery bug, can't seem to track it

I am encountering an issue with the nivoslider script in IE8 and I am having trouble identifying what is causing it. The nivoslider works perfectly fine in all other browsers except for IE8. Any help or guidance on this matter would be greatly appreciated ...

Ways to trigger the display of an image upon hovering over a button without using inheritance

<div class="a"> <img class="img_c" id="img_id" src="~~"> <div class="b"> <div class="c"> <ul class="d" id="e"> <li ~~~ > </ul> </div> </div> </div> Is there a way to display the ...

Firefox with Ajax bug bar

I have encountered an issue with my Navbar specifically on Firefox. The problem I am facing is that the Navbar is flickering, constantly growing and shrinking. Interestingly, this issue does not appear when using Chrome or Opera. I'm curious to know ...

Tips on adjusting a JavaScript animation function

I'm currently working on adjusting the animation function within the "popup" class that controls the gallery section of a website. When the page loads, an image and background start expanding from zero scale in the center to 100 VIEWPORT HEIGHT (VH) a ...

Is there a way to create a new perspective for Ion-Popover?

Looking for a solution: <ion-grid *ngIf="headerService.showSearch()"> <ion-row id="searchbar" class="main-searchbar ion-align-items-center"> <ion-col size="11"> ...

Endlessly triggering document.execCommand, the JavaScript selectionchange-EventListener seems to have a mind of

I recently implemented an event listener for selectionchange in the following manner: document.addEventListener("selectionchange", function() { highlight(); console.log("selectionchange-triggered"); }, false); After that, I included the code bel ...

Overriding the !important declaration in inline styles within various CSS IDs and classes

I need to find a way to override an inline !important declaration that is nested inside multiple CSS ids and classes. Let's analyze the following situation: <div class="A"> <div class="B"> <div class="C"> < ...

Automatically scrolling down a div as new content is added using XMLHTTPRequest.openConnection

https://jsfiddle.net/kv5gbamg/ - I created a jsfiddle to demonstrate the functionality of the system Essentially, I am seeking a way to automatically scroll the scrollbar to the bottom every time a new message is received. My system updates the div with ...

Cannot assign border to an undefined element - JavaScript

Currently, I am attempting to validate some code using javascript. However, I am encountering a frustrating issue where I keep getting an "Uncaught TypeError: Cannot set property 'border' of undefined". Being relatively new to javascript, I ...

Tips for creating a subclass using jQuery

I am familiar with selecting the class "myClass" using $(".myClass"), but my question is how do I select the child link within an element with the class "myClass" using jQuery? Here is a sample of the code: <a href=#>Home</a> <div class ...

Sending values from buttons to different Django templates/views

Initially, the user provides a CSV file. (Template File:) <form method="post" action="{% url 'rowcol' %}" enctype="multipart/form-data"> {% csrf_token %} <input type="file" name="file" accept=".csv"> <button type="sub ...

Achieve a Smooth Transition: Utilizing Bootstrap 3 to Create an Alert Box that Fades In upon Click and Fades Out

Incorporating AngularJS and Bootstrap 3 into my web app, there is an "Update" button that saves user changes. Upon clicking the "Update" button, I aim to trigger and display bootstrap's alert box with the message "Information has been saved," followed ...

Expanding the MatBottomSheet's Width: A Guide

The CSS provided above is specifically for increasing the height of an element, but not its width: .mat-bottom-sheet-container { min-height: 100vh; min-width: 100vw; margin-top: 80px; } Does anyone have a solution for expanding the width of MatBott ...

Do not insert a MUI divider after the final item in the menu

Is there a way to remove the divider after the last category when using MUI components and adding a divider after each category? This is my code with the divider right at the bottom before the closing tag. export const MenuItemGroup = ({ label, items, ...

Manipulate sibling elements using jQuery's addClass and remove methods

I have implemented a function that adds the class active to elements when they reach the top of the browser, ensuring that the next element will scroll in over the top. While this works smoothly in Chrome, I am encountering some jumping behavior when testi ...

Why has my dropdown menu decided to go horizontal, rather than dropping down as it should?

Hi there! I'm new to css and tried following a tutorial with some tweaks. However, when adding a drop down menu using lists, it ended up going sideways instead of downward. Can anyone help me out? I also would like to have a collapsible menu implemen ...

Choose an element on a webpage by leveraging the power of Selenium

When working with Selenium in FireFox, I encountered an issue while trying to select specific fields on a webpage. The HTML pages I am referring to are available at the following links: Sample HTML Page, another sample page. The code snippet I used is sh ...

"Incorporate Bootstrap drop-down and button together for a stylish group feature

I am looking to format the code below to resemble the layout shown in the image using Bootstrap 3.3. When there isn't enough space to show all elements in a single line, I want them to stack vertically so that each element utilizes the entire width of ...

Creating a custom dropdown behavior using Angular

I have a unique interface requirement where users must be able to click on an element to view its options, which are displayed as checkbox inputs. Essentially, I need a custom 'select' element that allows for multiple checkbox selections. ...