Open In App

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:



Example:




<!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>    

Output:


Article Tags :