EN
CSS - conditional fixed margin between items with flex-box
7 points
In this short article we would like to show trick, how to create conditional fixed margin between items with flex-box using only CSS.
Simple example:
xxxxxxxxxx
1
2
<html>
3
<head>
4
<style>
5
6
div.container {
7
border: 1px solid red;
8
width: 430px; /* <------------- change it or remove to see full effect */
9
box-sizing: content-box;
10
overflow: hidden; /* <--------- optional */
11
}
12
13
div.flex {
14
/* v-space h-space <--- to remove top and left margins */
15
/* | | */
16
margin: -10px 0 0 -10px;
17
flex-wrap: wrap; /* <---------- required if we want wrapping for items */
18
display: flex; /* <------------ required */
19
}
20
21
div.item {
22
/* v-space h-space <--- to set vertical and horizontal space */
23
/* | | */
24
margin: 10px 0 0 10px; /* <---- required */
25
background: orange;
26
width: 100px;
27
height: 100px;
28
}
29
30
</style>
31
</head>
32
<body>
33
<div class="container">
34
<div class="flex">
35
<div class="item">1</div>
36
<div class="item">2</div>
37
<div class="item">3</div>
38
<div class="item">4</div>
39
<div class="item">5</div>
40
<div class="item">6</div>
41
</div>
42
</div>
43
</body>
44
</html>