"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to achieve 1px Flex project spacing in the image library?

How to achieve 1px Flex project spacing in the image library?

Posted on 2025-04-13
Browse:567

How to Achieve 1px Spacing Between Flex Items in an Image Gallery?

How to Adjust Spacing in Image Gallery Flex Items with Margins

In flexbox layouts, items with flex values of '0 0 25%' have a fixed width and do not shrink or grow. To add space between these items while maintaining a grid-like structure, margins can be applied. However, applying a 1% margin results in gaps that may be too large.

To achieve a 1px margin, use the flex value '1 0 22%'. This sets the flex-basis to 22%, ensuring that there are only four items per row. The flex-grow value is set to 1, allowing the items to grow and fill the remaining space left by the margins. This creates a subtle 1px gap between the items.

#foto-container {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
  margin: 10px;
}

.foto {
  flex: 1 0 22%;
  min-height: 50px;
  margin: 1px;
  background-color: red;
}
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3