Home > Net >  Difference between flex display property and block display property
Difference between flex display property and block display property

Time:11-09

What is the difference between flex and block display property?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .flex{
            display: flex;
            background-color: red;
            height: 200px;
        }
        .block{
            height: 200px;
            background-color: rgb(51, 170, 120);
            display: block;
        }
    </style>
</head>
<body>
    <div>Lorem ipsum dolor sit <p class="flex">Hello</p> adipisicing elit. Alias velit optio, quod tenetur omnis, in voluptas corporis voluptatem quaerat iure reprehenderit quasi accusamus sed dignissimos odio ex ipsam dolor consequatur.</div>

    <div>Lorem ipsum dolor sit <p class="block">Hello</p> adipisicing elit. Alias velit optio, quod tenetur omnis, in voluptas corporis voluptatem quaerat iure reprehenderit quasi accusamus sed dignissimos odio ex ipsam dolor consequatur.</div>
</body>
</html>

In this code both are working same. Can anyone help me please?? :(

CodePudding user response:

At first glance they look similar, but they are not... The key is that display: flex has access to the flexbox model feature. To check this in action, you can add to .flex class selector two properties: align-items: center; and justify-content: center;

  • display block "The element generates a block element box, generating line breaks both before and after the element when in the normal flow." MDN

  • display flex "The element behaves like a block element and lays out its content according to the flexbox model." MDN

  • Related