Tips for customizing the toggle animation styling

I'm new to HTML and CSS, currently working on designing a website for our conference. I've implemented a toggle effect for displaying the information about invited speakers along with their research interests using the following code:

<!DOCTYPE html>
<html>
<head>
<style>
.maindrop{
  width:50%;
}
.bar{
  padding: 20px;
color: white;
background: #1FB5AC;
display: block;
font-family: Times;
text-decoration: none;
font-size: 16px;
}

.bar:hover{
background: gray;
}

.dropbox{
  height: 0;
  transition: 1s;
  overflow: hidden;
  width : 100%;
   transition-property: background;
  transition-timing-function: linear;   
}

.dropbox:target{
  height:auto;
}
</style>
</head>
<body>

<div class="maindrop">
  <div class="fold default">
    <a class="bar" href="#tab1">Speaker 1</a>
    <div class="dropbox" id='tab1'>
      // Speaker 1 Details
    </div>
  </div>
  ...
  <div class="fold">
    <a class="bar" href="#tab4">Speaker 4</a>
    <div class="dropbox" id='tab4'>
      // Speaker 4 Details
    </div>
  </div>
</div>

</body>
</html>

Issues Faced:

The animation isn't functioning as expected. The drop-down box should appear smoothly and gradually. It seems like there might be an issue with the animation implementation.

Any pointers on correcting this?

Additionally, after clicking once to expand the boxes, is there a way to reset them upon a second click?

PS: Suggestions for enhancing the styling are welcome! A polished and sophisticated display of information regarding conference speakers is what I aim for.

Answer №1

.maindrop {
    width: 50%;
}
.bar {
    padding: 20px;
    color: white;
    background: #1FB5AC;
    display: block;
    font-family: Times;
    text-decoration: none;
    font-size: 16px;
    transition: .2s ease-out;
}
.bar:hover {
    background: gray;
}
.dropbox {
    max-height: 0;
    transition: .2s ease-out;
    overflow: hidden;
    width: 100%;
}
.dropbox:target {
    max-height: 300px;
}
<!DOCTYPE html>
<html>

<head>
</head>

<body>
    <div class="maindrop">
        <div class="fold default">
            <a class="bar" href="#tab1">Speaker 1</a>
            <div class="dropbox" id='tab1'>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandit, ut viverra diam ...
            </div>
        </div>
        <div class="fold">
            <a class="bar" href="#tab2"> Speaker 2</a>
            <div class="dropbox" id='tab2'>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere, cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed nulla blandi...
            </div>
        </div>
        <div class="fold">
            <a class="bar" href="#tab3"> Speaker 3</a>
            <div class="dropbox" id='tab3'>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed null...
            </div>
        </div>
        <div class="fold">
            <a class="bar" href="#tab4"> Speaker 4</a>
            <div class="dropbox" id='tab4'>
                Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam diam enim, interdum eget iaculis in, pretium non libero. Pellentesque id interdum risus, vitae iaculis ipsum. Duis mi tortor, sodales nec nisl nec, venenatis sollicitudin turpis. Pellentesque hendrerit nec massa sit amet scelerisque. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque a mi sit amet metus hendrerit placerat. Duis ullamcorper lectus vel erat luctus egestas. Maecenas pharetra justo sed null...
            </div>
        </div>
    </div>
</body>

</html>

Answer №2

The animation seems to be experiencing some issues.

One major problem is that the browser struggles to animate smoothly between height:0; and height:auto; because it requires specific start and end points for the animation.

An alternative approach would be to animate between height:0; and height:200px;, but this poses a challenge when dealing with Speaker Profiles of varying lengths. Manual adjustment of each profile's height isn't practical, especially during edits.

To overcome this hurdle, consider setting the height value through JavaScript instead of CSS. This way, you can smoothly transition between:

dropbox.style.height = '0';

and

dropbox.style.height = dropbox.scrollHeight + 'px';

Another issue arises when an open dropbox does not close upon a second click. This occurs because the :target pseudo-class identifies the initial click, making no change if clicked again.

For a solution, add or remove the class .open dynamically using JavaScript. The script will determine whether to open or close the fold based on its current state.

Here's a functional example (featuring Speaker Profiles of different lengths):

- Snippet scripts and styles go here -

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

Troubleshooting issues with CSS absolute positioning

I'm having trouble getting my icons to align in the center of the screen and I can't figure out why it's not working CSS: #Main{ position: absolute; top: 50%; left: 50%; } HTML: <!DOCTYPE html> <html> <head& ...

scrolling through a list using .slice choosing an excessive amount of items

Is it possible to create a dynamic pager that can be customized using parameters from the URL? I have noticed that when I hardcode the perTime variable, everything works fine. However, as soon as I try to use a parameter from the URL, the page starts behav ...

Looking for assistance with deleting a child element in an XML file using PHP

