Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

div width 100% +10px: relative to parent?

I have a ul/li menu with display:table/table-cell:auto layout, stretched to fill the container and distribute horizontal space between menu items. I have to align an additional UI element to the last menu item. I can achieve this by adding that element as a child of the last li element and making it 100% wide.

However, I need to position this UI element (language selector) partially outside of the main content, so I position this by a relative right:-10px. But the side effect is that the left edge of the selector moves to the right, compared to the menu element. Normally I'd increase the selector div's width by 10px, but since it's 100%, I can't do that. And the parent li's width is determined by its text content.

Is there a way to get this right using pure css? I can't add padding on the selector, because I need its child content to fill the whole div.

<div class="menu">
    <ul>
        <li><a href="#">first</a>
        </li>
        <li><a href="#">second</a>
            <div id="selector">
                <a href="#">EN</a>
                <a href="#">FR</a>
            </div>
        </li>
    </ul>
</div>

Here's a working example: http://jsfiddle.net/hJXng/13/

like image 394
pandoukht Avatar asked Mar 03 '13 06:03

pandoukht


People also ask

How do you make a div full width of a parent?

Method 1: First method is to simply assign 100% width and 100% height to the child div so that it will take all available space of the parent div. Consider this HTML for demonstration: HTML.

Is a div 100% width by default?

So, by nature, it doesn't matter how much content the element contains because its width is always 100%, that is, until we alter it. Think of elements like <p> , <article> , <main> , <div> , and so many more.

How do you make a div 100 width?

The width property is used to fill a div remaining horizontal space using CSS. By setting the width to 100% it takes the whole width available of its parent. Example 1: This example use width property to fill the horizontal space. It set width to 100% to fill it completely.

What does width 100% do in CSS?

It seems like this should be one of the easiest things to understand in CSS. If you want a block-level element to fill any remaining space inside of its parent, then it's simple — just add width: 100% in your CSS declaration for that element, and your problem is solved.


1 Answers

Depending on what browsers you want to support, a simple option is to use calc:

width: calc(100% + 10px);

See also: Can I use calc()
Example: http://jsfiddle.net/hJXng/19/

like image 121
Kobi Avatar answered Oct 19 '22 19:10

Kobi