Go back

All about flexboxes

In this class we talked all about flexboxes.

Resources to reference

Codes used in this class

Basic Flex

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div class="container">
        <div class="box">
            box 1
        </div>
    
        <div class="box">
            box 2
        </div>

        <div class="box" id="important">
            box 3
        </div>
        <div class="box">
            box 4
        </div>
    </div>
</body>
</html>

style.css

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

.container{
    width: 80%;
    margin: 0 auto;
}

.box:nth-child(odd){
    background-color: #333;
    color: white;

}

.box:nth-child(even){
    background-color: blanchedalmond;
    color: black;   
}

.box{
    padding: 20px;
    
    height: 200px;
    flex-grow: 1;
}

.container{
    display: flex;
    /* justify-content: space-between; */
    /* align-items: center; */
    gap: 20px;
    flex-wrap: wrap;
    flex-direction: row;
}

Centering a div

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document 2</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <div>This page shows how to center a div in a page</div>
</body>
</html>

style.css

*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: Arial, Helvetica, sans-serif;
}

body{
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
}