Expanding and collapsing text display using solely CSS

Here's a sneak peek of my current progress:

#expand {
  height: 0px;
  width: 0px;
  overflow: hidden;
  transition: height 0.5s, width 0.5s ease-in-out;
  background: #000000;
  color: #FFF;
}

input {
  display: none;
  visibility: hidden;
}

input:checked+label+#expand {
  height: 150px;
  width: 600px;
}

#toggle:checked~label::before {
  content: "-";
}


/* unnecessary styling */

main {
  background: #EEE;
  width: 100px;
  margin: 20px auto;
  padding: 10px 0;
  box-shadow: 0 3px 5px rgba(0, 0, 0, 0.3);
}

section {
  padding: 0 20px;
}

label {
  display: block;
  padding: 0.5em;
  text-align: center;
  border-bottom: 1px solid #CCC;
  color: #666;
}

label:hover {
  color: #000;
}

label::before {
  font-family: Consolas, monaco, monospace;
  font-weight: bold;
  font-size: 15px;
  content: "+";
  vertical-align: text-top;
  display: inline-block;
  width: 20px;
  height: 20px;
  margin-right: 3px;
  background: radial-gradient(ellipse at center, #CCC 50%, transparent 50%);
}
<main>
  <input id="toggle" type="checkbox" />
  <label for="toggle">Hidden Kitten</label>
  <div id="expand">
    <section>
      <p>Lorem 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>
    </section>
  </div>

  <input id="toggle1" type="checkbox" />
  <label for="toggle1">Hidden Kitten</label>
  <div id="expand">
    <section>
      <p>mew</p>
    </section>
  </div>
</main>

In the input:checked + label + #expand selector, notice that the width and height are fixed.

I am able to set the width as a percentage without any issue, but attempting to set the height as a percentage disrupts the animation. How can I resolve this? Keep in mind that I do not know the text's height beforehand. My goal is to achieve this using CSS alone.

Answer №1

You may want to consider using max-height in place of height.

#expand {
  max-height: 0px;
  width: 0px;
  overflow: hidden;
  transition: max-height 0.5s, width 0.5s ease-in-out;
  background: #000000;
  color: #FFF;
}

input {
  display: none;
  visibility: hidden;
}

input:checked+label+#expand {
  max-height: 150px;
  width: 600px;
}

#toggle:checked~label::before {
  content: "-";
}


/* some additional styles */

main {
  background: #EEE;
  width: 100px;
  margin: 20px auto;
  padding: 10px 0;
  box-shadow: 0 3px 5px rgba(0, 0, 0, 0.3);
}

section {
  padding: 0 20px;
}

label {
  display: block;
  padding: 0.5em;
  text-align: center;
  border-bottom: 1px solid #CCC;
  color: #666;
}

label:hover {
  color: #000;
}

