How to Dynamically Generate Flex Gap Styles and Responsive Reverse with JavaScript for Container Element

Hi Funnelishers,

I hope you guys are doing well. Today we are going to learn something about the Container element.

The Container is a display flex element. When we add two or more boxes to the Container element, they will be placed side by side.

If an image is added to the first box and some text to the second box, they will look like the following screenshot if no margin or padding is used.

In the screenshot, we notice there is no gap between the image in the first box and the text in the second box. However, we can create space using the CSS gap property . To do this, we define a class called flex_gap_50 and add it to the container.

2024-6-9_16-38-43 (1)

Next, we add the following CSS in More Actions > Custom Codes > CSS :

.flex_gap_50 {
  gap: 50px;
}

Now, we will see a 50px space between the image and the text.

But we might need different spaces for different situations, which is why we developed a JavaScript code that dynamically generates the gap for Container boxes.

Here is the code:

<script>
// ----- Dynamic Gap Styles Generation -----
document.addEventListener('DOMContentLoaded', function () {
  // Create a style element and append it to the document head for dynamic gap styles
  var styleElement = document.createElement('style');
  document.head.appendChild(styleElement);

  // Function to generate dynamic CSS rules for flex gap
  function generateGapStyles() {
    var styleRules = '';
    var processedGaps = {};
    var gapElements = document.querySelectorAll('[class*="flex_gap_"]');

    gapElements.forEach(function (el) {
      var classes = el.className.split(' ');
      classes.forEach(function (className) {
        if (className.startsWith('flex_gap_') && !processedGaps[className]) {
          var gapValue = className.split('_').pop();
          if (!isNaN(gapValue)) {
            processedGaps[className] = true;
            styleRules += `.${className} { gap: ${gapValue}px; }\n`;

            // Add the mobile-specific gap style
            styleRules += `@media (max-width: 768px) { .${className} { gap: 10px; } }\n`;
          }
        }
      });
    });

    styleElement.innerHTML = styleRules;
  }

  // Call the function once to apply initial gap styles
  generateGapStyles();

  // Start observing the document for changes in the DOM
  var observer = new MutationObserver(function (mutationsList) {
    for (var mutation of mutationsList) {
      if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
        generateGapStyles();
      }
    }
  });

  // Start observing the document for changes in the DOM
  observer.observe(document.body, {
    childList: true,
    subtree: true
  });
});
</script>

Copy the code and go to More Actions > Custom Codes > Body HTML and paste it.

Now, you do not need to write different gaps for different Containers. Just add the appropriate class. For example, if you have 3 containers and need different spaces: 25px gap for the first container, 50px gap for the second, and 70px gap for the third, just add the classes flex_gap_25, flex_gap_50, and flex_gap_70 respectively to the CSS classes field.

If you do not understand the topic or have any difficulties implementing this, feel free to comment below.

Hi anwershif@funnelish hope you are doing well
I want to create and know how i create the custom coded sections in checkout pages and how to fetch these codes with our needs

You can use custom code element to do that.

Where and how i can find custom code elements can you please tell me

Could you tell me more about the code you want to add? Then I can tell you how and where…

As you can see in the picture i add the slides pictures on left side of the container keep in mind i add this pictures slides with funnelish images slider tool i mean without custom coded.And on the right side of the container if i click on the circle button color also should be change on the left which is with out custom coded and if i change the payment section the progress bar also change and increased
Can you please tell me step by step or where i can learn these kind of things

Hi @anwerashif please tell me i need it

@Awais_Shabbir Take a look at this video…