Height Animation and Divs Placement

My approach is as follows:

Upon clicking an image (refer to the screenshot provided), I intend for text related to that specific image to be displayed beneath the row of images (circles). To achieve this, I experimented with using transitions. Initially, I created a parent div (colored green) with its position set to relative, and a child div (colored red) with its position set to absolute. I then animated the top property of the child div, resulting in the desired effect.

.parent { position: relative; }
.child { position: absolute; top: -100px; transition: top 1s linear; }
.child:target { top: 0; }

However, I encountered an issue when attempting to have additional divs below the sliding text content (the 'test' text in the demonstration) push down as the text expanded. This limitation arises from the fact that when a child div's position is set to absolute, the height of the parent div (green box) does not automatically adjust accordingly.

Therefore, my question stands: Is there a method to resolve this challenge?

Thank you for sharing your insights.

PS: As shown in the screenshot, the green box's height exceeds that of the red box due to another invisible red div positioned below the visible one, but obscured by having opacity set to 0.

Answer №1

There are various methods to achieve this.

One approach I used involves CSS3 with height and absolute positioning. By utilizing height without any positional issues, the layout can flow smoothly. However, a drawback of using heights is the necessity of providing a fixed height for CSS transitions to function properly. To circumvent this limitation, an additional wrapper around the div can be employed to expand and utilize its height for the expanding div as shown below. Disregard the row and columns classes.

HTML

<div class="small-12 columns  level-1">
    <div class="row category-name ">
        <div class="small-12 columns">
            <span class="">Click to expand or collapse</span>
        </div>
    </div>

    <div class="level-2 level-2-container" style="height: 60px;">
        <div class="grow-wrapper small-12 columns">
            Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
        </div>
    </div>
</div>

CSS

.level-2-container {
    -moz-transition: height 0.5s;
    -ms-transition: height 0.5s;
    -o-transition: height 0.5s;
    -webkit-transition: height 0.5s;
    transition: height 0.5s;
    overflow: hidden;
    /* outline: 1px solid red; */
    padding: 0;
    overflow:hidden;
}

JavaScript

$(document).on('click', '.level-1', function(event) {
    var $level1 = $(event.currentTarget);
    var $level2 = $level1.find('.level-2-container');
    var $growWrapper = $level1.find('.grow-wrapper');
    var heightToSet = $growWrapper.height();

    growDiv = $level2[0];
    if (growDiv.clientHeight) {
        $level2.height(growDiv.clientHeight);
        $level2.height(0);
    } else {
        growDiv.style.height = heightToSet + "px";
    }

    event.stopPropagation();
});

JS-Fiddle http://jsfiddle.net/hps8p/

Answer №2

After realizing the complexity of the original query, it became clear that a mouseover was necessary to trigger text display beneath an image. Each image corresponds to a specific block of text (indicated by red boxes). The goal is to have all text boxes hidden by default and only appear when the user hovers over the associated image, with a smooth animation effect. Additionally, the surrounding divs should adjust accordingly.

Following Vishwanath's insightful advice, it was determined that the text blocks needed to be nested within another container for better organization. A JavaScript function calculates the height of the text block connected to the hovered image (identified by its id), adjusting the parent div box size accordingly through CSS3 transitions. Upon mouseout, the parent div resets to zero height, moving the text out of sight.

This is the modified solution:

.bio_container
{
    display: inline-block;
    position: relative;
    border: 2px solid green;
    width: 100%;
    transition: height 1s ease-in-out;
    overflow: hidden;
}

.bio
{
    position: absolute;
    top: -200px;
    transition: top 1s ease-in-out, opacity 0.5s ease-in-out;
    border: 1px none green;
    z-index: 0;
    opacity: 0;
    border: 1px solid red;
}

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

Enhancing the Bootstrap carousel slider with offset background content container

