HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<!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>
/* 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;
}
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();
});
Also see: Tab Triggers