Home Web Front-end CSS Tutorial How to make two divs overlap with css

How to make two divs overlap with css

Nov 25, 2020 pm 05:37 PM
css div

Css method to make two divs overlap: first use the [position:absolute] attribute to set absolute positioning for the div; then use the z-index attribute to determine which div is on the upper layer. The z-index attribute is used to specify the stacking order of an element.

How to make two divs overlap with css

The operating environment of this tutorial: Windows 10 system, CSS2 version, this method is suitable for all brands of computers.

Introduction to important attributes:

The position attribute specifies the positioning type of the element.

(Learning video sharing: css video tutorial)

Attribute value:

  • absolute Generate absolutely positioned elements, relative Positioned on the first parent element other than static positioning. The position of the element is specified through the "left", "top", "right" and "bottom" attributes.

  • relative Generates a relatively positioned element, positioned relative to its normal position. Therefore, "left:20" adds 20 pixels to the element's LEFT position.

The z-index attribute specifies the stacking order of an element. Elements with a higher stacking order will always appear in front of elements with a lower stacking order.

Attribute value:

  • auto Default. The stacking order is equal to the parent element.

  • #number Set the stacking order of elements.

  • #inherit Specifies that the value of the z-index attribute should be inherited from the parent element.

Code implementation:

html code:

<div id="img_head" >
<img src="img/首页logo.png" class="head">
</div>
<div id="img_head_back">
<img src="img/背景.png" class="back" >
</div>

css code:

#img_head{
border: 1px solid #FF0000; 
position:absolute;
width:210px;
height: 210px;
z-index: 20;
left:600px;
top:170px;
text-align: center;
}
#img_head_back{
border: 0px solid #FF0000;
position:absolute;
width: 210px;
height: 210px;
left:600px;
top:170px;
z-index:10;
}

Related recommendations: CSS tutorial

The above is the detailed content of How to make two divs overlap with css. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1535
276
What are the virtual currency trading apps?_The top ten recommended official virtual currency trading apps in 2025 What are the virtual currency trading apps?_The top ten recommended official virtual currency trading apps in 2025 Aug 08, 2025 pm 06:42 PM

1. Binance is known for its huge transaction volume and rich trading pairs. It provides diversified trading models and perfect ecosystems. It also ensures the security of user assets through SAFU funds and multiple security technologies and attaches great importance to compliant operations; 2. OKX Ouyi provides a wide range of digital asset trading services and unified trading account models, actively deploys the Web3 field, and improves transaction security and experience through strict risk control and user education; 3. gate.io Sesame opens the door and has good currency speed and rich currency, provides diversified trading tools and value-added services, adopts multiple security verification mechanisms and adheres to the transparency of asset reserves to enhance user trust; 4. Huobi provides one-stop digital asset services with deep industry accumulation, with strong transaction depth and

How to use vw and vh units in CSS How to use vw and vh units in CSS Aug 07, 2025 pm 11:44 PM

vw and vh units achieve responsive design by associating element sizes with viewport width and height; 1vw is equal to 1% of viewport width, and 1vh is equal to 1% of viewport height; commonly used in full screen area, responsive fonts and elastic spacing; 1. Use 100vh or better 100dvh in the full screen area to avoid the influence of the mobile browser address bar; 2. Responsive fonts can be limited with 5vw and combined with clamp (1.5rem, 3vw, 3rem) to limit the minimum and maximum size; 3. Elastic spacing such as width:80vw, margin:5vhauto, padding:2vh3vw, can make the layout adaptable; pay attention to mobile device compatibility, accessibility and fixed width content conflicts, and it is recommended to give priority to using dvh first;

How to create a vertical line with CSS How to create a vertical line with CSS Aug 11, 2025 pm 12:49 PM

Use a div with border to quickly create vertical lines, and define styles and heights by setting border-left and height; 2. Use ::before or ::after pseudo-elements to add vertical lines without additional HTML tags, suitable for decorative separation; 3. In Flexbox layout, by setting the width and background color of the divider class, adaptive vertical dividers between elastic containers can be achieved; 4. In CSSGrid, insert vertical lines as independent columns (such as autowidth columns) into grid layout, which is suitable for responsive design; the most appropriate method should be selected according to the specific layout needs to ensure that the structure is simple and easy to maintain.

How to use the filter property in CSS How to use the filter property in CSS Aug 11, 2025 pm 05:29 PM

TheCSSfilterpropertyallowsvisualeffectslikeblur,brightness,andgrayscaletobeapplieddirectlytoHTMLelements.1)Usethesyntaxfilter:filter-function(value)toapplyeffects.2)Combinemultiplefilterswithspaceseparation,e.g.,blur(2px)brightness(70%).3)Commonfunct

How to use CSS selectors effectively How to use CSS selectors effectively Aug 11, 2025 am 11:12 AM

When using CSS selectors, low-specific selectors should be used first to avoid excessive limitations; 1. Understand the specificity level and use them reasonably in the order of type, class, and ID; 2. Use multi-purpose class names to improve reusability and maintainability; 3. Use attributes and pseudo-class selectors to avoid performance problems; 4. Keep the selector short and clear scope; 5. Use BEM and other naming specifications to improve structural clarity; 6. Avoid the abuse of tag selectors and:nth-child, and give priority to the use of tool classes or modular CSS to ensure that the style is controllable for a long time.

css mix-blend-mode example css mix-blend-mode example Aug 08, 2025 pm 12:59 PM

The mix-blend-mode attribute is used to control the mixing effect of element content and background. 1. Multiply can achieve the overlapping of text and background images; 2. Screen brightens the image and is suitable for dark backgrounds; 3. Overlay enhances contrast, combining multiply and screen features; 4. Difference creates a strong contrast, which is suitable for creative design; it is necessary to ensure that elements overlap and the correct z-index stacking order, and combined with isolation:isolate, the mixing range can be limited, and rich visual effects can be achieved by adjusting colors and modes.

How to change the list style in CSS How to change the list style in CSS Aug 17, 2025 am 10:04 AM

To change the CSS list style, first use list-style-type to change the bullet or numbering style. 1. Use list-style-type to set the bullet of ul to disc, circle or square, and the number of ol is decimal, lower-alpha, upper-alpha, lower-roman or upper-roman. 2. Remove the tag completely with list-style:none. 3. Use list-style-image:url('bullet.png') to replace it with a custom image. 4. Use list-style-position:in

How to prevent a line break in CSS How to prevent a line break in CSS Aug 08, 2025 pm 05:14 PM

Usewhite-space:nowraptopreventtextfrombreakingontomultiplelines,ensuringcontentstaysonasingleline;2.Applythispropertytoinline,inline-block,orflexitems,whereflex-wrap:nowrappreventsitemwrappingandwhite-space:nowrappreventsinternaltextwrapping;3.Forspe

See all articles