0

I've created a flex box container and 3 child divs in it and
I want to keep the sizes of the 3 in same the same ratio (1:1:1)
but when I add some text into the child divs, they change ratio to cover up for the text overflow

Like this:

#container
{
    display: flex;
    width: 100%;
    height: 100px;
    text-align:  center;
}
.item
{
    color: white;
  flex-grow:1;

  
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>

</head>
<body>
before text:
<div id="container"> 
 <div class="item" style="background-color:red;"></div>
 <div class="item" style="background-color:green;"></div>
 <div class="item" style="background-color:blue;"></div>
</div>
<br>
after text:
<div id="container"> 
 <div class="item" style="background-color:red;">hello there</div>
 <div class="item" style="background-color:green;">hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii</div>
 <div class="item" style="background-color:blue;">no</div>
</div>
</body>
</html>

so how do I maintain the aspect ratio while adding text?

NOTE:
hiding the overflow is acceptable

Gucal
  • 842
  • 1
  • 11
  • 19
cak3_lover
  • 1,440
  • 5
  • 26

4 Answers4

2

Change flex-grow:1 to flex:1

#container
{
    display: flex;
    width: 100%;
    height: 100px;
    text-align:  center;
}
.item
{
    color: white;
  flex:1;
 min-width:0; /* thanks to Temani */
  
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>

</head>
<body>
before text:
<div id="container"> 
 <div class="item" style="background-color:red;"></div>
 <div class="item" style="background-color:green;"></div>
 <div class="item" style="background-color:blue;"></div>
</div>
<br>
after text:
<div id="container"> 
 <div class="item" style="background-color:red;">hello there</div>
 <div class="item" style="background-color:green;">hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii</div>
 <div class="item" style="background-color:blue;">no</div>
</div>
</body>
</html>
Paulie_D
  • 107,962
  • 13
  • 142
  • 161
1

You may try to add flew-basis: 0 to .item. See updated example below:

#container
{
    display: flex;
    width: 100%;
    height: 100px;
    text-align:  center;
}
.item
{
    color: white;
  flex-grow:1;
  flex-basis: 0;

  
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>

</head>
<body>
before text:
<div id="container"> 
 <div class="item" style="background-color:red;"></div>
 <div class="item" style="background-color:green;"></div>
 <div class="item" style="background-color:blue;"></div>
</div>
<br>
after text:
<div id="container"> 
 <div class="item" style="background-color:red;">hello there</div>
 <div class="item" style="background-color:green;">hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii</div>
 <div class="item" style="background-color:blue;">no</div>
</div>
</body>
</html>
Brebber
  • 2,986
  • 2
  • 9
  • 19
  • Sorry. I have been to fast. Have not seen that the answer has just been given. Additional @gowthamRajJ presented an alternative solution. – Brebber Feb 27 '21 at 23:06
0

Issue is caused by the default value flex-basis: auto for .item. You might want to consider adding flex-basis: 33.33%(100/no of columns) and also a max-width of same value.

In your case flex-basis: 0 would also work. Check: Make flex-grow expand items based on their original size

Gowtham Raj J
  • 937
  • 9
  • 26
-1

Simply, change flex-grow: 1; to flex: 1 or to width: 33.3333% to .item

#container
{
    display: flex;
    width: 100%;
    height: 100px;
    text-align:  center;
}
.item
{
    color: white;
    width: 33.3333%;

  
}
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>

</head>
<body>
before text:
<div id="container"> 
 <div class="item" style="background-color:red;"></div>
 <div class="item" style="background-color:green;"></div>
 <div class="item" style="background-color:blue;"></div>
</div>
<br>
after text:
<div id="container"> 
 <div class="item" style="background-color:red;">hello there</div>
 <div class="item" style="background-color:green;">hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii</div>
 <div class="item" style="background-color:blue;">no</div>
</div>
</body>
</html>
mowx
  • 77
  • 8