Expanding a flexbox child using transform creates gaps between elements

I am working with a flexbox containing 5 direct children. My goal is to scale the first and last column (.colonnade__column) to zero instead of using display: none. When I achieved this, the content scales as expected but still keeps the space reserved, causing the next/previous element not to move.

I experimented with sizing the child elements of the columns and making the columns themselves more flexible, but unfortunately, I hit dead ends...

HTML

<div class="colonnade">
    <aside class="sidebar colonnade__column alpha">
        <div class="">
            NAVI
        </div>
    </aside>
    <label class="colonnade__trigger alpha" for="colonnade-alpha">
        <span class="label">
            Show Navigation
        </span>
    </label>
    <main class="main" id="main">
    </main>
    <aside class="sidebar colonnade__column omega">
        <div class="">
            CART
        </div>
    </aside>
    <label class="colonnade__trigger omega" for="colonnade-omega">
        <span class="label">
            Show Cart
        </span>
    </label>
</div>

CSS

.colonnade {
    display: flex;
}

.colonnade__column {
    padding: 1em;
    width: 20%;
    min-width: 16em;
    max-width: 20em;
    /*display: none;*/
    transform: scaleX(0);
    transition: transform .3s;
    background: #ccc;
}

.colonnade__column.alpha {
    transform-origin: 0 0;
}
.colonnade__column.omega {
    transform-origin: 100% 0;
}

.colonnade__state.alpha:checked ~ .colonnade__column.alpha,
.colonnade__state.omega:checked ~ .colonnade__column.omega {
    /*display: block;*/
    transform: scaleX(1);
}

You can see the behavior in action on this CodePen link. It is triggered by the light grey areas with vertical text.

https://codepen.io/bitstarr/pen/WKbMjL

Answer №1

Temani's analysis is spot on: the transform property only pertains to visual effects and does not impact the layout of elements. Layout calculations are performed prior to the application of transforms.

To achieve a similar effect, consider utilizing flexbox properties:

.colonnade {
    display: flex;
}

.colonnade__column {
    flex: 0 1 0; /* Represents: flex-grow, flex-shrink, flex-basis */
    padding: 1em 0; /* Eliminate horizontal padding when columns are hidden */
    transition: all .3s;
    background: #ccc;
}

.colonnade__state.alpha:checked ~ .colonnade__column.alpha,
.colonnade__state.omega:checked ~ .colonnade__column.omega {
    flex: 1 0 20%;
    padding-left: 1em;
    padding-right: 1em;
}

Check out the updated demonstration here: https://codepen.io/wiiiiilllllll/pen/pZvLzj.

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

What to do when Google Font fails to load and how to define an alternative font styling

Can anyone help me out with a solution? I am facing an issue with google fonts not working in Safari on my website. Is there a way to instruct the browser to switch to a different style when the font is not supported? Below is the CSS code I am currently u ...

Utilize jQuery to create a dynamic image swapping and div showing/hiding feature

I'm having trouble implementing a toggle functionality for an image and another div simultaneously. Currently, when the image is clicked, it switches to display the other div, but clicking again does not switch it back. Can someone please advise on wh ...

Adding a close button inside a circular background using HTML/CSS

I'm struggling to align the X inside the circle perfectly; they just don't seem to match up no matter what I try. Here's a glimpse: https://i.sstatic.net/OIZik.png html: <div class="messages"> <span class="c ...

Struggling with modifying css attribute in a JQuery Chrome Extension

I have a basic chrome extension that I utilize on a specific URL to make slight adjustments to the page layout (such as color and position). Now, I am attempting to apply the same modifications to a new URL. The code I am using for both URLs is identical, ...

Increasing the size of a container without displacing the elements beneath it

There is an element on my webpage that, when hovered over, expands to reveal hidden text. The issue I'm facing is that this expansion causes the content below it to shift position. I attempted to use position:absolute, but it did not have the desired ...

jQuery Mobile: issue with positioning an external panel on the right side

