Creating a layout where elements are evenly sized using Flexbox, regardless of their

How to ensure uniform size for all grid cells?

Check out the issue on JSBIN | See the code on JSBIN

I'm utilizing flexbox to construct a grid layout. Some cells in the grid have content while others are empty, causing inconsistencies in cell sizes. What's the best approach to make all cells evenly sized regardless of their content?

https://i.sstatic.net/slELk.png

HTML

The structure consists of three rows, each containing three cells. Only the center cell in the middle row has children elements.

<div>
  <div></div>
  <div></div>
  <div></div>
</div>

<div>
  <div></div>
  <div><span>contains span</span></div>
  <div></div>
</div>

<div>
  <div></div>
  <div></div>
  <div></div>
</div>

CSS

The center cell is currently larger than other cells due to CSS styling.

body, html {
  height: 100%;
}

body {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}

body>div {
  display: flex;
}

body>div>div {
  border: solid 1px blue;
}

div {
  flex-grow: 1;
}

body>div:nth-child(2)>div:nth-child(2) {
  display: flex;
  align-items: center;
  justify-content: center;
}

Answer №1

When using flex-grow: 1, the initial width of flex items will be based on their content and then the available space will be distributed equally among them.

If you want all flex items to have the same width regardless of their content, you can achieve this by using:

flex: 1;

It's important to note that Flexbox sets auto as the initial value for min-width, which may result in varying widths in some scenarios. To ensure consistent equal widths, it's recommended to include min-width: 0 or specify a different value for overflow.

body,
html {
  height: 100%;
}
body {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}
body > div {
  display: flex;
}
body > div > div {
  border: solid 1px blue;
}
div {
  flex: 1;
  min-width: 0;
}
body > div:nth-child(2) > div:nth-child(2) {
  display: flex;
  align-items: center;
  justify-content: center;
}
<div>
  <div></div>
  <div></div>
  <div></div>
</div>
<div>
  <div></div>
  <div><span>contains span</span></div>
  <div></div>
</div>
<div>
  <div></div>
  <div></div>
  <div></div>
</div>

Answer №2

Make sure to utilize the flex-basis property in order to define the initial length of the flexible element.

body, html {
  height: 100%;
}

body {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}

body>div {
  display: flex;
}

body>div>div {
  border: solid 1px blue;
}

div {
  flex-grow: 1;
   flex-basis: 80px; /*specify the initial length*/
}

body>div:nth-child(2)>div:nth-child(2) {
  display: flex;
  align-items: center;
  justify-content: center;
}
<div>
  <div></div>
  <div></div>
  <div></div>
</div>

<div>
  <div></div>
  <div><span>contains span asdsa asd asd asd asdsad ads asd sad</span></div>
  <div></div>
</div>

<div>
  <div></div>
  <div></div>
  <div></div>
</div>

Answer №3

To evenly distribute available space between child elements, it is recommended to use flex: 1 0 0.

The issue with existing solutions is that they do not explicitly set the flex-basis to 0 or rely on fixed/percentage widths assuming a known number of columns. In scenarios where the number of columns may vary, you would still want to distribute space uniformly among child elements.

This shorthand equates to flex-grow: 1;, flex-shrink: 0;, and flex-basis: 0;. By setting flex-basis to 0, you are instructing the browser not to consider the element's natural width when deciding how to distribute space using flex-grow: 1;.

For more information, refer to the MDN explanation here

https://i.sstatic.net/6jIYV.png

Below is the code showcasing examples with both 3 and 4 columns:

<div class="flex">
    <div></div>
    <div></div>
    <div></div>
</div>

<div class="flex">
    <div></div>
    <div><span>contains span</span></div>
    <div></div>
</div>

<div class="flex">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

<div class="flex">
    <div></div>
    <div><span>contains span</span></div>
    <div></div>
    <div></div>
</div>
<style lang="text/css">
.flex {
    display: flex;
    justify-content: center;
    gap: 3px;
    margin-bottom: 3px;
    background: rgba(0,0,0,0.15);
}
.flex > div {
    flex: 1 0 0;
    background: rgba(0,0,0,0.15);
    padding: 20px;
}
</style>

Answer №4

Simply adjust the flex-basis property to 33%

  body>div>div {
      border: solid 1px blue;
      flex: 0 0 33%;
    }

body, html {
  height: 100%;
}

body {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}

body>div {
  display: flex;
}

body>div>div {
  border: solid 1px blue;
  flex: 0 0 33%;
}

div {
  flex-grow: 1;
}

body>div:nth-child(2)>div:nth-child(2) {
  display: flex;
  align-items: center;
  justify-content: center;
}
   <div>
      <div></div>
      <div><span>contains span</span></div>
      <div></div>
    </div>
   <div>
      <div></div>
      <div></div>
      <div></div>
    </div>
      
   <div>
      <div></div>
      <div></div>
      <div></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

Implementing a password toggle feature on a form that extends Django's default Authentication Form

