- How to overlay one div over another div
- 9 Answers 9
- The Problem
- Proposed Solution
- CSS Position Required Knowledge
- The Solution
- The Result
- An Alternate (Grid) Solution
- An Alternate (No Wrapper) Solution
- Show child element above parent element using CSS
- 6 Answers 6
- How to Overlay One Div Over Another
- Create HTML
- Add CSS
- Example of overlaying one over another:
- Result
- Example of overlaying one over another with hovering effects:
How to overlay one div over another div
I need assistance with overlaying one individual div over another individual div . My code looks like this:
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 .
Not the case in this question but if you have one div inside another div the inner div may be fully or partially masked due to overflow: hidden , use overflow: visible instead.
9 Answers 9
#container < width: 100px; height: 100px; position: relative; >#navi, #infoi < width: 100%; height: 100%; position: absolute; top: 0; left: 0; >#infoi
a b
I would suggest learning about position: relative and child elements with position: absolute .
thanks alex for your help but what I am finding now is that when I resize my window and drag it to be smaller, my info image is not staying with it’s parent div. Basically want it to move with the parent div and stay pretty much at the same position even though the screen has been resized somewhat.
@tonsils: The instructions by alex should give you the desired result, so there must be something else causing the problem you describe. Could you provide us with a sample of the code (HTML + CSS) so we can help you?
absolute ly positioned elements are positioned relative to their nearest explicitly positioned ( position:absolute|relative|fixed ) parent element. just further clarification . jsfiddle.net/p5jkc8gz
Depending on the case, this can be adapted. In my situation I didn’t need #navi to be top:0 left:0, and as a general case If you are having HTML code, it’s going to be situated as long as the HTML tree is parsed. So maybe in this case that definition is not necessary.
Adding the in z-index:10; resolved the issue I was having with the textarea being uneditable when other textarea’s was not visible. Thank you, Alex.
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 two separate, sibling, elements and the goal is to position the second element (with an id of infoi ), so it appears within the previous element (the one with a class of navi ). The HTML structure cannot be changed.
Proposed Solution
To achieve the desired result we’re going to move, or position, the second element, which we’ll call #infoi so it appears within the first 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 are relative , absolute , sticky , and fixed . By setting an element’s position to one of these other values it’s now possible to use a combination of the following four properties to position the element:
In other words, by setting position: absolute , we can add top: 100px to position the element 100 pixels from the top of the page. Conversely, if we set bottom: 100px the element would be positioned 100 pixels 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 the body element. position: absolute with top: 100px means the element is positioned 100 pixels from the top of the body element.
The position frame of reference, or position context, can be altered by setting the position of a parent element to any value other than position: static . That is, we can create a new position context by giving a parent element:
For example, if a element is given position: relative , any child elements use the as their position context. If a child element were given position: absolute and top: 100px , the element would be positioned 100 pixels from the top of the element, because the 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
element would cover the
element. Since
comes after
in the HTML structure it has a higher stacking order.
The stacking order can be changed with CSS using the z-index or order 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 so we can create a new position context.
Then create a new position context by giving .wrapper a position: relative .
With this new position context, we can position #infoi within .wrapper . First, give #infoi a position: absolute , allowing us to position #infoi absolutely in .wrapper .
Then add top: 0 and right: 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.
/* * position: relative gives a new position context */ .wrapper < position: relative; >/* * The .navi properties are for styling only * These properties can be changed or removed */ .navi < background-color: #eaeaea; height: 40px; >/* * Position the #infoi element in the top-right * of the .wrapper element */ #infoi < position: absolute; top: 0; right: 0; /* * Styling only, the below can be changed or removed * depending on your use case */ height: 20px; padding: 10px 10px; >
An Alternate (Grid) Solution
Here’s an alternate solution using CSS Grid to position the .navi element with the #infoi element in the far right. I’ve used the verbose grid properties to make it as clear as possible.
:root < --columns: 12; >/* * Setup the wrapper as a Grid element, with 12 columns, 1 row */ .wrapper < display: grid; grid-template-columns: repeat(var(--columns), 1fr); grid-template-rows: 40px; >/* * Position the .navi element to span all columns */ .navi < grid-column-start: 1; grid-column-end: span var(--columns); grid-row-start: 1; grid-row-end: 2; /* * Styling only, the below can be changed or removed * depending on your use case */ background-color: #eaeaea; >/* * Position the #infoi element in the last column, and center it */ #infoi
An 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 use position: relative . This allows us to reposition the #infoi element from its default position below the .navi element. With position: relative we can use a negative top value to move it up from its default position, and a left value of 100% minus a few pixels, using left: calc(100% — 52px) , to position it near the right-side.
/* * The .navi properties are for styling only * These properties can be changed or removed */ .navi < background-color: #eaeaea; height: 40px; width: 100%; >/* * Position the #infoi element in the top-right * of the .wrapper element */ #infoi < position: relative; display: inline-block; top: -40px; left: calc(100% - 52px); /* * Styling only, the below can be changed or removed * depending on your use case */ height: 20px; padding: 10px 10px; >
Show child element above parent element using CSS
I have two divs, one nested inside of the other. The parent element has a defined width/height. How can I show the child div above the parent (outside of it) using only CSS? EDIT: Sorry, maybe I should clarify that I mean «above» as along the z axis. And yes, I already tried z-index. My problem is that when the child element is larger than the parent, it results in a «frame» or «window» effect, cutting off part of the div.
6 Answers 6
Set overflow: visible; on the parent div.
Changed to reflect asker’s update.
You could use the position definition to position it either relatively or absolutely on the page. IE:
To show it directly above you would replace 100px in this statement with the size of the child box.
Using position: absolute will get rid of the empty space left by the child when it gets shifted up.
Edit — after reading your update: position:absolute still applies for this situation too. It gets the child out of the parent. Then you use the margins to position it how you want.
This way you can make the child bigger than the parent and above it.
If you’re sure you need to do this, then try putting
margin-top: -100px; on the child element or however many px is needed to make it appear above.
Similar to what Chacha said, you should give the parent a position of relative and the child a position of absolute, then give the child top: -100px.
Does the parent have overflow set to hidden? That might hinder your efforts.
How to Overlay One Div Over Another
Creating an overlay effect for two elements can be easily done with CSS. This can be done with the combination of the CSS position and z-index properties. The z-index of an element defines its order inside a stacking context. In our examples, we’ll use the “absolute” and “relative” values of the position property and add the z-index property to specify the stacking order for the positioned elements.
Create HTML
- Use a element with the class named “container”.
- Add two other elements within the first one. Add classes to them as well.
div class="container"> div class="box"> div> div class="box overlay"> div> div>
Add CSS
- Specify the width and height of the «container» class. Set the position to «relative» and add the margin property.
- Set both the width and height of the «box» class to «100%». Specify the position with the «absolute» value. Add the top and left properties. Also, specify the background and opacity of the «box» class.
- Style the «overlay» class by using the z-index , margin and background properties.
.container < width: 150px; height: 150px; position: relative; margin: 30px; > .box < width: 100%; height: 100%; position: absolute; top: 0; left: 0; opacity: 0.7; background: #0057e3; > .overlay < z-index: 9; margin: 30px; background: #009938; >
Now, we can bring together the parts of our code.
Example of overlaying one over another:
html> html> head> title>Title of the document title> style> .container < width: 150px; height: 150px; position: relative; margin: 30px; > .box < width: 100%; height: 100%; position: absolute; top: 0; left: 0; opacity: 0.7; background: #0057e3; > .overlay < z-index: 9; margin: 30px; background: #009938; > style> head> body> div class="container"> div class="box"> div> div class="box overlay"> div> div> body> html>
Result
Let’s see another example where we use a bit more CSS. Here, we add some hovering effects using the CSS :hover pseudo-class.
Example of overlaying one over another with hovering effects:
html> html> head> title>Title of the document title> style> .container < position: absolute; height: 250px; width: 400px; background-color: #7d807e; > .box1 < color: #fff; padding-top: 60px; padding-left: 50px; font-size: 50px; font-weight: bold; > .box2 < padding-left: 50px; > .box1:hover < z-index: -1; opacity: 0.5; font-size: 30px; text-align: center; transform-style: all; transition-duration: 2s; > .box2:hover < z-index: -1; opacity: 0.3; font-size: 40px; text-align: center; transform-style: all; transition-duration: 2s; > style> head> body> div class="container"> div class="box1">W3Docs div> div class="box2">Learn programming div> div> body> html>