translate(-50%,-50%) in CSS achieves horizontal and vertical centering effect

translate(-50%,-50%) in CSS achieves horizontal and vertical centering effect

translate(-50%,-50%) attributes:
Move it up and left by 50% of its length and width to center it.

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.
(When top and left are 50%, the upper left corner of the window is used as the origin).

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:

insert image description here

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

Recommend

Markup language - CSS layout

Click here to return to the 123WORDPRESS.COM HTML ...

Complete steps to set up automatic updates in CentOS 8

The best thing you can do for your data and compu...

JavaScript implements the nine-grid mobile puzzle game

This article shares the specific code for JavaScr...

Solution to the problem that input in form cannot be submitted when disabled

I wrote a test program before, in which adding and...

Detailed explanation of hosts file configuration on Linux server

Linux server hosts file configuration The hosts f...

Example code for text origami effect using CSS3

Preface This article mainly shares with you an ex...

The rel attribute of the HTML link tag

The <link> tag defines the relationship bet...

JavaScript to implement checkbox selection or cancellation

This article shares the specific code of JavaScri...

JSONP cross-domain simulation Baidu search

Table of contents 1. What is JSONP 2. JSONP cross...

Web Design Teaching or Learning Program

Section Course content Hours 1 Web Design Overvie...

js Promise concurrent control method

Table of contents question background Idea & ...

Detailed explanation of mkdir command in Linux learning

Table of contents Preface 1. Basic knowledge of f...