label::before {
  font-family: Consolas, monaco, monospace;
  font-weight: bold;
  font-size: 15px;
  content: "+";
  vertical-align: text-top;
  display: inline-block;
  width: 20px;
  height: 20px;
  margin-right: 3px;
  background: radial-gradient(ellipse at center, #CCC 50%, transparent 50%);
}
<main>
  <input id="toggle" type="checkbox" />
  <label for="toggle">Hidden Kitten</label>
  <div id="expand">
    <section>
      <p>Lorem 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>
    </section>
  </div>

  <input id="toggle1" type="checkbox" />
  <label for="toggle1">Hidden Kitten</label>
  <div id="expand">
    <section>
      <p>mew</p>
    </section>
  </div>
</main>

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

How to reference global Sass variables in a Sass module

My preferred method involves utilizing a global .scss file that houses all the foundational styles, such as primary color selections. Following this, I create separate modules for each React Component to maintain organization and reduce the need for import ...

Having trouble locating an element on a webpage? When inspecting the element, are you noticing that the HTML code differs from the source page?

I'm having trouble locating a specific element on a webpage. When I use the Inspect Element tool, I can see the HTML code with the element id = username, but when I check the page source, all I see is JavaScript code. Does anyone have any suggestions ...

Why is it that $_GET consistently returns null despite successfully transmitting data via AJAX?

I'm currently developing a PHP script that dynamically generates tables in MySQL based on user input for the number of rows and columns. The data is being sent to the server using AJAX, but even though the transmission is successful, the server is rec ...

Angular 7 - Customize Mat-select-value color depending on a specific condition

One issue I am facing is changing the color of a selected value in a mat select based on a condition. This change should be visually apparent to the user. Although accessing mat-select-value from the styles is possible, implementing a condition using ng-cl ...

Fade between two elements using jQuery

Can someone assist me with adding a fade effect between elements in my code? Any advice or suggestions would be greatly appreciated! $("#example p:first").css("display", "block"); jQuery.fn.timer = function() { if(!$(this).children("p:last-child").i ...

Generating custom reports based on specific data fields

My current approach involves displaying all the columns for the searched records in a div with the id of searchresults using AJAX. I then provide a print option to print those records. However, I now want to enhance this functionality by allowing users to ...

Unable to load the Universe 55 font using the @font-face rule

I recently encountered an issue with using a custom font (Universe 55 font) in html5. I downloaded the .ttf file, applied it with the @font-face rule, and tested it on various browsers including IE, Chrome, and mobile browsers. Unfortunately, the font does ...

Is there a way to automatically load data whenever a specific inner div on an HTML page is modified?

On my website, I have created a main page with three divs: header, footer, and main. The header div contains buttons that change the content of the main div when clicked. Rather than reloading the entire page, only the main div is updated with a new HTML p ...

Exporting a table filled with data from a MySQL database to an Excel spreadsheet

Querying a MySQL database to display data in a table has been successful. Currently, it is set up to show results within a specific date range. Now, the goal is to add a button to the table that allows users to export the data to an Excel file. Previously ...

Adjust the size of various elements in real-time

I am currently working on a script that adjusts the size of filtering-inputs based on the width of gridview columns. Initially, it only involved textboxes, and I managed to make it work adequately: Script Snippet: $("#<%= myGridView.ClientID %> th" ...

What could be causing my inability to accurately guess people's ages?

My latest project involves developing a game where players have to guess names from images. Each game consists of 10 rounds, followed by a bonus round where participants must wager their points on guessing the age of the person in the image within a 2-year ...

Is there a way to ensure a Bootstrap grid column takes up the entire remaining row space?

I have a dynamic set of elements that need to be inserted into a page, with some elements being labeled as "small" and others as "big". The challenge is that I do not know in advance the quantity or order of these elements. If an element is labeled "small ...

Attaching the CSS file to the Haml layout template

I'm currently working on linking a css file to a haml layout template Within the ApplicationHelper class, I have a method to properly generate html module ApplicationHelper def styletag(sheet_name) "<link rel='stylesheet' href=&a ...

Creating a stylish diagonal background with CSS has never been easier!

Can CSS be utilized to achieve the design effect depicted in the image below? https://i.sstatic.net/o1k7O.png I am seeking to generate <div> elements with a diagonal split background featuring a solid color on one side and white on the other. ...

Is there a way to extract information from a <span> element with restricted access?

I'm currently utilizing bs4 and urllib2 to extract data from a website. Check out the webpage here. The goal is to retrieve the remaining digits of the telephone number 3610...... but beforehand, it's necessary to click on a button to reveal th ...

What is the method to trigger the alt + f4 combination in AngularJS when a button is clicked?

Is there a way to manually close the current tab in AngularJS by simulating Alt+F4 key press? $scope.cancel = function (event) { var keycode = event.keycode; }; <button class="btn btn-outline-primary" ng-click="cancel($event);">OK</button&g ...

Discover new films with TheMovieDB - Explore a random movie

Currently, I am utilizing the movie database API () to search for movies based on criteria such as popularity, name, or ID. However, I am now interested in incorporating a random movie generator feature on my website despite there being no clear instructio ...

Switching the background image when hovering over a list element

Looking at the screenshot, it's clear that there is an unordered list with a background image set on the div. What I am trying to achieve is to have the background image change whenever I hover over a list item. Each list item should trigger a differe ...

Ensuring PHP Input Validation on HTML Forms for Matching Two Values

This code snippet is designed for a form where users can input numbers. Each pair of numbers entered will be compared to determine which one is higher. <input type="number" name="team1" class="inputbox" oninput="validity.valid||(value='') ...

The ASP variable fails to display its value within the tags

I have the following code snippet that is part of a larger code base. <% dim rsFav sSQL = "(SELECT shorthand, display, larry_ranking, site_url FROM larrydb_site_list lsl JOIN larrydb_review lr on lsl.sid = lr.sid WHERE display=true AND niche=' ...