HTML elements size adaptation

Question:
How do I adapt the HTML element size based on the dynamic content of the HTML code inside?
Product:
Wix Studio

What are you trying to achieve:
I am creating via HTML an accordion menu. I would the page and the elements adapt once I click to open the accordion and show the content inside. Now when I click the accord meno there is no adaptation and the content is the visualized “cut”.

What have you already tried:
This is the HTML of my accordion:

<div class="accordion-container">
  <!-- Accordion 1 -->
  <div class="accordion">
    <div class="accordion-header">
      <span class="package-name">Weekend Escape</span>
      <span class="icon">˅</span>
    </div>
    <div class="accordion-content">
      <h3>Weekend Escape</h3>
      <p><strong>Price:</strong> €250 per night</p>
      <ul>
        <li>Welcome drink on arrival</li>
        <li>Daily breakfast for two</li>
        <li>Access to the wellness spa</li>
        <li>Late checkout (subject to availability)</li>
      </ul>
    </div>
  </div>

  <!-- Accordion 2 -->
  <div class="accordion">
    <div class="accordion-header">
      <span class="package-name">Luxury Retreat</span>
      <span class="icon">˅</span>
    </div>
    <div class="accordion-content">
      <h3>Luxury Retreat</h3>
      <p><strong>Price:</strong> €450 per night</p>
      <ul>
        <li>Private suite with a sea view</li>
        <li>Gourmet dinner for two</li>
        <li>Complimentary bottle of champagne</li>
        <li>24/7 concierge service</li>
      </ul>
    </div>
  </div>
</div>

<style>
  /* Import Cormorant Garamond Semi Bold */
  @import url('https://fonts.googleapis.com/css2?family=Cormorant+Garamond:wght@600&display=swap');

  .accordion-container {
    margin: 0 auto;
    width: 90%;
    max-width: 600px;
  }

  .accordion {
    border: 1px solid #E1D1B6;
    border-radius: 8px;
    margin-bottom: 16px;
    box-shadow: 0px 2px 4px rgba(0, 0, 0, 0.1);
    overflow: hidden;
  }

  .accordion-header {
    background-color: white;
    color: #E1D1B6;
    font-family: 'Cormorant Garamond', serif;
    font-weight: 600;
    font-size: 18px;
    padding: 12px 16px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    border-bottom: 1px solid #E1D1B6;
  }

  .accordion-header.active {
    background-color: rgba(225, 209, 182, 0.2);
  }

  .accordion-content {
    background-color: white;
    padding: 0 16px;
    max-height: 0;
    overflow: hidden;
    font-family: 'Cormorant Garamond', serif;
    font-weight: 600;
    font-size: 16px;
    color: black;
    transition: max-height 0.3s ease, padding 0.3s ease;
  }

  .accordion-content h3 {
    font-size: 18px;
    color: #E1D1B6;
    margin: 16px 0 8px 0;
  }

  .accordion-content ul {
    list-style-type: circle;
    padding-left: 20px;
    color: #E1D1B6;
  }

  .accordion-content ul li {
    margin-bottom: 8px;
  }

  .accordion-content strong {
    color: #E1D1B6;
  }

  .icon {
    transition: transform 0.3s ease;
  }

  .accordion-header.active .icon {
    transform: rotate(180deg);
  }
</style>

<script>
  // Function to send the updated height to Wix parent
  function updateHeight() {
    const height = document.documentElement.scrollHeight;
    window.parent.postMessage({ type: 'resize', height: height }, '*');
  }

  document.addEventListener("DOMContentLoaded", () => {
    // Initial height post
    updateHeight();

    // Select all accordion headers
    const headers = document.querySelectorAll(".accordion-header");

    headers.forEach(header => {
      header.addEventListener("click", () => {
        const accordionContent = header.nextElementSibling;
        const icon = header.querySelector(".icon");

        // Toggle active state for the header
        header.classList.toggle("active");

        // Toggle max-height for smooth height transition
        if (header.classList.contains("active")) {
          accordionContent.style.maxHeight = accordionContent.scrollHeight + "px";
          accordionContent.style.padding = "16px";
          icon.style.transform = "rotate(180deg)";
        } else {
          accordionContent.style.maxHeight = "0";
          accordionContent.style.padding = "0 16px";
          icon.style.transform = "rotate(0deg)";
        }

        // Post the updated height after content toggles
        setTimeout(updateHeight, 300); // Delay to allow transition
      });
    });
  });

  // Recalculate height when the window is resized
  window.addEventListener("resize", updateHeight);
</script>

Additional information:
HTML element option is set to “Scale size proportionally”

The issue you’re experiencing likely stems from the way Wix Studio’s “Scale size proportionally” setting interacts with the dynamic content of your accordion. The page may not automatically adapt to the content height changes when the accordion expands, causing the content to be clipped.

Thanks. And how do I may solve?

Firstly , Double-check that Wix is set up to listen for the resize message and update the HTML container height accordingly. This is critical for dynamic height adjustments.

Thx for reply. However I do not know how to double check? is it a setting in my main dashboard or in the editing ? where I should check this?

you can try this method for ur HTML element if u face further issue u can contact us on fiza@astrosia.in where we can further help you overcome your problem.

Enable Stretch-to-Fit for the HTML Element

  • Open your Wix Editor.
  • Select the HTML element you are using for the accordion or dynamic content.
  • In the Settings panel for the HTML element, look for Size Options or similar.
  • Ensure that the option for Stretch to Fit or “Auto-Adjust Height” is enabled (not “Scale Size Proportionally”).
  • This tells Wix to dynamically adjust the container size based on the content of the HTML element.

Thanks again. Now the element is fully showed but the height is simply increased. So now the issue is the opposite. With the accordion items closed, there is an empty space below the accordion.

okay, to have better understanding of your issue can we connect over a meet , could connect on whatsapp: 8697581130 if you are interested.

1 Like

you could also connect on fiza@astrosia.in in you face further issues . would be happy to help you