I need help figuring out how to delete a child from my jobs.xml file with a PHP script. My jobs.xml file looks like this: <jobs> <event jobid="1"> <title>jobtitle</title> <desc>description</desc> &l ...

Avoid the div from covering the scrollbar within its containing parent div

Background: Trying to resolve an issue mentioned here: https://github.com/likeastore/ngDialog/issues/94 Challenge: If you open this plnkr link: http://plnkr.co/edit/qKJiNwyivqJVCAtyhwYR?p=preview and attempt to hold and drag the scrollbar with the mouse, ...

Issue with Jquery autocomplete not populating data in additional fields

Issue: I have implemented a dropdown list for selecting suppliers, and based on the selected supplier, users can search for items using JQuery Autocomplete. However, when an item is selected from the search results, the text boxes for 'description&apo ...

How to perfectly position various height <a> elements in a WordPress mega menu

I'm currently working on a WordPress mega menu with four columns, each with a heading or top menu item. The headings should have a gradient border at the bottom and differ in length. However, I'm facing an issue where some headings span two lines ...

What is the best way to display multiple divs in a single location?

I am facing an issue with displaying different text on mouseover for three buttons. I successfully implemented it for one button, but when I added the other two, it stopped working. Here is the code I used for the first button: Using jQuery- document.get ...

Chakra UI Not Displaying Proper Responsiveness on the User Interface

I recently integrated Chakra UI for styling a project on Next.js, and while most components are functioning properly, I am facing challenges with implementing responsive layouts. The styles do not seem to adapt when the screen size is reduced. Here's ...

How come the mongoose ref feature is not functioning properly for me when I attempt to reference objects from a different schema?

I'm having trouble accessing the attributes of a store's address, as I keep getting 'undefined'. It seems that the address is only an id, even though I set up a 'ref' in the address schema. What could be causing this issue? H ...

Retrieving current element in AngularJS using jQuery

I have 4 templates, each with mouse actions that trigger functions: ng-mouseover="enableDragging()" ng-mouseleave="disableDragging()" Within these functions, I update scope variables and would like to add a class using jQuery without passing any paramete ...

Utilizing the CSS :not() selector within a nested table

I am having an issue with my AjaxControlToolkit where the main-page CSS is affecting the inner "calendar" popup. To illustrate what I need, what I've tried, and the temporary workaround, I have simplified everything into a one-page example. I am in s ...

Express.io is encountering an issue where it is unable to read the property 'expires' of an undefined element

I am new to working with node.js and I am attempting to utilize express.io for saving cookies. Following a tutorial at https://github.com/techpines/express.io/tree/master/examples#sessions, I have encountered the following error: root@server1 [/nodeexamp ...

Why is the console log not working on a library that has been imported into a different React component?

Within my 'some-library' project, I added a console.log("message from some library") statement in the 'some-component.js' file. However, when I import 'some-component' from 'some-library' after running uglifyjs with ...

What could be causing ngInfiniteScroll to not work properly with tables?

I've been attempting to incorporate ngInfiniteScroll into my table using ng-repeat on <tbody> however, it isn't triggering when I reach the end of the page. <div infinite-scroll="list.getMoreItems()"> <table md-table md-row-se ...

Using AngularJS controllers: How can one trigger a controller from within a different controller?

Within my script, I have a list controller defined as follows: define([ 'jquery', 'app' ], function ($,app) { app.controller("ListContacts", function($scope,$route,$http){ $http({ method: 'GET&apo ...

Scraping specific section of text from a webpage's source code with BeautifulSoup library in Python

As a novice in python, I have little to no knowledge of HTML. After stumbling upon a captivating youtube video (https://www.youtube.com/watch?v=kEItYHtqQUg&ab_channel=edureka%21) on web scraping, I became intrigued by the idea of extracting text from U ...

Spacing between a pair of jQuery datepicker panels

https://i.sstatic.net/Xnbh1.png Is there any way to add spacing between two tables inside the jquery date picker with a common header? I've tried various solutions like adding padding to the parent div, but haven't been able to fix it. Can anyon ...

What is the method for linking a function to a div element without using an event handler that is loaded dynamically?

I have created a square element which adjusts its size based on the width using jquery. It works perfectly fine on statically loaded divs, but how can I make it work on dynamically added elements? HTML: <button class="click">press to add a div</ ...

What is the best method to incorporate a JavaScript object key's value into CSS styling?

I am currently working on a project in React where I'm iterating over an array of objects and displaying each object in its own card on the screen. Each object in the array has a unique hex color property, and my goal is to dynamically set the font co ...

The footer refuses to stay positioned at the bottom of the page

Can you help me locate the CSS style for the footer? #footer { background-color: #000; bottom: 0px; color: #fff; clear: both; text-align: center; padding: 5px; } What is the best way to ensure the footer is positioned at the bottom of ...