Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Absolutely positioned flexbox doesn't expand to fit contents

  • This topic is empty.
Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #200141
    Shaggy
    Participant

    As you can see from this Fiddle, an absolutely positioned, columnar flexbox won’t expand to fit its children.

    In Chrome, for example, it will only be as wide as the widest child element and as tall as the shortest column.

    Can anyone suggest a solution without using any additional markup?

    #200142
    Paulie_D
    Member

    I’m curious..why would you absolutely position a flexbox item?

    #200146
    Shaggy
    Participant

    It’s a dropdown menu.

    #200151
    Paulie_D
    Member

    It’s not clear to me what you are trying to do.

    How is this supposed to look?

    #200154
    Shaggy
    Participant

    As you can see in this Fiddle, the ul is nested in an li and positioned off-screen with a negative left value. The :hover state of the parent li sets the left value of the flexed ul to 0. Pretty standard stuff.

    The submenus have the potential to contain many items so, to ensure they don’t drop off the end of the screen, I need to break to a new column after a set number of items.

    #200158
    Paulie_D
    Member

    I’m pretty sure that display:flex and position:absolute are incompatible in this respect.

    It works slightly better if you make the height a fixed value rather than max-height.

    http://jsfiddle.net/cpg28ugf/3/

    #200312
    Shaggy
    Participant

    Thanks, Paulie. The problem with setting a specific height is that some menus will have less than 6 items in them so that solution would create too “white space” in those instances.

    Currently, I’m using CSS columns until I can solve this flexbox issue but they’re creating a few problems of their own:

    1. I have to specify the exact number of columns I want, rather than allowing the menus to break as necessary.
    2. The menus will always spread across that number of columns, regardless of how many items there are in each.

    3. All columns are as wide as the longest item in each menu.

    Now, admittedly, I haven’t worked much with columns so I may well be overlooking something that would give me the same results I’m trying to achieve with the flexbox.

    #200313
    Paulie_D
    Member

    As always, a Codepen Demo would be useful.

    #200332
    Shaggy
    Participant

    For the columns, you mean? Here’s a Fiddle: http://jsfiddle.net/fndroeee/

    That one primarily illustrates the problem of both columns being the width of line 8 but you can see the other problems I’m having by adding or removing items, the list will no longer break to a new column after every 6th item.

    #200460
    gena
    Participant

    Have you considered using position-relative + top & display:none
    Then change to display: block on hover?

    I would think that would work with flexbox.

    .parent ul {
    display: none;
    position: relative;
    top: height of parent element;
    }
    .parent:hover ul {
    display: none;
    }

    #200489
    Shaggy
    Participant

    Unfortunately not, gena; setting the display property to “block” would overwrite the “flex” value.

    #243075
    matfish2
    Participant

    Had the same problem but with a one column dropdown. Solved by applying width:100% to the dropdown ul.

    #248982
    madhurgarg
    Participant

    If you want to give display:flex to absolutely positioned element, then you have to set left:0, right:0 like this:

    .parent {
       position: absolute;
       left: 0;
       right: 0;
       display: flex;
    }
    

    This worked for me !

Viewing 13 posts - 1 through 13 (of 13 total)
  • The forum ‘CSS’ is closed to new topics and replies.