Show More / Show Less HTML Code

Show More / Show Less HTML Code
Code Snippet:show more/show less with javascript
Author: laxmi0411
Published: March 3, 2024
Last Updated: March 4, 2024
Downloads: 401
License: MIT
Edit Code online: View on CodePen
Read More

This  HTML code snippet helps you to create a show more / show less feature for content containers. It limits content visibility initially. Clicking “Show more” expands the content, and “Show less” collapses it. This functionality helps manage lengthy content visibility.

You can use this code on websites with long content, like blogs or product descriptions. The benefit is offering users control over content visibility, and enhancing readability.

How to Create Show More / Show Less HTML Code

1. Insert the HTML structure for your content containers. Each container should have a container class wrapping the content inside a content class.

<div class="container">
    <div class="content">
      <p>
        Content for container 1.
        orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
      </p>
    </div>
  </div>

  <div class="container">
    <div class="content">
      <p>
        Content for container 2.
       orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum
      </p>
    </div>
  </div>

2. Style your containers using CSS. Adjust the max-height property in the .content class to determine the initial height of the content to display. Add styling for the “Show more” button, defining its appearance and transitions.

.container {
      max-width: 600px;
      margin: 20px auto;
      font-family: Arial, sans-serif;
    }

    .content {
      max-height: 100px; /* Adjust the initial max-height as needed */
      overflow: hidden;
      transition: max-height 0.3s ease; /* Add smooth transition effect */
    }
.show-more {
    cursor: pointer;
      color: blue;
      text-decoration: none;
      
      background: linear-gradient(to bottom, #3a7bd5, #3a6073); /* Gradient background color */

3. Finally, place the JavaScript code inside a <script> tag or in an external JavaScript file. The code uses the document.querySelectorAll method to select all elements with the content class. For each content container, a “Show more” button is created and appended before the content.

Event listeners are attached to these buttons. When clicked, they toggle the max-height property of the content to reveal or hide the entire content.

document.addEventListener('DOMContentLoaded', function () {
      const contentContainers = document.querySelectorAll('.content');
      
      contentContainers.forEach((content, index) => {
        const showMoreButton = document.createElement('div');
        showMoreButton.classList.add('show-more');
        showMoreButton.innerHTML = '<span>&#43;</span> Show more';
        content.parentNode.insertBefore(showMoreButton, content.nextSibling);

        showMoreButton.addEventListener('click', function () {
          if (content.style.maxHeight) {
            content.style.maxHeight = null;
            showMoreButton.innerHTML = '<span>&#43;</span> Show more';
          } else {
            content.style.maxHeight = content.scrollHeight + 'px';
            showMoreButton.innerHTML = '<span>&#45;</span> Show less';
          }
        });
      });
    });

That’s all! hopefully, you have successfully integrated this Show More/Show Less HTML code into your project. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X