Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you fill the horizontal space in a line of flex children?

.flex {
    flex-direction: row;
    flex-wrap: nowrap;
    justify-content: space-around;
} 
<div class="flex">
  <div class="child">
    <svg></svg>1
  </div>
  <div class="child">
    <svg></svg>2
  </div>
  <div class="child">
    <svg></svg>3
  </div>
</div>

Currently the width of the child divs is determined by the size of the svg. I would like them to take up all available room in the row, so that there are three divs each taking 33% width. Can I do this with flexbox?

like image 764
Loren Avatar asked Sep 26 '14 00:09

Loren


People also ask

How do I fill my flexbox space?

Use the flex-grow property to make a flex item consume free space on the main axis. This property will expand the item as much as possible, adjusting the length to dynamic environments, such as screen re-sizing or the addition / removal of other items.

How do you make a div fill a remaining horizontal space?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent.

How do you make flex items arrange themselves vertically instead of horizontally?

what you are looking for is the 'align-content' property which is default set to 'stretch' and justifies elements vertically (the cross-axis). Opposed to 'justify-content', default 'flex-start' which justifies elements horizontally (main-axis). 'align-self', default 'auto', can be used to control individual items.


1 Answers

You do this simply by setting flex-grow on each flex-item's, in your case child's, css. Like this:

.child{
    background: lightblue;
    flex-grow: 1;
}

Complete Example: (parent component needs to be display: flex, I removed the irrelevant wrap and justify-content)

 <!DOCTYPE html>
    <html>
    <head>
    <style>
    body {background-color: powderblue;}
    h1   {color: blue;}
    .flex {
        display: flex;  
    }
    .child {
        background: orange;
        height: 50px;
        border: 1px blue dashed;
    
        flex-grow: 1;
    }
    </style>
    </head>
    <body>
        <h1>How to fill horizontal space in line of flex children</h1>
        <div class="flex">
            <div class="child">
              <svg>
            </div>
            <div class="child">
              <svg>
            </div>
            <div class="child">
              <svg>
            </div>
          </div>
    </body>
    </html>
like image 77
wojjas Avatar answered Oct 02 '22 21:10

wojjas