How can I show the remaining paragraph text upon clicking a button in a responsive manner using CSS and JavaScript?

I want to add a nice animation effect to the height of my text when a button is clicked. I've managed to achieve this by setting a height: 30px with overflow: hidden initially, and then toggling a class with height: 300px. However, I'm facing an issue with making it responsive. While it looks good on mobile devices, increasing the resolution makes the height too small.

Is there a way to adjust the height based on the screen width? Or are there other methods to hide part of the text responsively?

Below is the code snippet:

SCSS:

.about__body{
    @include displayFlex($direction: column, $align-item: center);
    gap: 20px;
    &-content {
        height: 60px;
        overflow: hidden;
        transition: all 400ms ease-in-out;
    }
}

.about__body-content-show {
    transition: all 400ms ease-in-out;
    height: auto;
}

JavaScript:

let btnAbout = document.getElementById("btn--about")
let contentAbout = document.querySelector(".about__body-content")

const onClickShow = () => {
    btnAbout.addEventListener('click', (e) => {
        e.preventDefault()
        contentAbout.classList.toggle('about__body-content-show')
    })
}
onClickShow()

Answer №1

To accomplish this, you can utilize JavaScript to adjust the height of your container to match the height of your content.

document.getElementById('expand').addEventListener('click', function() {
     document.querySelector('.content-holder').style.height = document.querySelector('.content').offsetHeight + 'px';
     document.querySelector('.content-holder').classList.toggle('collapsed');
});

You can see a demonstration of this in action here: https://jsfiddle.net/sdjhetq1/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Element within container div being forced out of view (off-screen)

I'm in the process of developing a responsive website and my goal is to keep all my divs centered at all times. However, I've encountered an issue where using the flex attribute in my container causes the bootstrap cards to go offscreen. Can any ...

An issue has been identified where a single image is not displaying properly on Android devices, as well as IE7 and

Hey there! I recently created a Wordpress site and it seems that the Art Below logo isn't appearing in IE7, IE8, and Android for some reason. Would anyone be willing to help troubleshoot this issue? If you have fresh eyes, please take a look at the f ...

Converting Cookies to Numeric Values in JavaScript: A Step-by-Step Guide

I'm currently developing a cookie clicker website and am encountering an issue with saving the user's score to localstorage when they click the "save" button. Here is what my code looks like: let score = 0; function addPoint() { score += 1; } ...

Retrieve MongoDB collection using Node.js

I am completely new to the express/mongo stack, and I have encountered an issue that I was unable to find a solution for on Stack Overflow. Here is my problem: Within my index.js file, the code looks something like this: var mongoose = require('mong ...

What is causing the recurring failure of file input changes to take effect?

Here is the code snippet I'm working with: <input type="file" #fileInput ng2FileSelect [uploader]="uploader" (onFileSelected)="onFileSelected($event)" /> Along with the handler function: public onFileSelected(e: Fi ...

What is the best way to interact with a html element using the onclick event in Selenium?

Struggling to find the xpath for a link on a webpage. The HTML code for the link is as follows: <td style="width: 50%; text-align: right; vertical-align: middle"> <img id="ctl00_ContentPlaceHolder1_RadGrid1_ctl00_ctl02_ctl00_AddRecord1" ...

Nested elements not transitioning using CSS

Why is my transition not being applied to the submenu <a> tag? The color change is working fine, but the transition does not occur on hover. When I use the same rules on another element with a different main class, it works perfectly. It seems like t ...

Tips for sending variable from JavaScript to PHP Page through XMLHTTP

Make sure to review the description before flagging it as a duplicate. In my understanding, the method of transmitting data from JavaScript to PHP is through Ajax Call. This is the situation I am facing: With PHP, I bring forth an HTML page that cont ...

increasing the size of the input thumb compared to the others

Hello fellow React developers, I'm currently learning by coding and I have a question about a slider. I'm trying to make the thumb of the slider bigger, but when I increase its size it doesn't fully display within the input area (as you can ...

Is there a variance in outcomes between constructing a pattern with a string and constructing a pattern with a regular expression "literal" in JavaScript?

Are there any distinctions between using RegExp literals versus strings? http://jsfiddle.net/yMMrk/ String.prototype.lastIndexOf = function(pattern) { pattern = pattern + "(?![\s\S]*" + pattern + ")"; var match = this.match(pattern); ...

Utilizing an npm Package in Laravel - Dealing with ReferenceError

I'm having trouble with the installation and usage of a JS package through npm. The package can be found at . First, I executed the npm command: npm install --save zenorocha/clipboardjs Next, I added the following line to my app.js file: require(& ...

Is it possible to incorporate cmd.exe into an HTML file?

Currently in the process of creating an online batch tutorial and aiming to integrate the Windows command prompt as a dynamic element on my webpage. Initially attempted using the html5 embed tag, however encountered an error stating unsupported plugin. S ...

What could be causing me difficulty in integrating NProgress into my Next.js application?

Despite following all the necessary steps for implementing the nprogress package, I am facing an issue where the loading bar shows up when routes are changed but nprogress fails to function properly. I have attempted alternative ways such as linking the st ...

Failed to parse module: Encountered an unexpected token while using babel-loader with BABEL_ENV set to development

I recently encountered an issue with my React project using webpack when I added the @superset-ui/embedded-sdk library via yarn. While the production build runs smoothly, the development build fails to compile. Since I am not very familiar with webpack, it ...

A guide on updating data with Vue-SweetAlert2 by passing user input

I'm currently working on creating a "create" button that will prompt a popup with a text input field for the user, similar to the image below. https://i.sstatic.net/eP7ki.png The expected behavior is that when the OK button is clicked, the name ente ...

Implementing theme in Monaco editor without initializing an instance

I recently developed a web application incorporating Monaco Editor. To enhance user experience, I also integrated Monaco for syntax highlighting in static code blocks. Following guidance from this source, I successfully implemented syntax highlighting wit ...

What is the best method to fetch a specific post from supabase for showcasing in a dynamic Route with Nextjs14?

I'm currently working on a Next.js application where I am fetching posts from a Supabase database. Everything works fine when retrieving all posts, but when trying to retrieve a single post dynamically using the ID, the screen displays null. Here&apos ...

Animating scrollTop using JQuery

There are two buttons with hidden parts related to each. When a button is clicked, its associated part will be displayed and the document will scroll to the top. Issue: The displayed part does not move to the top as intended. Your assistance in resolving ...

What does it mean when an "Unidentified column in the field list" error occurs?

I've encountered a problem with my HTML+PHP code. Whenever I try to input data into this table within my database, an error message pops up saying "Unknown column 'phone_outlet' in 'field list'"... What should I do? :( I can't ...

Can HTML Elements be used within a Contenteditable section?

I am facing an issue with a contenteditable tag on my website. Users are unable to type code into the tag and have it display as an actual element instead of just text. Is there a way for users to input full, working HTML elements in a contenteditable box ...