How to make container shrink-to-fit child elements as they wrap?

A Flexible Layout must have a parent element having display property set to flex. Direct child elements of the flexible container automatically become flexible items.

Approach:

  • The element below represents a flex container with four flex items.
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    <div id="container">
        <ul>
            <li>icecream</li>
            <li>sandwich</li>
            <li>juice</li>
            <li>coldrink</li>
               </ul>
    </div>

    chevron_right

    
    

  • Parent Element (Container)

    The flex container becomes flexible by making the display property to flex:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    .flex-container {
      display: flex;
    }

    chevron_right

    
    

  • The flex-wrap Property

    The flex-wrap property states whether the flex items should wrap or not.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    .flex-container {
      display: flex;
      flex-wrap: wrap;
    }

    chevron_right

    
    

    It wraps value specifies that the flex items will wrap if necessary

  • Text Alignment

    The text-align property sets the horizontal alignment of a text. A text can be left or right-aligned, centered, or justified.



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    h1 {
      text-align: center;
    }
      
    h2 {
      text-align: left;
    }
      
    h3 {
      text-align: right;
    }

    chevron_right

    
    

  • CSS Setting height and width

    The height & width properties are used to set the height & width of an element.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    div {
      height: 500px;
      width: 50%;
    }

    chevron_right

    
    

  • CSS Padding

    The CSS padding property is used to generate space around an element, inside borders.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    div {
      padding-top: 100px;
      padding-right: 50px;
      padding-bottom: 1000px;
      padding-left: 100px;
    }

    chevron_right

    
    

  • CSS Margins

    The CSS margin property is used to create space around elements, outside borders.

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    p {
      margin-top: 100px;
      margin-bottom: 100px;
      margin-right: 150px;
      margin-left: 80px;
    }

    chevron_right

    
    

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
<style>
ul {
    display: flex;
    flex-wrap: wrap;
    margin: 0;
    padding: 0;
    border: 5px solid green;
}
  
li {
    list-style-type: none;
    border: 1px solid gray;
    margin: 15px;
    padding: 5px;
    width: 200px;
    text-align: center;
}
</style>
<body>
<div id="container">
    <ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
        <li>6</li>
    </ul>
    <p> Geeks for Geeks </p>
</div></body>
</html>    

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.