I am having some problems with margins in absolutely positioned elements.
As already answered in another question, margins in absolutely positioned elements are dictated by the first not statically positioned parent container.
CSS absolute positioned elements and margins
The example below is a simplified version of a multilevel dropdown menu, in which list items are generated dynamically. Meaning that is not possible to know the actual width beforehand (unless some javascript is implemented). The ">" with yellow background indicates another dropdown level. I'd like to have 5px margin between the end of the text and the ">".
See http://jsfiddle.net/3jzx8jh3
ul.level1 {
position: relative;
border: 1px solid black;
width: 100px;
}
ul.level2 {
position: absolute;
border: 1px solid red;
}
span.right {
margin-left: 5px;
float: right;
border: 1px solid black;
background-color: yellow;
}
li {
white-space: nowrap;
border: 1px solid blue;
}
<ul class="level1">
<li>Link1</li>
<li>Link2
<ul class="level2">
<li><span class="right">></span>Fear is the path to the dark side</li>
</ul>
</li>
</ul>
I've noticed that when I set margin-left to about 190px, the ">" starts moving right. Meaning that the margin is indeed set in respect of the non statically positioned parent, this is the parent li element.
Can I get the margin as I wish just using css?
Thanks