
Collapsible text in HTML can be achieved without JavaScript, which is great news for developers who want to keep their code lightweight and accessible. This can be done using the HTML details and summary elements.
The details element is a container for the collapsible content, while the summary element is the button that toggles the display of the content. The summary element is required for the details element to work properly.
By using these elements, you can create collapsible text that is both functional and semantic. This means that not only does it work as expected, but it also provides meaning to the structure of your HTML document.
Additional reading: Summary Html Tag
HTML Structure
To create a basic HTML structure for collapsible text, you'll need to define a section tag with a class name "accordion". This is the foundation of the accordion structure.
The accordion structure involves wrapping all other elements inside the section tag. You'll place an input tag (with type checkbox or radio) just before the label tag. The label tag will be used for the visible short heading of the accordion.
Take a look at this: Html Section Element

The div tag, placed after the label tag with a class name "content", will be used for the detailed information. You can add multiple collapsible elements just like this structure by updating the input and label IDs.
For example, if you want to add a second accordion, you'll increase the ID number for both the input and label. This is a simple way to create multiple collapsible elements without having to create a new section tag.
Radio input vs. checkbox input:
- Radio input: Close other opened accordion when select a new one but not toggle same accordion.
- Checkbox input: Toggle (show / hide) accordion contents but not close others.
Adding Collapsible Text
To add collapsible text in HTML, you need to use the correct tags and class names.
A collapsible content chunk must have a container div around the whole chunk, a heading that serves as the controller, and a target div that surrounds the content that you want to expand/collapse.
For the container, use a div with class="openberkeley-collapsible-container". This is the foundation of the collapsible text.
The controller is the heading that serves as the button to expand or collapse the content. Use a heading (h2, h3, h4, or h5) with class="openberkeley-collapsible-controller" for this purpose.
Intriguing read: Html Collapse
The target content is the text that will be hidden or shown when the controller is clicked. Use a div with class="openberkeley-collapsible-target" for this.
Here's a rundown of the required tags and class names:
- Container: div with class="openberkeley-collapsible-container"
- Controller: heading (h2, h3, h4, or h5) with class="openberkeley-collapsible-controller"
- Target content: div with class="openberkeley-collapsible-target"
To get started, copy and paste the sample code into your HTML editing window, and replace the text in the controller heading and target content tags with your own expand/collapse text.
Accordion Styles
To style and add functionality to an accordion, you'll need to hide the input element using CSS.
You can use the display property to hide the input tag, but this method isn't compatible with the CSS transition attribute for smoothness.
To bring smoothness while toggling (hiding and showing) text contents, you can use the 0 value for height and overflow to hide, which is a recommended method.
Keep in mind that you can also use the display none property, but it's not compatible with the CSS transition attribute.
For your interest: Html Property Attribute
You can define the height value with the :checked selector to smoothly expand and collapse text in the accordion.
Setting the height value to auto will not expand contents smoothly, but other functionalities will remain the same.
You can use the Font Awesome angle up icon and rotate it to 180° when text contents are expended, but you can also remove the label:before code block if you don't want to use an arrow down icon with the accordion heading.
On a similar theme: Html Accordion
Existing Solutions
Existing solutions for collapsible text in HTML have been proposed by the community. I found a nice post on codingartistweb.com that shows exactly what I needed, with written as well as a video tutorial.
The code is only HTML and CSS, with no JavaScript involved. It uses an invisible checkbox to achieve the effect.
This solution works great for a single expandable text section, but it fails when there are multiple sections. In such cases, generating pseudo-random IDs on the client or server side is necessary.
Curious to learn more? Check out: B Tag in Html
Content Markup
To create collapsible text in HTML, you need to understand the basic structure of a collapsible content chunk. Each chunk must have a container div, a controller, and a target content div. For the container, use a div with class="openberkeley-collapsible-container".
When it comes to the controller, use a heading (h2, h3, h4, or h5) with class="openberkeley-collapsible-controller". This will serve as the clickable button that expands or collapses the content.
The target content is wrapped in a div with class="openberkeley-collapsible-target". This is the text that will be hidden or shown when the controller is clicked.
Here's a breakdown of the required tags and class names:
- Container: div with class="openberkeley-collapsible-container"
- Controller: heading (h2, h3, h4, or h5) with class="openberkeley-collapsible-controller"
- Target content: div with class="openberkeley-collapsible-target"
To create a collapsible block of content, you can also use the data-role="collapsible" attribute on the container. This will add a "+" icon to the left of the header and make it clickable.
Additional reading: Collapsible Text Html
In HTML structure, you can define a collapsible section using a section tag with class name “accordion”. This will wrap all other elements inside it. The basic expand and collapse structure includes an input tag (with type checkbox or radio) before the label tag, which is used for the visible short heading of the accordion.
Worth a look: How to Link Nav to Section of Page Html
JavaScript-Free Solution
You can create a JavaScript-free solution for collapsible text in HTML using the basic expand and collapse structure.
To start, you'll need to define a section tag with a class name of "accordion" and wrap all other elements into it. This structure includes an input tag (with type checkbox or radio) placed just before the label tag, which will be used for the visible short heading of the accordion.
The div tag (with class name "content") placed after the label tag is used for detailed information. You can add multiple collapsible elements just like this structure, updating the input and label IDs for each one.
Recommended read: The Html Canvas Element Is Used to
One thing to keep in mind is that using radio input will close other opened accordions when selecting a new one, but not toggle the same accordion. On the other hand, using checkbox input will toggle (show/hide) accordion contents, but not close others.
Here's a summary of the main output difference between radio and checkbox type:
- Radio input: Close other opened accordion when select a new one but not toggle same accordion.
- Checkbox input: Toggle (show / hide) accordion contents but not close others.
This means you can choose the type of input that best suits your needs, depending on whether you want to close other opened accordions or toggle the same one.
Types and Sets
Collapsible text in HTML can be categorized into two main types: basic collapsibles and grouped sets.
Basic collapsibles are a great starting point for adding interactive content to your website.
Grouped sets, on the other hand, allow you to combine multiple collapsibles into a single accordion widget that can be expanded and collapsed.
A unique perspective: Basic Html Editor
Types
In mathematics, a set is an unordered collection of unique elements, known as a set of elements.

A set can be defined using a variety of notations, such as curly brackets { } or the set builder notation { x | x satisfies a certain property }.
A set is not a list, and the order of its elements does not matter.
In a set, each element is unique and cannot be repeated.
For example, the set {1, 2, 3} is the same as the set {3, 2, 1}.
A set can be finite or infinite, and it can contain any type of element, including numbers, letters, or even other sets.
A set can be thought of as a container that holds a collection of elements.
In programming, sets are often used to eliminate duplicate elements from a list or to check if an element is already present in a collection.
For instance, in Python, the set data type can be used to remove duplicate elements from a list.
A set can also be used to check if an element is present in a collection, which can be useful for tasks such as validating user input.
In mathematics, a type is a way to categorize sets based on their properties.
Explore further: Scroll X Html

For example, the set of all integers is a type of set that is known as a set of integers.
A type can be thought of as a label or a category that describes the properties of a set.
Types can be used to define the properties of a set, such as its size or its elements.
In programming, types are often used to define the properties of a variable, such as its data type or its size.
For instance, in Python, the type() function can be used to determine the data type of a variable.
Related reading: Is Html Used to Create Web Pages
Sets
Sets can be combined to create a grouped set that acts like an accordion widget, making it easy to expand and collapse different sections.
This feature is especially useful for organizing and presenting large amounts of information in a compact and user-friendly way.
Featured Images: pexels.com


