I need assistance with overlaying one individual div
over another individual div
.
My code looks like this:
<div class="navi"></div>
<div id="infoi">
<img src="info_icon2.png" height="20" width="32"/>
</div>
Unfortunately I cannot nest the div#infoi
or the img
, inside the first div.navi
.
It has to be two separate div
s as shown but I need to know how I could place the div#infoi
over the div.navi
and to the right most side and centered on top of the div.navi
.
I would suggest learning about
position: relative
and child elements withposition: absolute
.The accepted solution works great, but IMO lacks an explanation as to why it works. The example below is boiled down to the basics and separates the important CSS from the non-relevant styling CSS. As a bonus, I've also included a detailed explanation of how CSS positioning works.
TLDR; if you only want the code, scroll down to The Result.
The Problem
There are 2 separate, sibling, elements and the goal is to position the 2nd element (with an
id
ofinfoi
) so it appears within the previous element (the one with aclass
ofnavi
). The HTML structure cannot be changed.Proposed Solution
To achieve the desired result we're going to move, or position, the 2nd element, which we'll call
#infoi
so it appears within the 1st element, which we'll call.navi
. Specifically, we want#infoi
to be positioned in the top-right corner of.navi
.CSS Position Required Knowledge
CSS has several properties for positioning elements. By default, all elements are
position: static
. This means the element will be positioned according to its order in the HTML structure, with few exceptions.The other
position
values arerelative
,absolute
, andfixed
. By setting an element'sposition
to one of these 3 values it's now possible to use a combination of the following 4 properties to position the element:top
right
bottom
left
In other words, by setting
position: absolute
, we can addtop: 100px
to position the element 100px from the top of the page. Conversely, if we setbottom: 100px
the element would be positioned 100px from the bottom of the page.Here's where many CSS newcomers get lost -
position: absolute
has a frame of reference. In the example above, the frame of reference is thebody
element.position: absolute
withtop: 100px
means the element is positioned100px
from the top of thebody
element.The position frame of reference, or position context, can be altered by setting the
position
of a parent element to any value other thanposition: static
. That is, we can create a new position context by giving a parent element:position: absolute;
position: relative;
position: fixed;
For example, if a
<div class="parent">
element is givenposition: relative
, any child elements use the<div class="parent">
as their position context. If a child element were givenposition: absolute
andtop: 100px
, the element would be positioned 100px from the top of the<div class="parent">
element, because the<div class="parent">
is now the position context.The other factor to be aware of is stack order - or how elements are stacked in the z-direction. The must-know here is the stack order of elements are, by default, defined by the reverse of their order in the HTML structure. Consider the following example:
In this example, if the two
<div>
elements were positioned in the same place on the page, the<div>Top</div>
element would cover the<div>Bottom</div>
element. Since<div>Top</div>
comes after<div>Bottom</div>
in the HTML structure it has a higher stacking order.The stacking order can be changed with CSS using the
z-index
ororder
properties.We can ignore the stacking order in this issue as the natural HTML structure of the elements means the element we want to appear on
top
comes after the other element.So, back to the problem at hand - we'll use position context to solve this issue.
The Solution
As stated above, our goal is to position the
#infoi
element so it appears within the.navi
element. To do this, we'll wrap the.navi
and#infoi
elements in a new element<div class="wrapper">
so we can create a new position context.Then create a new position context by giving
.wrapper
aposition: relative
.With this new position context, we can position
#infoi
within.wrapper
. First, give#infoi
aposition: absolute
, allowing us to position#infoi
absolutely in.wrapper
.Then add
top: 0
andright: 0
to position the#infoi
element in the top-right corner. Remember, because the#infoi
element is using.wrapper
as its position context, it will be in the top-right of the.wrapper
element.Because
.wrapper
is merely a container for.navi
, positioning#infoi
in the top-right corner of.wrapper
gives the effect of being positioned in the top-right corner of.navi
.And there we have it,
#infoi
now appears to be in the top-right corner of.navi
.The Result
The example below is boiled down to the basics, and contains some minimal styling.
The Alternate (No Wrapper) Solution
In the case we can't edit any HTML, meaning we can't add a wrapper element, we can still achieve the desired effect.
Instead of using
position: absolute
on the#infoi
element, we'll useposition: relative
. This allows us to reposition the#infoi
element from it's default position below the.navi
element. Withposition: relative
we can use a negativetop
value to move it up from it's default position, and aleft
value of100%
minus a few pixels, usingleft: calc(100% - 52px)
, to position it near the right-side.I am not much of a coder nor an expert in CSS but I am still using your idea in my web designs. I have tried different resolutions too:
Of course there will be a wrapper around both of them. You can control the location of the menu div which will be displayed within the header div with left margins and top positions. You can also set the div menu to float right if you like. Hope this helps.
By using a
div
with stylez-index:1;
andposition: absolute;
you can overlay yourdiv
on any otherdiv
.z-index
determines the order in which divs 'stack'. A div with a higherz-index
will appear in front of a div with a lowerz-index
. Note that this property only works with positioned elements.This is what you need:
Here follows a simple solution 100% based on CSS. The "secret" is to use the
display: inline-block
in the wrapper element. Thevertical-align: bottom
in the image is a hack to overcome the 4px padding that some browsers add after the element.Advice: if the element before the wrapper is inline they can end up nested. In this case you can "wrap the wrapper" inside a container with
display: block
- usually a good and olddiv
.