Languages

Flexbox 2 elements per row - 100% width of the page when each item have 10px of margin and padding

6 points
Asked by:
Savannah
559

How to arrange 2 elements per row using flex? Let's say total elements is 6, so we need to have 3 rows with 2 elements per row.

Each element needs to have margin and padding set to 10px.

1 answer
10 points
Answered by:
Savannah
559

Solution:

// ONLINE-RUNNER:browser;

<html>
<head>
  <style>

    div {
        border: 1px solid red;
    }

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

    div.child {
        margin: 10px;
        padding: 10px;
        flex: 40%;
    }

  </style>
</head>
<body>
  <div class="container">
    <div class="child">1</div>
    <div class="child">2</div>
    <div class="child">3</div>
    <div class="child">4</div>
    <div class="child">5</div>
    <div class="child">6</div>
  </div>
</body>
</html>
2 comments
Michael_Dias
If you have an even number of rows (try adding a new one), then, the last row will take the whole space. How to prevent that?
Root-ssh
&#64; Michael_Dias, try this solution: https://dirask.com/questions/CSS-How-to-use-flexbox-with-3-elements-per-row-keeping-last-elements-position-pr6Ymp
Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join