Div on top of another div without absolute positioning năm 2024

Quick start with absolute and relative positioning

Div on top of another div without absolute positioning năm 2024

Div on top of another div without absolute positioning năm 2024

Cristian Salcescu

Follow

Published in

Frontend Essentials

4 min read

Jul 9, 2021

--

Photo by Jené Stephaniuk on Unsplash

This article looks at how to put a

element over and relative to another
element.

Let’s start by creating the two HTML tags and see what happens on the screen.

When the z-index property is not specified on any element, elements are stacked in the following order (from bottom to top):

  1. The background and borders of the root element.
  2. Descendant non-positioned elements, in order of appearance in the HTML.
  3. Descendant positioned elements, in order of appearance in the HTML.

See for an explanation of positioned and non-positioned elements.

Keep in mind, when the order property alters rendering from the order of appearance in the HTML within flex containers, it similarly affects the order for stacking context.

In this example, DIV

1 through DIV

4 are positioned elements. DIV

5 is static, and so is drawn below the other four elements, even though it comes later in the HTML markup.

You want to overlay a

over another
. For example, you have two
elements in a container:

item 1
item 2

You want the second item to be on top of the first item. How do you do this?

The Solution

You can set the position of the

that you want to be the overlay to absolute. This removes the item 2
from the normal document flow so that no space is created for item 2 in the page layout. Set the position of the container to relative so that you can position item 2 relative to the container. You can then position item 2 using the

.container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; }

0,

.container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; }

1,

.container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; }

2, and

.container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; }

3 CSS properties so that it is placed on top of item 1:

.container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; }

We moved item 2 20px from the top and 40px from the left of the container that item 2 is positioned relative to. This places item 2 on top of item 1. If you have multiple items, you can change the stacking order of items using the

.container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; }

4 property. A new stacking context is created by an element when the element has a position value of relative or absolute and the

.container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; }

4 value is not

.container { position: relative; } .item { width: 150px; height: 150px; border: 1px solid black; } .item1 { background: blue; } .item2 { top: 20px; left: 40px; position: absolute; background: red; }

9, which is the default value. The stacking order of elements is relative to their stacking context. Note that new stacking contexts can also be created by specific CSS properties such as

`0,
1,
2,
3, and
`4, as described .

Loved by over 4 million developers and more than 90,000 organizations worldwide, Sentry provides code-level observability to many of the world’s best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

How to overlay a div on another div without using position absolute?

.

.

step 1
.

Next.

.

.

step 2
.

Next.

Can you put a div on top of another div?

Creating an overlay effect simply means putting two div together at the same place but both the div appear when needed i.e while hovering or while clicking on one of the div to make the second one appear. Overlays are very clean and give the webpage a tidy look.nullHow to overlay one div over another div using CSS - GeeksforGeekswww.geeksforgeeks.org › how-to-overlay-one-div-over-another-div-using...null

How do I fix a div on top of another div?

The Solution If you have multiple items, you can change the stacking order of items using the z-index property. A new stacking context is created by an element when the element has a position value of relative or absolute and the z-index value is not auto , which is the default value.15 thg 4, 2023nullHow do I overlay one div over another div? - Sentrysentry.io › answers › how-to-overlay-one-div-over-another-divnull

How to make a div overlap another 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. ... .

Style the "overlay" class by using the z-index, margin and background properties..