Sleek CSS 'shrink' animation on dimensions, spacing, and outlines

I am currently working on developing a transition that can effectively 'collapse' a div along with its floating elements by adjusting the width, padding, and border properties.

After conducting some research, I have compiled my findings in the following link: http://jsfiddle.net/p38b6ndb/1

Here is a sample of how it looks:

HTML:

<div>
    <div class="section">
        <div class="sub-section">label</div>
        <div class="sub-section">field</div>
    </div>
    <div class="section collapsible">
        <div class="sub-section">label</div>
        <div class="sub-section">field</div>
    </div>
    <div class="section">
        <div class="sub-section">label</div>
        <div class="sub-section">field</div>
    </div>    
</div>

CSS:

.section {
    float: left;
    width: 25%;
}

.section.collapsible {
    opacity: 1;
    transition: opacity linear 1s, width linear 1s;
}

.section.collapsible.collapsed {
    width: 0;
    opacity: 0;
}

.sub-section {
    width: 50%;
    float: left;
    height: 42px;
}

The goal: The intention is for the second section to collapse while the third section moves to the left next to the first section.

The issue: The problem arises when there is padding and/or border on the collapsing div. As the width decreases during the collapse, the div wraps around causing an increase in height, subsequently pushing down other elements below it.

  • I attempted using box-sizing: border-box, but it did not provide the desired effect.
  • Transitioning the padding and border-width properties alongside the width did not prevent wrapping either. Adjusting durations also did not yield the expected outcome.
  • Ignoring padding and border changes as they become opaque and simply adding a margin-left did not address the height increase issue.
  • Although fixing the height is an option, ideally, the height should be flexible since each section may contain varying content sizes.

Can someone provide guidance on implementing this collapse transition for divs containing padded/bordered elements?

If possible, demonstrating a solution by updating the provided fiddle would be greatly appreciated.

Answer №1

For a smoother transition, make sure to apply 'padding: 0' to every .collapsed > sub-section-border-box element while also setting up a proper transition effect.

Check out the demo here

.section.collapsible .sub-section-border-box {    
    padding: 10px;
    border-width: 2px;
    transition: all linear 1s;
}

.section.collapsible.collapsed .sub-section-border-box {    
    padding: 0;
    border-width: 0;
}

Answer №2

After experimenting with various options, I have discovered a clever solution to achieve the desired effect. By transitioning padding and border-width to 0 only for the left and right sides, not the top and bottom, the collapse appears to be solely on the width. This method ensures that the divs do not change in height at all.

.section.collapsible {
    opacity: 1;
    transition: opacity linear 1s, width linear 1s;
}

.section.collapsible.collapsed {
    width: 0;
    opacity: 0;
}

.section.collapsible .sub-section {    
    padding: 10px;
    border-width: 2px;
    transition: padding linear 1s, border-width linear 1s;
}

.section.collapsible.collapsed .sub-section {    
    padding: 10px 0;
    border-width: 2px 0;
}

To see this solution in action, check out the demo here.

Additionally, utilizing box-sizing: border-box still remains possible with this approach.

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

Placing a table within a div causes the div to expand in width beyond 100%

A situation has arisen where a table with a large number of columns (30 columns) is being affected by the white-space:nowrap style set on each th tag, causing the parent div to stretch unnaturally. How can this issue be resolved and allow for a horizonta ...

Ways to efficiently locate a CSS class amidst numerous stylesheets

Recently, I was assigned a theme for a website that included multiple stylesheets. As I sift through the index.html file, I notice various classes, but struggle to pinpoint which stylesheet they belong to. Instead of manually checking each stylesheet, I ...

obtaining the values from a dynamically generated group of checkboxes

My dynamic checkboxes are categories that can be selected individually. <input type="checkbox" name="SubCats" class="subcat-checkbox" value="18001700">first</input> <input type="checkbox" name="SubCats" class="subcat-checkbox" value="180018 ...

Is it possible to utilize the computed value from one query in a subsequent query?

