Skip to content
Related Articles

Related Articles

Improve Article

How to set flexbox having unequal width of elements using CSS ?

  • Last Updated : 18 Jul, 2019

The flex property in CSS is the combination of flex-grow, flex-shrink, and flex-basis property. It is used to set the length of flexible items. The flex property is much responsive and mobile friendly. It is easy to positioning child elements and the main container.
The margin doesn’t collapse with the content margins. Order of any element can be easily changed without editing the HTML section. But few times we have an unequal width of the element also that time you can design the whole things in the CSS section.

Syntax:

flex: number;

Note: Elements width depend on the other elements and screen of your window in this case.

Example 1: Here you will see the two types of width flexbox is design by using CSS.




<!DOCTYPE html>
<html>
    <head>
        <title>Unequal width of Element | Flexbox</title>
        <style>
            h1{
                color:green;
            }
            div.flexcontainer{
                display: flex;
                min-height: 200px; 
                font-size:15px;
            }
   
            div.columns{
                flex: 1;
                padding: 10px;
   
            }
            div.columns:nth-of-type(even){
                flex: 2;
                  
            }
   
            div.columns:nth-of-type(odd){
                background: #85929E;
                color: white;
            }
            div.columns:nth-of-type(even){
                background: #A5DDEF;
                color: green;
            }
        </style>
    </head>
    <body>
        <center>
            <h1>GeeksforGeeks</h1>
    <div class="flexcontainer">
   
        <div class="columns">This is 1st column</div>
        <div class="columns">This is 2nd column</div>
        <div class="columns">This is 3rd column</div>
        <div class="columns">This is 4th column</div>
   
    </div>
    </body>
</html>                    

Output:

Example 2: In this example you will see 4 items each of them having unequal width compare to each other.




<!DOCTYPE html>
<html>
    <head>
        <title>Unequal width of Element | Flexbox</title>
        <style>
            h1{
                color:green;
            }
            div.flexcontainer{
                display: flex;
                min-height: 200px; 
                font-size:15px;
                border:2px solid orange;
            }
   
            div.columns{
                padding: 10px;
                color:white;
   
            }
            div.columns:nth-of-type(1){
                flex: 0.5;
                background: #1B2631;
            }
            div.columns:nth-of-type(2){
                flex: 1;
                background:#424949;
                  
            }
            div.columns:nth-of-type(3){
                flex: 2;
                background:#4D5656;
                  
            }
            div.columns:nth-of-type(4){
                flex: 3;
                background:#626567;
                  
            }
            th, td{
                border:1px solid white;
            }
   
              
        </style>
    </head>
    <body>
        <center>
            <h1>GeeksforGeeks</h1>
    <div class="flexcontainer">
   
        <div class="columns">ID</div>
        <div class="columns">Ph_no</div>
        <div class="columns">Name</div>
        <div class="columns">Adsress</div>
   
    </div>
    </body>
</html>                    

Output:




My Personal Notes arrow_drop_up
Recommended Articles
Page :