When we design the page, we often need to center the p, and display it horizontally and vertically relative to the page window, such as centering the login window.
So far, many methods have been explored.
HTML:
<body> <p class="maxbox"> <p class="minbox align-center"></p> </p> </body>
Rendering (the following methods have the same renderings):
First Type: CSS absolute positioning
mainly uses absolute positioning, and then uses margin to adjust to the middle position.
Parent element:
.maxbox{ position: relative; width: 500px; height: 500px; margin: 5px; box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); }
Child element:
.minbox{ width: 200px; height: 200px; box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); }
Horizontal and vertical center alignment:
.align-center{ position: absolute; left: 50%; top: 50%; margin-left: -100px; /*width/-2*/ margin-top: -100px; /*height/-2*/ }
Second type: CSS absolute positioning + Javascript/JQuery
Mainly use absolute positioning, and then use Javascript/JQuery to adjust to the middle position. Compared with the first method, this method improves the flexibility of the class.
Parent element:
.maxbox{ position: relative; width: 500px; height: 500px; margin: 5px; box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); }
Child element:
.minbox{ width: 200px; height: 200px; box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); }
Horizontal vertical center alignment:
.align-center{ position: absolute; left: 50%; top: 50%; }
JQuery:
$(function(){ $(".align-center").css( { "margin-left": ($(".align-center").width()/-2), "margin-top": ($(".align-center").height()/-2) } ); });
The third type: CSS3 absolute positioning + displacement
Using absolute positioning and CSS3's transform: translate can also achieve the same effect.
Parent element:
.maxbox{ position: relative; width: 500px; height: 500px; margin: 5px; box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); }
Child element:
.minbox{ width: 200px; height: 200px; box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); }
Horizontal vertical center alignment:
.align-center{ position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); transform: translate(-50%, -50%); /*向左向上位移*/ }
Fourth type : Flexbox: [Telescopic Layout Box Model]
It is too easy for the Flexbox model to make elements horizontal and vertical.
You need to change the HTML here:
<p class="maxbox align-center"> <p class="minbox"></p> </p>
Parent element:
.maxbox{ position: relative; width: 500px; height: 500px; margin: 5px; box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); }
Child element:
.minbox{ width: 200px; height: 200px; box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.8), -1px -1px 1px rgba(0, 0, 0, 0.8); }
Horizontal and vertical center alignment:
.align-center{ display: flex; display: -webkit-flex; /*兼容问题*/ justify-content: center; align-items: center; }
Comparison of several methods:
The first CSS absolute positioning margin adjustment, with good compatibility But it lacks flexibility. If there are many boxes that need to be horizontally and vertically centered, they need to write different .align-center because of their different width and height.
The second one uses scripting language, which has good compatibility and makes up for the shortcomings of the first one. The effect of horizontal and vertical centering will not be affected by changes in width and height.
The third one uses some new properties of CSS3 and is compatible with IE10, Chrome, Firefox, and Opera. The compatibility is not very good, and the horizontal and vertical centering effect will not be affected by changes in width and height.
Using the Flexbox model, compatible with Firefox, Opera and Chrome, IE is completely wiped out. It also does not affect the horizontal and vertical centering effect due to changes in width and height.
The above is the entire content of this article, I hope it will be helpful to everyone's study.
For more HTML related articles on horizontal and vertical centering of elements, please pay attention to the PHP Chinese website!

The article discusses the HTML <datalist> element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

The article discusses the HTML <progress> element, its purpose, styling, and differences from the <meter> element. The main focus is on using <progress> for task completion and <meter> for stati

The article discusses the HTML <meter> element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates <meter> from <progress> and ex

The article discusses the <iframe> tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

This article explains the HTML5 <time> element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
