Sample code for achieving three-dimensional picture placement effect with pure CSS

Sample code for achieving three-dimensional picture placement effect with pure CSS

1. Percentage basis for element width/height/padding/margin

When setting the percentage of an element's width/height/padding/margin, do you know what the benchmark is?

For example:

.parent {
  width: 200px;
  height: 100px;
}
.child {
  width: 80%;
  height: 80%;
}
.childchild {
  width: 50%;
  height: 50%;
 padding: 2%;
  margin: 5%;

}
<div class="parent">
        <div class="child">
            <div class="childchild"></div>
        </div>
    </div>

In the above code, what is the width of the childchild element? What is height? What is the padding? What is the margin?

The height percentage of an element is based on the height of its parent element, and the width, padding, and margin percentages of an element are based on the width of its parent element.

From this, I believe everyone has already figured it out. You can try it out~

In interviews, you will often encounter a simple CSS style question, which is to implement an adaptive square. The principle is based on the above knowledge. Just need

#box {
            width: 50%;
            padding-top: 50%;
            background: #000;
        }

Because the base values ​​of element width and padding are both the width of the parent element, and the width of the body is the browser window. So this setting will make the square adaptive as the browser window size changes.

2. Use pure CSS to achieve the effect of three-dimensional picture placement

Now back to the topic, if you want to achieve the three-dimensional placement effect of the pictures shown in the following figure, you need to apply the knowledge of padding, width, and height.

It looks a little familiar. Is it similar to the style of recommended books in novel software?

Here, first let's look at its placement. One picture is horizontally centered and in the front, while the other two pictures are aligned left and right and a little further back, presenting a three-dimensional placement. Here I learned a way to achieve this three-dimensional effect by simply relying on CSS.

The different heights are supported by padding-top, which is large or small. · The before and after effects are controlled by the z-index folding order. The arrangement is controlled by the nth-of-type pseudo-element + position.

Do you have any idea? Let’s not beat around the bush and get straight to the code.

<html>
    <head>
        <style>
            * {
                margin: 0;
                padding: 0;
            }
            .box {
                width: 300px;
                height: 200px;
                position: relative;
            }
            .img {
                width: auto;
                height: 0;
            }
            .box img {
                width: 100%;
                display: inline-block;
            }
            .box .img:nth-of-type(1) {
                display: inline-block;
                position: absolute;
                left: 50%;
                top: 50%;
                padding-bottom: 50%;
                transform: translate(-50%, -50%);
                z-index: 6;
            }
            .box .img:nth-of-type(2), .box .img:nth-of-type(3) {
                position: absolute;
                top: 50%;
                transform: translateY(-50%);
                padding-bottom: 63%;
                z-index: 3;
            }
            .box .img:nth-of-type(2) {
                right: 0;
            }
            .box .img:nth-of-type(3) {
                left: 0;
            }
        </style>
    </head>
    <body>
        <div class="box">
            <div class="img">
                <img src="https://febaidu.com/list/img/3ns.png" />
            </div>
            <div class="img">
                <img src="https://febaidu.com/list/img/3ns.png" />
            </div>
            <div class="img">
                <img src="https://febaidu.com/list/img/3ns.png" />
            </div>
        </div>
    </body>
</html>

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

<<:  HTML sub tag and sup tag

>>:  React internationalization react-i18next detailed explanation

Recommend

Vue implements 3 ways to switch tabs and switch to maintain data status

3 ways to implement tab switching in Vue 1. v-sho...

MySQL online log library migration example

Let me tell you about a recent case. A game log l...

CSS scroll bar style modification code

CSS scroll bar style modification code .scroll::-...

MySQL high availability solution MMM (MySQL multi-master replication manager)

1. Introduction to MMM: MMM stands for Multi-Mast...

HTML Basic Notes (Recommended)

1. Basic structure of web page: XML/HTML CodeCopy...

js to achieve a simple magnifying glass effect

This article shares the specific code of js to ac...

HTML table tag tutorial (24): horizontal alignment attribute of the row ALIGN

In the horizontal direction, you can set the row ...

Two ways to make IE6 display PNG-24 format images normally

Method 1: Please add the following code after <...

Practical record of Vue3 combined with TypeScript project development

Table of contents Overview 1. Compositon API 1. W...

Detailed analysis of javascript data proxy and events

Table of contents Data Brokers and Events Review ...

TypeScript uses vscode to monitor the code compilation process

Install Install ts command globally npm install -...

Can asynchrony in JavaScript save await?

I knew before that to synchronously obtain the re...

How to use explain to query SQL execution plan in MySql

The explain command is the primary way to see how...