I am looking to incorporate an external header and two external panels, one on the left and one on the right. The challenge is to ensure that these panels remain visible at all times on larger screens. I encountered some difficulties positioning the panel ...

Stopping horizontal scrolling in Material-UI Autocomplete/Popper: A troubleshooting guide

I am currently working on incorporating the Material-UI Autocomplete component, and my goal is to have each option label show an icon along with some text. The challenge I'm facing is that I only want the popper element to be the width of the text inp ...

Show an Unordered List in a Horizontal Orientation

I'm having trouble getting my Subnav list to display horizontally. Can anyone offer a solution to this issue? http://jsfiddle.net/nategines/9bued/10/ ...

Struggling to combine select dropdown choices in one calculation

​​I'm currently working on a project where I need to create a website that multiplies three numbers selected from dropdown menus together. However, when I attempt to perform the calculation, I only get the result "1" displayed. I've spent sev ...

Creating a customized design for a q-popup-edit

As I navigate through using Quasar in Vue, I stumbled upon a q-popup-edit that allows me to gather input from the user. Despite my attempts at researching via Google and reading documentation, I have hit a roadblock when it comes to customizing the style o ...

werkzeug.exceptions.BadRequestKeyError: 400 Bad Request: The server is unable to process the request sent by the browser or proxy. This error occurred in a Flask web application

Can someone guide me on troubleshooting this issue in Flask? I am still learning. Server running at (Press CTRL+C to exit) 127.0.0.1 - - [26/Jul/2020 11:19:45] "GET /predict HTTP/1.1" 500 - Traceback (most recent call last): raise exceptions. ...

Creating a navigation bar that stays fixed at the top of

Hey there, currently I am utilizing a combination of jquery and css to affix my navigation menu at the top when scrolled. However, the problem is that my navigation menu is positioned beneath a div with a height set in viewport units (vh). The jquery scr ...

What is the process for updating the CSS style of every other piece of data (such as an image) retrieved

Is it possible to utilize PHP code when retrieving data from a MySQL database in order to have every other record display with unique CSS formatting? For instance, I am incorporating images similar to this in my comment system: http://prntscr.com/3hpiep ...

dynamically assigning a style attribute based on the dimensions of an image retrieved from a URL

My aim is to determine whether or not I should use an image based on its dimensions. To achieve this, I came across a function on stack overflow that can retrieve the dimensions of an image just by using its URL. Here is the code snippet they provided: f ...

The HTML dropdown menu becomes crowded with numerous <li menu items, leading to it wrapping

One issue arises when the number of menu items exceeds the capacity to fit on a single line, causing them not to wrap from left to right onto the second line. The desired outcome is for the second line of the menu to start on the left just like the first l ...

Enable vertical overflow to adjust automatically without needing to set a specific height

I have a unique table that consists of 3 main parts: the header the content the footer Using flexbox, I managed to make the "content" section occupy all available view height. However, this was not explicitly done and now when there are too many items in ...

Is it possible to display the desktop version of a website on a mobile device

I am trying to display the desktop version on mobile without any alterations. I have included the code below: <meta name="viewport" content="width=device-width, initial-scale=0" /> or <meta name="viewport" content="width=device-width, initial-sca ...

What could be causing my JavaScript code to malfunction, even though it appears to be coded correctly?

// JavaScript Document "use strict"; $(window).scroll(function(){ if($(window).scroll() > 100){ $("#scrollTop").fadeIn(); } }); $(window).scroll(function(){ if($(window).scroll() < 100){ $("#scrollTop").fadeOut(); } }); $(document).ready(function() ...

Configuration for secondary dependencies in Tailwind

As per the guidelines outlined in the official documentation, it is recommended to configure Tailwind to scan reusable components for class names when using them across multiple projects: If you’ve created your own set of components styled with Tailwin ...

What method can be used to eliminate the shadow of a container when rotating a div?

I am currently working on creating a dynamic card that expands and reveals additional information upon mouse rollover. The challenge I am facing is that the shadow effect remains visible when flipping the card, which disrupts the overall desired effect. H ...