Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html>

<head>
  <title>Convert Yoast SEO FAQ Block into an Accordion with Collapsible Headers</title>
  <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>

<body>
  <div class="schema-faq">
    <div class="schema-faq-section" id="faq-question-1687650041365">
      <strong class="schema-faq-question">What is the Yoast SEO FAQ Block?</strong>
      <p class="schema-faq-answer">
        The Yoast SEO FAQ Block is a feature provided by the Yoast SEO plugin, which is a popular SEO plugin for WordPress websites. The FAQ Block is a Gutenberg block that allows you to easily create and add FAQ content to your WordPress pages or posts.

        With the Yoast SEO plugin installed and activated, you can utilize the FAQ Block to create structured FAQ content that is optimized for search engines. The block provides a user-friendly interface where you can add questions and corresponding answers.
      </p>
    </div>
    <div class="schema-faq-section" id="faq-question-1687650053029">
      <strong class="schema-faq-question">How do I convert the Yoast SEO FAQ Block to an accordion?</strong>
      <p class="schema-faq-answer">
        To change the default Yoast SEO FAQ Block into an accordion, you'll need to apply custom CSS and JavaScript to your theme. The following styles define the visual presentation of the Yoast SEO FAQ Block on the frontend, including the appearance of questions, the toggle functionality of expanding and collapsing answers, and specific styles for the block editor environment.
      </p>
    </div>
  </div>
</body>

</html>
              
            
!

CSS

              
                /* Frontend Styles */

.schema-faq-section {
  font-family: Arial;
  background: white;
  margin: 1rem 0;
  border: 2px solid rgba(0, 0, 0, 0.13);
  border-radius: 10px;
  box-shadow: 2px 2px 6px 2px rgba(0, 0, 0, 0.03);
}

.schema-faq-question {
  cursor: pointer;
  display: flex;
  align-items: center;
  transition: opacity ease 0.25s;
  padding: 1rem;
  color: var(--wp--preset--color--primary);
  font-size: var(--wp--preset--font-size--x-large);
}

.schema-faq-question:hover {
  color: var(--wp--preset--color--secondary);
}

.schema-faq-question:after {
  width: 16px;
  height: 20px;
  display: inline-block;
  margin-left: auto;
  margin-right: 5px;
  vertical-align: top;
  color: inherit;
  content: "+";
}

.schema-faq-question.expanded:after {
  content: "-";
}

.schema-faq-question:hover {
  opacity: 0.75;
}

.schema-faq-answer {
  padding: 0 1rem 1rem 1rem;
  display: none;
}

.schema-faq-answer.default {
  display: block;
}

/* Backend Styles: Ensure all content is visible in the block editor */

.editor-styles-wrapper .schema-faq-question {
  cursor: text;
}

.editor-styles-wrapper .schema-faq-answer {
  display: block;
}

              
            
!

JS

              
                jQuery(function ($) {
  var yoast = {
    accordion: function () {
      var isAnimating = false; // Flag variable to track animation state

      $(".schema-faq-section")
        .find(".schema-faq-question")
        .click(function (event) {
          event.stopPropagation(); // Stop event propagation

          if (isAnimating) {
            return; // Ignore click if animation is in progress
          }

          isAnimating = true; // Set animation flag

          var answer = $(this).nextAll(".schema-faq-answer").eq(0);

          // Expand or collapse this panel
          answer.slideToggle(250, function () {
            $(this).toggleClass("expanded");
            $(this).prev(".schema-faq-question").toggleClass("expanded"); // Toggle 'expanded' class on question element
            isAnimating = false; // Reset animation flag after animation completes
          });

          // Hide the other panels
          $(".schema-faq-answer").not(answer).slideUp("fast");
        });

      $(".schema-faq-section .schema-faq-question").click(function (event) {
        event.stopPropagation(); // Stop event propagation

        if (isAnimating) {
          return; // Ignore click if animation is in progress
        }

        isAnimating = true; // Set animation flag

        $(".schema-faq-section .schema-faq-question")
          .not($(this))
          .removeClass("expanded");
        $(this).toggleClass("expanded");

        isAnimating = false; // Reset animation flag
      });
    }
  };

  yoast.accordion();
});

              
            
!
999px

Console