Apologies if the title seems a bit unclear. I want to clarify that I am working with the latest version of pure Bootstrap (https://getbootstrap.com/) and nothing more. My goal is to replicate the layout shown in the following image for a client: https:// ...

Exploring how to retrieve time using PHP and JavaScript

When displaying a date in php or javascript, what factors influence the calculation? Is it dependent on the user's computer time settings, or is it sourced externally? If the user has control over it, how can I ensure my code accurately retrieves the ...

Displaying an HTML button above JavaScript code

Hello, I am currently working on a project that involves displaying the Earth and I am in need of assistance with positioning some buttons on the screen. Currently, I am facing an issue where the buttons appear either above or below the JavaScript code. I ...

Iterating through elements within a Div will retrieve the initial element exclusively

Is there a way to loop through all elements within the MainDiv Div in order to retrieve their values? Currently, I am only able to retrieve the value of the first element. <div id="MainDiv"> <input type="text" id="MyText"value="Text1" /> ...

Web fonts are functioning properly only on Chrome and Safari for Macintosh operating systems

A web designer provided me with some fonts to use on a website. Below is the content of my fonts.css file: /* Font Awesome */ /* Font Awesome Bold */ @font-face{ font-family: 'font-awesome'; src: url('fonts/font-awesome-bold-webf ...

"Form submission fails to populate _POST when file and text fields are used simultaneously

Looking for a solution... <form action="upload.php" method="get" enctype="multipart/form-data"> Enter a 10 digit mobile number: &nbsp;<input id="imgphno" type="text" maxlength="10" /> <br/><br/> <input id="file" type="file" ...

Use JavaScript to create a new window and load the HTML content from an external URL

Just starting out with HTML and Javascript I'm trying to use JavaScript to open a window and load content from an external source. I attempted using document.write(), but it only works when I hardcode the HTML as input. Any suggestions on how to get ...

Which is better: Utilizing Ajax page echo or background Ajax/direct HTML manipulation?

I am facing a dilemma and I could really use some guidance. Currently, I am in the process of developing an ordering system using PHP/Smarty/HTML/jQuery. The main functionality revolves around allowing sellers to confirm orders on the site. My goal is to ...

I am attempting to add user input to the parent container when the button is clicked, but for some reason it is

Recently delving into jquery, I attempted to dynamically add an input field to a parent div on button click, but encountered some difficulties. Specifically, my goal was to insert a div with an input element into the parent div identified by the id "join ...

The Push Over Menu is malfunctioning and not functioning properly

I am facing an issue with pushing my menu along with the content to the right. The JS code I have is not working as expected. When I click on <div class="menu-btn toggle"></div>, the menu does not trigger. Can someone help me understand why thi ...

Content in Bootstrap not filling entire mobile screen

The content in this form does not stretch to full screen on mobile phones, but it appears properly on computers and tablets. Please advise on how to solve this issue. https://i.stack.imgur.com/S2XkS.jpg <link rel="stylesheet" href="https://maxcdn.bo ...

How can I display different divs based on a selected option?

I'm encountering difficulties while attempting to develop the code for this specific task. My goal is to display or hide divs based on the selection made in a select option, with a total of 4 options available. There are 2 select elements <select ...

The labels in production are getting cut off when creating a view in ASP .NET MVC

Upon viewing a Create View on my PC, I noticed that the main form and labels are adequately spaced: https://i.sstatic.net/1Q2bC.png However, once the website is deployed to IIS and accessed on the same PC with the same browser, everything appears cramped ...

CSS: Card elevates when keyboard is activated on a mobile device

[view image 1[view image 2] Image 1: Normal View, Image 2: When the keyboard is enabled, the card jumps up and when I close it's normal. Is this behavior normal? Or is there a fault in my CSS? I utilized styled-components for writing the CSS. CSS ...

Loading JS and CSS files in relation to the website root directory

When it comes to including CSS/JS files, what are the best practices for defining the file URI? Some people prefer to include files relative to the website root by specifying the full URI: <link rel="stylesheet" href="/my/path/to/css/main.css"> Thi ...

Changes made to the CSS are not being reflected on the live production server hosted on our Bitbucket repository

My WordPress website is hosted on a Linux server and connected to a Bitbucket repository, but I am experiencing an issue where CSS changes are not reflecting on the live site. I have made changes to the HTML code and style sheet, and while the HTML code u ...

Transferring a unique Python object back and forth between Jinja2 and Flask

As a newcomer to Flask, Jinja2, and HTML templates, I am working on understanding how to transfer data between the controller and views. Currently, I have a calendar of events where each event is a hyperlink that uses url_for to navigate to a view with add ...

Is it possible to customize the appearance of a toast notification using Toastr beyond the default settings? If so, what options are available for creating

Working with Angular 9 and ngx-toastr has presented me with an interesting challenge. I am tasked with creating custom toast styles that differ significantly from the default ones provided by toastr. Each toast in the set will have unique border colors, f ...

What is the best way to trigger a modal on Bootstrap using a JavaScript/jQuery function?

I encountered an issue while attempting to call a bootstrap modal using a simple button or link, which may be due to a conflict with my select2 plugin (although I am uncertain). I tried appending the button to the select2 dropdown but it doesn't seem ...

Adding a background image in javascript using data from a MySQL database

My current tech stack includes CodeIgniter, vanilla JavaScript, AJAX, CSS, and MySQL. I am trying to figure out how to set the background of an image that is stored in a MySQL database. While the following code is error-free and working perfectly, my cha ...