Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making parent container stretch to width absolutely positioned child and vice versa

I am working with some markup for a drop down menu.

The markup is as follows:

<div class="select">
    <a href="#" class="anchor menu-active">
        <label>val1</label><span class="arrow"><label></label></span>
    </a>

    <div class="container menu-visible">
        <ul>
            <li>
                <a value="val1" class="item-selected">val1</a>
            </li>
            <li>
                <a value="val2333333333">val2333333333</a>
            </li>
        </ul>
    </div>
</div>

The first a represents the menu item and the container div represents a list of submenu items.

  • The width of the submenu items are variable as they are generated dynamically.
  • The text in anchor is bolded, so in certain cases, the width of the anchor may be bigger than the submenu list.

What needs I need to do: Both the width of the container and the anchor must be the same. It is possible that the submenu can be wider than the anchor or vice-versa. In addition, the width should not be artificially restricted and should be allowed to expand with the wider content, whether it is the anchor or the container.

Here's the CSS I have been playing with, but can't seem to get the anchor to expand to fit the container at all:

.container{
    position: absolute;
}

.anchor{
    font-weight: bold;
}

li{
    background: blue;
}

ul{
    display: inline-block;
}

.select{
    background: red;
    display: inline-block;
}

And the fiddle: http://jsfiddle.net/MxdQS/

I wish to do this with CSS only, where possible, so no javascript solutions please :)

And a diagram to better demonstrate what I am talking about: enter image description here

Some clarification: If I have 2 containers of different widths and 1 is positioned absolutely, how can I match them up together so that the shorter container will always expand to the width of the longer container?

Could anyone advice as to how this can be done?

like image 240
F21 Avatar asked Dec 11 '25 02:12

F21


1 Answers

Check DEMO HERE

CSS

.container{
    position: absolute;
    width:150px;
}

.anchor{
    font-weight: bold;
}
ul{
    margin:0;
    padding:0;
}

li{
    background: blue;
    list-style:none;
    margin:0;
    padding:0l
}



.select{
    background: red;
    display: inline-block;
    width:150px;
}

UPDATE

check updated DEMO

like image 166
Kamal Avatar answered Dec 13 '25 11:12

Kamal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!