Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move an absolute positioned element's right side to the left side of it's parent

Tags:

html

css

The Problem

I have a container within another one. The rule display: inline-block; on the outer container is a given. I want that the content of the inner container ends where the content of the outer one starts. I tried using left: -100%; but naturally it moves the content only as far as the outer container is wide. The use case: The inner element will be a tooltip shown on hover aligned on the left side.

How can I got both elements aligned after each other and not overlapping without using JavaScript?

HTML

<div>
    Short content
    <div>This is very long test sentence.</div>
</div>

CSS

div {
    position: relative;
    display: inline-block;
    margin: 40px 200px;
    background: rgba(123, 234, 345, 0.7);
}

div > div {
    position: absolute;
    top: 20px;
    left: -100%;
    margin: 0;
    background: rgba(0, 0, 0, 0.1);
    white-space: nowrap;
}

Demo on jsFiddle

Demo

like image 806
lampshade Avatar asked Mar 03 '14 15:03

lampshade


People also ask

How do you move an absolute positioned element?

Absolute Positioning You can use two values top and left along with the position property to move an HTML element anywhere in the HTML document. Move Left - Use a negative value for left. Move Right - Use a positive value for left. Move Up - Use a negative value for top.

Is absolute position relative to parent?

Absolute In position: relative , the element is positioned relative to itself. However, an absolutely positioned element is relative to its parent. An element with position: absolute is removed from the normal document flow. It is positioned automatically to the starting point (top-left corner) of its parent element.

What determines the position of an element that has position absolute set?

The absolutely positioned element is positioned relative to its nearest positioned ancestor (i.e., the nearest ancestor that is not static ).


1 Answers

Try using right: 100%; instead of left: -100%;. Like so:

http://jsfiddle.net/dkv9W/1/

like image 158
skip405 Avatar answered Oct 12 '22 22:10

skip405