Consider the following simple HTML and CSS
a.rel{
position:relative;
}
button{
position:absolute;
top:0;
left:0;
}
Lorem ipsum dolor sit amet
<a class="rel" href="https://www.google.co.uk">
<img src=
"https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
<button>I'm a button</button>
</a>
Now consider CSS2 10.1.4.1
If the element has 'position: absolute', the containing block is established by the nearest ancestor with a 'position' of 'absolute', 'relative' or 'fixed', in the following way:
- In the case that the ancestor is an inline element, the containing block is the bounding box around the padding boxes of the first and the last inline boxes generated for that element. In CSS 2.1, if the inline element is split across multiple lines, the containing block is undefined.
or CSS3 3.1.2.2
If the element has position: absolute, the containing block is established by the nearest ancestor with a position other than static, in the following way:
In the case that the ancestor is inline-level, the containing block depends on the direction property of the ancestor:
- If the direction is ltr, the top and left of the containing block are the top and left content edges of the first box generated by the ancestor, and the bottom and right are the bottom and right content edges of the last box of the ancestor.
Doesn't this mean that the button should appear on top of the image to the top left? What part of the spec have I failed to understand when the button appears below the image?
<a>
todisplay: block;
previously, and added a/>
to the image just to check, and it suddenly worked. I thought I was on to something, but realized I hadn't removed mydisplay: block;
on thea
. Apologies, thanks for the correction :) – Sarita