Pure CSS to achieve three-dimensional picture placement effect example code

Pure CSS to achieve three-dimensional picture placement effect example code

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%;<br> padding: 2%;<br> margin: 5%;<br>
}  

<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 percentage basis of an element's height is the height of its parent element, and the percentage basis of an element's width, padding, and margin is 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 has different sizes.

· 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>

Summarize

The above is the pure CSS method introduced by the editor to achieve the three-dimensional picture arrangement effect. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!

<<:  Difference between querySelector and getElementById methods in JS

>>:  Detailed process of compiling and installing Storm on Kylin V10 server

Recommend

Html+CSS floating advertisement strip implementation

1.html part Copy code The code is as follows: <...

Three uses and differences of MySQL not equal

Judgment symbols are often used in MySQL, and not...

HTML form and the use of form internal tags

Copy code The code is as follows: <html> &l...

CSS3 frosted glass effect

If the frosted glass effect is done well, it can ...

jQuery implements accordion effects

This article shares the specific code of jQuery t...

JavaScript built-in date and time formatting time example code

1. Basic knowledge (methods of date objects) 😜 ge...

Mysql method to copy a column of data in one table to a column in another table

mysql copy one table column to another table Some...

Summary of common problems and solutions in Vue (recommended)

There are some issues that are not limited to Vue...

Implementation steps for installing Redis container in Docker

Table of contents Install Redis on Docker 1. Find...

JavaScript CollectGarbage Function Example

First, let's look at an example of memory rel...

Solution to the problem that Docker cannot stop or delete container services

Preface Today, a developer gave me feedback that ...

HTML Tutorial: Ordered Lists

<br />Original text: http://andymao.com/andy...

How to dynamically modify the replication filter in mysql

MySQL dynamically modify replication filters Let ...