HTML centers elements horizontally and vertically
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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

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

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful 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 <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 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

WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.