Incorporating a password toggle feature has become quite the challenge as I extend Django's AuthenticationForm to create my UserLoginForm. Attempting to implement this feature has proven difficult, especially since I have been unable to make use of th ...

Show the Canvas element at the back of the DIV

This particular question requires a clear explanation. My goal is to have the canvas act as a background element on the page, while the main content of the page starts in its usual position. I attempted using separate DIVs with their own Z-index values, bu ...

Center text vertically within an HTML Bootstrap row

I am creating a Bootstrap row with three columns - col-sm-2, col-sm-8, and col-sm-2. In the first column, I plan to insert a card, in the second column some text, and in the third column a tooltip. This row will be repeated multiple times. As a beginner ...

Insert a new row into a table using tablesorter jQuery

Is it possible to add a row to a table built with the jQuery library "Tablesorter" by simply clicking on a button? I have tried this approach, but unfortunately, the new row does not inherit the CSS properties of Tablesorter. As a result, I am unable to se ...

Custom control experiencing issues with vertical alignment

I am currently working on creating a basic filter bar that includes a custom control element. My intention is to align the element vertically, but so far, I have been unsuccessful. Below is the code for the filter bar: <div class="bg-light p-3 card"&g ...

Fundamentals of object property in jQuery and Javascript

Currently working on creating a new property named startPosition and setting the top property to equal the button's current top value in CSS. Below is the jQuery code snippet: var morphObject = { button: $('button.morphButton'), c ...

Can we set a restriction on the maximum number of children a particular div can contain?

I'm trying to find a way to restrict the number of children in a div. The concept is that there is a selected commands div where you can drag and drop available commands (essentially buttons) from another div called available commands. However, I wan ...

Trigger the opening of a class or window upon pressing the button

Good evening, I'm attempting to create a functionality where pressing a specific button will open a window. However, I am unsure of how to achieve this using CSS classes. My initial thought was to add a new class in the CSS file and call it every ti ...

Styling Vue.js router links with the CSS property

I need help changing the default CSS properties for text within a router-link tag. Currently, the text is purple with an underscore and I want to modify the font, size, and color but I'm not sure where to focus in my CSS. I have already attempted usin ...

Tips for adjusting the border radius of a dropdown box on a Bootstrap website within the CodeIgniter framework

I attempted to use CSS in order to achieve rounded corners for my dropdown box, but unfortunately it didn't have the desired effect. What I am aiming for is to round the corners of my dropdown box, similar to a search bar or search button. https://i. ...

What causes the off-canvas menu to displace my fixed div when it is opened?

Using the Pushy off-canvas menu from GitHub for my website has been great, but it's causing some trouble with my fixed header. When I scroll down the page, the header sticks perfectly to the top, but once I open the off-canvas menu, the header disappe ...

Steps for showing an alert notification when the form field is empty

Within my script, there is a Bootstrap form where I am facing an issue. If the user fails to select a language and leaves the value blank or as "Select Language," I want to display an alert message prompting them to choose a language before proceeding to t ...

PHP mistakenly outputs content that is not enclosed within tags

After discovering StackOverflow, I couldn't resist sharing my PHP code conundrum with the incredible community here! So, in a WordPress template, I have this chunk of PHP: echo '<div class="thumbnail">'; echo the_post_thumbnail(); ec ...

What could be causing my React Router component to display incorrect styling?

While working with react-router, I encountered an issue where the text from the routed page started appearing in the navbar. Even though I separated it as its own component and it functions correctly, this specific problem persists. As a newcomer to React, ...

Background-color in CSS can be seen overlapping border on table cells in Internet Explorer

I am encountering an issue with a table containing two simple table cells: <table> <tr> <td>Test1</td> </tr> <tr> <td>Test2</td> </tr> </table> When I apply the following CSS to ...

Navigating a puzzle menu in web design can be simplified with a few easy steps

When it comes to executing my design ideas, I have 2 options in mind. My main concern is which one will offer more flexibility for adding animations and tinkering with CSS later on. The first idea involves a navigation menu where hovering over an item mak ...

Can JQuery be used to identify the CSS styles applied to selected elements?

Highlight the text by selecting it and clicking a button. If the text is already highlighted, clicking the button should make the selected text return to normal. Can this be achieved using jQuery and a single button? Is it possible to identify the CSS st ...

Do I need to have the "firequery" extension installed if I am using jQuery in my projects?

Is it recommended to keep "firequery" installed when using jQuery in projects? Can this tool be beneficial for development? ...

What's the trick to aligning navigation items side by side?

I'm currently working with the code snippet below: header { height: 110px; width: 100%; margin-bottom: 130px; position: fixed; top: 0; z-index: 11; /*additional styling removed*/ } nav ul { list-style: none; paddin ...

Tips for positioning a button directly under a horizontal navigation bar

How can I center a button just below a horizontal navigation menu, without overlapping the menu items and ensuring proper spacing when the menu expands? <!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> < ...