translate(-50%,-50%) attributes: Unlike using margin to achieve centering, margin must know its own width and height, while translate can be centered without knowing the width and height. The percentage in the tranlate function is relative to the width and height. Example: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style media="screen"> .container { position: relative; width: 50%; } .container img { width: 100%; display: block; height: auto; } .overlay { width: 100%; height: 100%; position: absolute; left: 0; top: 0; right: 0; bottom: 0; opacity: 0; transition: 0.5s ease; background: rgb(0, 0, 0); } .container:hover .overlay { opacity: 0.5; } .text { color: white; font-size: 20px; position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); -ms-transform: translate(-50%, -50%); } </style> </head> <body> <h2>Fade-in effect</h2> <div class="container"> <img src="./img/photo2.jpg" alt="Avatar" class="image"> <div class="overlay"> <div class="text">Hello World</div> </div> </div> </body> </html> Effect: This is the end of this article about how to achieve horizontal and vertical centering with translate(-50%, -50%) in CSS. For more information about horizontal and vertical centering with css translate, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! |
<<: HTML vertical column display text to make the text display in vertical columns
>>: A brief discussion on the design and optimization of MySQL tree structure tables
Click here to return to the 123WORDPRESS.COM HTML ...
The best thing you can do for your data and compu...
This article shares the specific code for JavaScr...
I wrote a test program before, in which adding and...
Copy code The code is as follows: <!--doctype ...
Linux server hosts file configuration The hosts f...
Preface This article mainly shares with you an ex...
The <link> tag defines the relationship bet...
This article shares the specific code of JavaScri...
Table of contents 1. What is JSONP 2. JSONP cross...
Layout part: <div id="slider"> &l...
Section Course content Hours 1 Web Design Overvie...
Table of contents question background Idea & ...
Currently, Nginx has reverse proxyed two websites...
Table of contents Preface 1. Basic knowledge of f...