Home  >  Article  >  Web Front-end  >  How to put the image on top of the element in css

How to put the image on top of the element in css

WBOY
WBOYOriginal
2021-12-06 09:45:0516085browse

In CSS, you can use the "z-index" attribute to place the image on top of the element. This attribute is used to set the stacking order of the elements. The larger the value of the attribute, the closer the order in which the elements are displayed. Previously, the syntax was "picture element {z-index:number;}".

How to put the image on top of the element in css

The operating environment of this tutorial: Windows 10 system, CSS3&&HTML5 version, Dell G3 computer.

How to place the image on top of the element in css

In css, through positioning, you can use z-index to define the element hierarchical relationship. The following example explains how to pin an image to the top in CSS.

1. Create a new html file, named test.html, to explain how to pin pictures in css. Use the div tag to create a module and the img tag to create an image. Add a class attribute to the div and img tags respectively for style definition.

In the css tag, define the style of the div through class (mytest), set its height and width to 400px, the background color to gray, and use z-index to set the level of the div to 0.

How to put the image on top of the element in css

2. In the css tag, define the img style through class (mypic), set the position attribute of the image to absolute positioning, 20px from the top, 20px from the left, and Set the level of img to 1 through z-index, that is, above the div, so that the image is on top.

How to put the image on top of the element in css

Open the test.html file in the browser to check the effect.

How to put the image on top of the element in css

Summary:

1. Use the div tag to create a module and the img tag to create an image.

2. Add a class attribute to the div and img tags respectively for style definition.

3. In the css tag, define the style of the div through class (mytest), set its height and width to 400px, the background color to gray, and use z-index to set the level of the div to 0.

4. In the css tag, define the img style through class (mypic), set the position attribute of the image to absolute positioning, 20px from the top, 20px from the left, and set the img level through z-index. 1, that is, above the div, so that the picture is on top.

(Learning video sharing: css video tutorial)

The above is the detailed content of How to put the image on top of the element in css. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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