I am performing a calculation for one parameter (X) in the following manner: <?php $link=mysql_connect("localhost","root",""); mysql_select_db("hand"); $result = mysql_query("select * from sessions",$link); $num_calls = mysql_num_rows($result); echo ...

Largest possible <main> container with footer positioned at the bottom

I have organized my webpage into three sections: the <header>, the <main>, and the <footer>. The <header> is fixed at the top with a height of 109px, including a 6px border, which results in the <main> having a margin of 109p ...

CSS trick for masking line borders with parent corner radius

I'm currently utilizing next js for my web application, and I have encountered a specific requirement that needs addressing. You can find the visual representation of this requirement in the following image: https://i.sstatic.net/zGVrl.png The progr ...

Having trouble incorporating Bootstrap with JS files in my Maven project

While attempting to download and transfer the files of the newest Bootstrap version to my Maven project, I encountered an error in all JavaScript files. It seems to be due to syntax errors, even though I have not made any alterations to the code in these ...

What are some effective ways of using the parent, before, and children selectors?

<table> <tr><td><input type="text" value="123"></td><td><input class="here" type="text"></td></tr> <tr><td><input type="text" value="333"></td><td><input class=" ...

Setting the minimum and maximum width of a div using Bootstrap, not based on the number of columns

I want to adjust the number of columns based on the width of the screen rather than choosing a specific value like 3, 4, 5, or 6. ...

Counting Clicks with the Button Counter

I'm having an issue with my jQuery code that is supposed to count button clicks - it stops after just one click. I need help fixing this problem. $(function() { $('.btn').click(function() { $(this).val(this.textContent + 1); }); } ...

Ways to create a group label to modify various textboxes when a click event occurs

Is it possible to change multiple textboxes with corresponding labels after a click event? Issue: The current output only displays the value of the last textbox. $(function () { $('a.edit').on('click', function (e) { e.pre ...

Animating jQuery Accordion in Horizontal Direction Extending to the Far Right

After implementing a horizontal accordion in jQuery based on the tutorial found at the following link: A minor issue arose during animation where a slight space was added on the far right side, causing the tabs to shift slightly. This problem is particula ...

How can I position the nav-item to the right without it becoming collapsed?

Hello! I am currently using Bootstrap 4 and attempting to create a navbar menu. Below is the code I have written: <nav class="navbar navbar-expand-lg navbar-light bg-light"> <button class="navbar-toggler" type="button" data-toggle="collapse" da ...

How to merge text (string) with date in HTML using JavaScript

I am attempting to blend a day with the phrase "this is the day" in HTML. I gave it a shot, but the concatenation didn't work as expected. ...

The CSS design morphs as I adjust the browser size. Despite my limited coding experience of just a few weeks, I've encountered a challenging obstacle

Hi there, I've been searching on W3 and various other sources but haven't found a solution to my problem. I have managed to create the layout I want, however, it starts falling apart when I resize the browser window. I'm relatively new to co ...

What is the best way to extract table data with Selenium in Python?

Is there a way to simplify the HTML table source below for easier reading with selenium in python? <div class="general_table"> <div class="general_s"> <div class="general_text1">Name</div> <div class="gener ...

Remove the presence of a black square when selecting elements using CSS

Update: After some investigation, I discovered that the mysterious black square is actually a part of the scroll bar. By adding the following code snippet: select::-webkit-scrollbar { display: none; } The square no longer appears. Initially, my se ...

Exploring the width of Bootstrap 5 Tab Pane content

My issue lies with the content width of my tab panels - specifically, it doesn't work well with columns. I am unable to divide a tab-pane as col-md-7 and col-md-5 (refer to the screenshot below). I am unsure of how to resolve this issue. <div clas ...

The expression '<xsl:value-of select="document(content)//title"/>' results in a null node response

Can someone assist me in retrieving the title of a basic HTML document in order to create a sitemap? I have been receiving an empty value every time I try. Upon inspecting the issue, it appears that the document(content) is returning document nodes. It s ...

sending an HTML request to a server from various devices

Hello everyone, I am new to website development and this is my first post here. I am reaching out because I have been having trouble finding information on a specific topic... My inquiry is about browsing the same website from two different devices, such ...